Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,186 +0,0 @@
|
|
| 1 |
-
import spaces
|
| 2 |
-
import torch
|
| 3 |
-
import tempfile
|
| 4 |
-
import shutil
|
| 5 |
-
import gradio as gr
|
| 6 |
-
|
| 7 |
-
from transformers import (
|
| 8 |
-
AutoModel,
|
| 9 |
-
AutoModelForCausalLM,
|
| 10 |
-
AutoTokenizer,
|
| 11 |
-
AutoProcessor,
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
from huggingface_hub import create_repo, upload_folder
|
| 15 |
-
from llmcompressor import oneshot
|
| 16 |
-
from llmcompressor.modifiers.quantization import QuantizationModifier
|
| 17 |
-
from llmcompressor.utils import dispatch_for_generation
|
| 18 |
-
|
| 19 |
-
@spaces.GPU(duration=300)
|
| 20 |
-
def fp8_dynamic_upload(
|
| 21 |
-
source_model,
|
| 22 |
-
target_repo,
|
| 23 |
-
hf_token,
|
| 24 |
-
max_new_tokens
|
| 25 |
-
):
|
| 26 |
-
try:
|
| 27 |
-
if not source_model or not target_repo or not hf_token:
|
| 28 |
-
return "❌ Please fill all required fields."
|
| 29 |
-
|
| 30 |
-
logs = []
|
| 31 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 32 |
-
|
| 33 |
-
# -------------------------------------------------
|
| 34 |
-
# Load model + processor
|
| 35 |
-
# -------------------------------------------------
|
| 36 |
-
logs.append(f"🚀 Loading model: {source_model}")
|
| 37 |
-
|
| 38 |
-
processor = AutoProcessor.from_pretrained(
|
| 39 |
-
source_model,
|
| 40 |
-
trust_remote_code=True,
|
| 41 |
-
token=hf_token
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 45 |
-
source_model,
|
| 46 |
-
attn_implementation="flash_attention_2",
|
| 47 |
-
torch_dtype=torch.bfloat16,
|
| 48 |
-
device_map="auto",
|
| 49 |
-
trust_remote_code=True,
|
| 50 |
-
token=hf_token
|
| 51 |
-
).eval()
|
| 52 |
-
|
| 53 |
-
logs.append("✅ Model loaded successfully")
|
| 54 |
-
|
| 55 |
-
# -------------------------------------------------
|
| 56 |
-
# Apply FP8_DYNAMIC Quantization
|
| 57 |
-
# -------------------------------------------------
|
| 58 |
-
logs.append("🔧 Applying FP8_DYNAMIC quantization")
|
| 59 |
-
|
| 60 |
-
recipe = QuantizationModifier(
|
| 61 |
-
targets="Linear",
|
| 62 |
-
scheme="FP8_DYNAMIC",
|
| 63 |
-
ignore=[
|
| 64 |
-
"lm_head",
|
| 65 |
-
],
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
oneshot(model=model, recipe=recipe)
|
| 69 |
-
|
| 70 |
-
logs.append("✅ Quantization applied successfully")
|
| 71 |
-
|
| 72 |
-
# -------------------------------------------------
|
| 73 |
-
# Sanity Generation
|
| 74 |
-
# -------------------------------------------------
|
| 75 |
-
logs.append("🧠 Running sanity generation")
|
| 76 |
-
|
| 77 |
-
dispatch_for_generation(model)
|
| 78 |
-
|
| 79 |
-
inputs = processor(
|
| 80 |
-
text="Hello my name is",
|
| 81 |
-
return_tensors="pt"
|
| 82 |
-
).to(device)
|
| 83 |
-
|
| 84 |
-
with torch.no_grad():
|
| 85 |
-
output = model.generate(
|
| 86 |
-
**inputs,
|
| 87 |
-
max_new_tokens=int(max_new_tokens)
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
sample_text = processor.decode(
|
| 91 |
-
output[0],
|
| 92 |
-
skip_special_tokens=True
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
logs.append("✅ Generation successful")
|
| 96 |
-
logs.append(f"Sample Output:\n{sample_text}")
|
| 97 |
-
|
| 98 |
-
# -------------------------------------------------
|
| 99 |
-
# Save compressed model
|
| 100 |
-
# -------------------------------------------------
|
| 101 |
-
tmp_dir = tempfile.mkdtemp()
|
| 102 |
-
logs.append("💾 Saving compressed model")
|
| 103 |
-
|
| 104 |
-
model.save_pretrained(
|
| 105 |
-
tmp_dir,
|
| 106 |
-
save_compressed=True
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
processor.save_pretrained(tmp_dir)
|
| 110 |
-
|
| 111 |
-
# -------------------------------------------------
|
| 112 |
-
# Create repo + Upload
|
| 113 |
-
# -------------------------------------------------
|
| 114 |
-
logs.append("☁ Creating private repo if not exists")
|
| 115 |
-
|
| 116 |
-
create_repo(
|
| 117 |
-
repo_id=target_repo,
|
| 118 |
-
token=hf_token,
|
| 119 |
-
private=True,
|
| 120 |
-
exist_ok=True
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
logs.append("⬆ Uploading to Hugging Face")
|
| 124 |
-
|
| 125 |
-
upload_folder(
|
| 126 |
-
repo_id=target_repo,
|
| 127 |
-
folder_path=tmp_dir,
|
| 128 |
-
token=hf_token
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
shutil.rmtree(tmp_dir)
|
| 132 |
-
|
| 133 |
-
logs.append("🎉 Upload completed successfully!")
|
| 134 |
-
|
| 135 |
-
return "\n\n".join(logs)
|
| 136 |
-
|
| 137 |
-
except Exception as e:
|
| 138 |
-
return f"❌ Error: {str(e)}"
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
# -------------------------------------------------
|
| 142 |
-
# Gradio UI
|
| 143 |
-
# -------------------------------------------------
|
| 144 |
-
with gr.Blocks(title="Dots OCR FP8_DYNAMIC Uploader") as app:
|
| 145 |
-
|
| 146 |
-
gr.Markdown("## 🔥 Dots OCR 1.5 → FP8_DYNAMIC → Hugging Face Upload")
|
| 147 |
-
|
| 148 |
-
source_model = gr.Textbox(
|
| 149 |
-
label="Model Source (HF Path)",
|
| 150 |
-
value="rednote-hilab/dots.ocr-1.5"
|
| 151 |
-
)
|
| 152 |
-
|
| 153 |
-
target_repo = gr.Textbox(
|
| 154 |
-
label="Target Repo (username/repo-name)",
|
| 155 |
-
placeholder="e.g. yourname/dots-ocr-fp8-dynamic"
|
| 156 |
-
)
|
| 157 |
-
|
| 158 |
-
hf_token = gr.Textbox(
|
| 159 |
-
label="Hugging Face Write Token",
|
| 160 |
-
type="password"
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
max_new_tokens = gr.Number(
|
| 164 |
-
label="Sanity Generation Max New Tokens",
|
| 165 |
-
value=20
|
| 166 |
-
)
|
| 167 |
-
|
| 168 |
-
run_btn = gr.Button("🚀 Quantize FP8_DYNAMIC & Upload")
|
| 169 |
-
|
| 170 |
-
output = gr.Textbox(
|
| 171 |
-
label="Status Log",
|
| 172 |
-
lines=22
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
-
run_btn.click(
|
| 176 |
-
fn=fp8_dynamic_upload,
|
| 177 |
-
inputs=[
|
| 178 |
-
source_model,
|
| 179 |
-
target_repo,
|
| 180 |
-
hf_token,
|
| 181 |
-
max_new_tokens
|
| 182 |
-
],
|
| 183 |
-
outputs=output
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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