Spaces:
Running on Zero
Running on Zero
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,182 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import spaces
|
| 3 |
-
|
| 4 |
-
from transformers import Qwen3_5ForConditionalGeneration, AutoProcessor
|
| 5 |
-
from huggingface_hub import create_repo, upload_large_folder, login
|
| 6 |
-
import os
|
| 7 |
-
import gradio as gr
|
| 8 |
-
|
| 9 |
-
@spaces.GPU(duration=900)
|
| 10 |
-
def load_and_reupload_model(model_name, new_repo_id, hf_token, max_shard_size="4.4GB"):
|
| 11 |
-
|
| 12 |
-
log_output = []
|
| 13 |
-
|
| 14 |
-
try:
|
| 15 |
-
|
| 16 |
-
# -------------------------
|
| 17 |
-
# Validate inputs
|
| 18 |
-
# -------------------------
|
| 19 |
-
if not model_name or not new_repo_id or not hf_token:
|
| 20 |
-
return "❌ Model name, repo id, and token are required."
|
| 21 |
-
|
| 22 |
-
# -------------------------
|
| 23 |
-
# Login to HuggingFace
|
| 24 |
-
# -------------------------
|
| 25 |
-
login(token=hf_token)
|
| 26 |
-
log_output.append("✅ Hugging Face login successful")
|
| 27 |
-
|
| 28 |
-
# -------------------------
|
| 29 |
-
# Create repository
|
| 30 |
-
# -------------------------
|
| 31 |
-
create_repo(
|
| 32 |
-
repo_id=new_repo_id,
|
| 33 |
-
private=True,
|
| 34 |
-
exist_ok=True
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
log_output.append(f"✅ Repo ready: {new_repo_id}")
|
| 38 |
-
|
| 39 |
-
# -------------------------
|
| 40 |
-
# Load processor
|
| 41 |
-
# -------------------------
|
| 42 |
-
log_output.append(f"🔄 Loading processor: {model_name}")
|
| 43 |
-
|
| 44 |
-
processor = AutoProcessor.from_pretrained(
|
| 45 |
-
model_name,
|
| 46 |
-
trust_remote_code=True
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
log_output.append("✅ Processor loaded")
|
| 50 |
-
|
| 51 |
-
# -------------------------
|
| 52 |
-
# Load model
|
| 53 |
-
# -------------------------
|
| 54 |
-
log_output.append(f"🔄 Loading model: {model_name}")
|
| 55 |
-
|
| 56 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 57 |
-
|
| 58 |
-
model = Qwen3_5ForConditionalGeneration.from_pretrained(
|
| 59 |
-
model_name,
|
| 60 |
-
dtype="auto",
|
| 61 |
-
device_map="auto",
|
| 62 |
-
trust_remote_code=True,
|
| 63 |
-
use_safetensors=True
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
model.eval()
|
| 67 |
-
|
| 68 |
-
log_output.append(f"✅ Model loaded on {device}")
|
| 69 |
-
|
| 70 |
-
# -------------------------
|
| 71 |
-
# Save locally
|
| 72 |
-
# -------------------------
|
| 73 |
-
local_dir = new_repo_id.split("/")[-1]
|
| 74 |
-
|
| 75 |
-
os.makedirs(local_dir, exist_ok=True)
|
| 76 |
-
|
| 77 |
-
log_output.append(
|
| 78 |
-
f"🔄 Saving model shards (max_shard_size={max_shard_size})"
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
model.save_pretrained(
|
| 82 |
-
local_dir,
|
| 83 |
-
max_shard_size=max_shard_size
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
processor.save_pretrained(local_dir)
|
| 87 |
-
|
| 88 |
-
log_output.append("✅ Model + processor saved locally")
|
| 89 |
-
|
| 90 |
-
# -------------------------
|
| 91 |
-
# Upload using upload_large_folder
|
| 92 |
-
# -------------------------
|
| 93 |
-
log_output.append("🔄 Uploading model to HuggingFace...")
|
| 94 |
-
|
| 95 |
-
upload_large_folder(
|
| 96 |
-
repo_id=new_repo_id,
|
| 97 |
-
repo_type="model",
|
| 98 |
-
folder_path=local_dir,
|
| 99 |
-
revision="main"
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
log_output.append("🚀 Upload completed successfully!")
|
| 103 |
-
|
| 104 |
-
except Exception as e:
|
| 105 |
-
|
| 106 |
-
log_output.append(f"❌ Error: {str(e)}")
|
| 107 |
-
|
| 108 |
-
return "\n".join(log_output)
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
# =====================================================
|
| 112 |
-
# Gradio UI
|
| 113 |
-
# =====================================================
|
| 114 |
-
|
| 115 |
-
with gr.Blocks(theme="soft") as demo:
|
| 116 |
-
|
| 117 |
-
gr.Markdown(
|
| 118 |
-
"""
|
| 119 |
-
# 🚀 Qwen3-VL Model Sharder & Re-Uploader
|
| 120 |
-
|
| 121 |
-
This tool will:
|
| 122 |
-
|
| 123 |
-
1️⃣ Download a **Qwen3-VL model**
|
| 124 |
-
2️⃣ Save it locally with **smaller shards**
|
| 125 |
-
3️⃣ Upload it to a **private Hugging Face repository**
|
| 126 |
-
|
| 127 |
-
Uses **upload_large_folder()** for reliable large uploads.
|
| 128 |
-
"""
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
with gr.Row():
|
| 132 |
-
|
| 133 |
-
with gr.Column(scale=2):
|
| 134 |
-
|
| 135 |
-
model_name = gr.Textbox(
|
| 136 |
-
label="Original Model Name",
|
| 137 |
-
value="Qwen/Qwen3-VL-2B-Instruct"
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
new_repo_id = gr.Textbox(
|
| 141 |
-
label="New Repository ID",
|
| 142 |
-
placeholder="username/my-private-qwen3vl"
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
hf_token = gr.Textbox(
|
| 146 |
-
label="HuggingFace Write Token",
|
| 147 |
-
type="password",
|
| 148 |
-
placeholder="hf_xxxxxxxxx"
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
max_shard_size = gr.Textbox(
|
| 152 |
-
label="Max Shard Size",
|
| 153 |
-
value="4.4GB"
|
| 154 |
-
)
|
| 155 |
-
|
| 156 |
-
run_btn = gr.Button(
|
| 157 |
-
"🚀 Shard & Upload Model",
|
| 158 |
-
variant="primary"
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
with gr.Column(scale=3):
|
| 162 |
-
|
| 163 |
-
logs = gr.Textbox(
|
| 164 |
-
label="Process Logs",
|
| 165 |
-
lines=20,
|
| 166 |
-
interactive=False,
|
| 167 |
-
autoscroll=True
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
run_btn.click(
|
| 171 |
-
fn=load_and_reupload_model,
|
| 172 |
-
inputs=[model_name, new_repo_id, hf_token, max_shard_size],
|
| 173 |
-
outputs=logs
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
# =====================================================
|
| 178 |
-
# Launch
|
| 179 |
-
# =====================================================
|
| 180 |
-
|
| 181 |
-
if __name__ == "__main__":
|
| 182 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|