Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,8 @@ def convert_and_upload(token, source_repo, target_repo, precision):
|
|
| 12 |
if not token:
|
| 13 |
yield "β Error: Please provide a valid Hugging Face Write Token."
|
| 14 |
return
|
| 15 |
-
if not target_repo.strip():
|
| 16 |
-
yield "β Error: Please specify a Target Repository."
|
| 17 |
return
|
| 18 |
|
| 19 |
# Map precision string to PyTorch dtype
|
|
@@ -48,7 +48,7 @@ def convert_and_upload(token, source_repo, target_repo, precision):
|
|
| 48 |
yield f"β³ Processing {file}..."
|
| 49 |
|
| 50 |
try:
|
| 51 |
-
# Download file locally, bypassing symlink cache to save space
|
| 52 |
local_path = hf_hub_download(
|
| 53 |
repo_id=source_repo,
|
| 54 |
filename=file,
|
|
@@ -72,7 +72,7 @@ def convert_and_upload(token, source_repo, target_repo, precision):
|
|
| 72 |
converted_path = os.path.join(TEMP_DIR, "converted.safetensors")
|
| 73 |
save_file(tensors, converted_path)
|
| 74 |
|
| 75 |
-
# Wipe tensors from RAM
|
| 76 |
del tensors
|
| 77 |
gc.collect()
|
| 78 |
|
|
@@ -110,20 +110,31 @@ def convert_and_upload(token, source_repo, target_repo, precision):
|
|
| 110 |
yield f"β
All files processed and successfully uploaded to {target_repo}!"
|
| 111 |
|
| 112 |
# Dynamic UI Update for Target Repo Name
|
| 113 |
-
def update_target_repo(source, precision):
|
|
|
|
| 114 |
model_name = "Z-Image-Turbo" if "Turbo" in source else "Z-Image-Base"
|
| 115 |
-
return f"
|
| 116 |
|
| 117 |
# Build the Gradio UI
|
| 118 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 119 |
gr.Markdown("# π Z-Image Quantizer & Uploader")
|
| 120 |
gr.Markdown(
|
| 121 |
-
"
|
| 122 |
-
"**text_encoder** and **transformer** files, and
|
|
|
|
| 123 |
)
|
| 124 |
|
| 125 |
with gr.Row():
|
| 126 |
with gr.Column(scale=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
source_repo = gr.Dropdown(
|
| 128 |
choices=["Tongyi-MAI/Z-Image", "Tongyi-MAI/Z-Image-Turbo"],
|
| 129 |
value="Tongyi-MAI/Z-Image-Turbo",
|
|
@@ -135,13 +146,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 135 |
label="Quantization Precision"
|
| 136 |
)
|
| 137 |
target_repo = gr.Textbox(
|
| 138 |
-
label="Target Repository",
|
| 139 |
-
value="
|
| 140 |
-
|
| 141 |
-
hf_token = gr.Textbox(
|
| 142 |
-
label="Hugging Face Token (Write Access)",
|
| 143 |
-
type="password",
|
| 144 |
-
placeholder="hf_..."
|
| 145 |
)
|
| 146 |
start_btn = gr.Button("Start Quantization & Upload", variant="primary")
|
| 147 |
|
|
@@ -154,8 +161,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 154 |
)
|
| 155 |
|
| 156 |
# Automatically update the target repo name when inputs change
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
start_btn.click(
|
| 161 |
fn=convert_and_upload,
|
|
|
|
| 12 |
if not token:
|
| 13 |
yield "β Error: Please provide a valid Hugging Face Write Token."
|
| 14 |
return
|
| 15 |
+
if not target_repo.strip() or "your-username" in target_repo:
|
| 16 |
+
yield "β Error: Please specify a valid Target Repository (e.g., your-username/repo-name)."
|
| 17 |
return
|
| 18 |
|
| 19 |
# Map precision string to PyTorch dtype
|
|
|
|
| 48 |
yield f"β³ Processing {file}..."
|
| 49 |
|
| 50 |
try:
|
| 51 |
+
# Download file locally, bypassing symlink cache to save disk space
|
| 52 |
local_path = hf_hub_download(
|
| 53 |
repo_id=source_repo,
|
| 54 |
filename=file,
|
|
|
|
| 72 |
converted_path = os.path.join(TEMP_DIR, "converted.safetensors")
|
| 73 |
save_file(tensors, converted_path)
|
| 74 |
|
| 75 |
+
# Wipe tensors from RAM to prevent OOM
|
| 76 |
del tensors
|
| 77 |
gc.collect()
|
| 78 |
|
|
|
|
| 110 |
yield f"β
All files processed and successfully uploaded to {target_repo}!"
|
| 111 |
|
| 112 |
# Dynamic UI Update for Target Repo Name
|
| 113 |
+
def update_target_repo(username, source, precision):
|
| 114 |
+
user_prefix = username.strip() if username.strip() else "your-username"
|
| 115 |
model_name = "Z-Image-Turbo" if "Turbo" in source else "Z-Image-Base"
|
| 116 |
+
return f"{user_prefix}/{model_name}-{precision}"
|
| 117 |
|
| 118 |
# Build the Gradio UI
|
| 119 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 120 |
gr.Markdown("# π Z-Image Quantizer & Uploader")
|
| 121 |
gr.Markdown(
|
| 122 |
+
"Convert the **Z-Image** or **Z-Image-Turbo** models to lower precisions (FP8, FP16, BF16) and push them directly to your own Hugging Face account.\n\n"
|
| 123 |
+
"**How it works:** This tool sequentially downloads, quantizes the **text_encoder** and **transformer** files, and uploads everything. "
|
| 124 |
+
"It is designed to run safely on free Spaces (16GB RAM) by processing files one at a time."
|
| 125 |
)
|
| 126 |
|
| 127 |
with gr.Row():
|
| 128 |
with gr.Column(scale=2):
|
| 129 |
+
hf_token = gr.Textbox(
|
| 130 |
+
label="Hugging Face Token (Write Access Required)",
|
| 131 |
+
type="password",
|
| 132 |
+
placeholder="hf_..."
|
| 133 |
+
)
|
| 134 |
+
hf_username = gr.Textbox(
|
| 135 |
+
label="Your Hugging Face Username",
|
| 136 |
+
placeholder="e.g., rootlocalghost"
|
| 137 |
+
)
|
| 138 |
source_repo = gr.Dropdown(
|
| 139 |
choices=["Tongyi-MAI/Z-Image", "Tongyi-MAI/Z-Image-Turbo"],
|
| 140 |
value="Tongyi-MAI/Z-Image-Turbo",
|
|
|
|
| 146 |
label="Quantization Precision"
|
| 147 |
)
|
| 148 |
target_repo = gr.Textbox(
|
| 149 |
+
label="Target Repository (Auto-generated)",
|
| 150 |
+
value="your-username/Z-Image-Turbo-FP8",
|
| 151 |
+
interactive=True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
)
|
| 153 |
start_btn = gr.Button("Start Quantization & Upload", variant="primary")
|
| 154 |
|
|
|
|
| 161 |
)
|
| 162 |
|
| 163 |
# Automatically update the target repo name when inputs change
|
| 164 |
+
inputs_to_watch = [hf_username, source_repo, precision]
|
| 165 |
+
for inp in inputs_to_watch:
|
| 166 |
+
inp.change(
|
| 167 |
+
fn=update_target_repo,
|
| 168 |
+
inputs=inputs_to_watch,
|
| 169 |
+
outputs=[target_repo]
|
| 170 |
+
)
|
| 171 |
|
| 172 |
start_btn.click(
|
| 173 |
fn=convert_and_upload,
|