Update app.py
Browse files11 unmodified lines121314151617181928 unmodified lines4849505152535417 unmodified lines7273747576777831 unmodified lines1101111121131141151161171181191201211221231241251261271281295 unmodified lines1351361371381391401411421431441451461476 unmodified lines15415515615715815916016111 unmodified linesif not token:yield "β Error: Please provide a valid Hugging Face Write Token."returnif not target_repo.strip():yield "β Error: Please specify a Target Repository."return# Map precision string to PyTorch dtype28 unmodified linesyield f"β³ Processing {file}..."try:# Download file locally, bypassing symlink cache to save spacelocal_path = hf_hub_download(repo_id=source_repo,filename=file,17 unmodified linesconverted_path = os.path.join(TEMP_DIR, "converted.safetensors")save_file(tensors, converted_path)# Wipe tensors from RAMdel tensorsgc.collect()31 unmodified linesyield f"β All files processed and successfully uploaded to {target_repo}!"# Dynamic UI Update for Target Repo Namedef update_target_repo(source, precision):model_name = "Z-Image-Turbo" if "Turbo" in source else "Z-Image-Base"return f"rootlocalghost/{model_name}-{precision}"# Build the Gradio UIwith gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# π Z-Image Quantizer & Uploader")gr.Markdown("Select your source model and desired precision. The tool will sequentially download, quantize the ""**text_encoder** and **transformer** files, and upload everything to your target repository while keeping memory usage under 16GB.")with gr.Row():with gr.Column(scale=2):source_repo = gr.Dropdown(choices=["Tongyi-MAI/Z-Image", "Tongyi-MAI/Z-Image-Turbo"],value="Tongyi-MAI/Z-Image-Turbo",5 unmodified lineslabel="Quantization Precision")target_repo = gr.Textbox(label="Target Repository",value="rootlocalghost/Z-Image-Turbo-FP8")hf_token = gr.Textbox(label="Hugging Face Token (Write Access)",type="password",placeholder="hf_...")start_btn = gr.Button("Start Quantization & Upload", variant="primary")6 unmodified lines)# Automatically update the target repo name when inputs changesource_repo.change(fn=update_target_repo, inputs=[source_repo, precision], outputs=[target_repo])precision.change(fn=update_target_repo, inputs=[source_repo, precision], outputs=[target_repo])start_btn.click(fn=convert_and_upload,11 unmodified lines121314151617181928 unmodified lines4849505152535417 unmodified lines7273747576777831 unmodified lines1101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391405 unmodified lines1461471481491501511521531546 unmodified lines16116216316416516616716816917017117217311 unmodified linesif not token:yield "β Error: Please provide a valid Hugging Face Write Token."returnif not target_repo.strip() or "your-username" in target_repo:yield "β Error: Please specify a valid Target Repository (e.g., your-username/repo-name)."return# Map precision string to PyTorch dtype28 unmodified linesyield f"β³ Processing {file}..."try:# Download file locally, bypassing symlink cache to save disk spacelocal_path = hf_hub_download(repo_id=source_repo,filename=file,17 unmodified linesconverted_path = os.path.join(TEMP_DIR, "converted.safetensors")save_file(tensors, converted_path)# Wipe tensors from RAM to prevent OOMdel tensorsgc.collect()31 unmodified linesyield f"β All files processed and successfully uploaded to {target_repo}!"# Dynamic UI Update for Target Repo Namedef update_target_repo(username, source, precision):user_prefix = username.strip() if username.strip() else "your-username"model_name = "Z-Image-Turbo" if "Turbo" in source else "Z-Image-Base"return f"{user_prefix}/{model_name}-{precision}"# Build the Gradio UIwith gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown("# π Z-Image Quantizer & Uploader")gr.Markdown("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""**How it works:** This tool sequentially downloads, quantizes the **text_encoder** and **transformer** files, and uploads everything. ""It is designed to run safely on free Spaces (16GB RAM) by processing files one at a time.")with gr.Row():with gr.Column(scale=2):hf_token = gr.Textbox(label="Hugging Face Token (Write Access Required)",type="password",placeholder="hf_...")hf_username = gr.Textbox(label="Your Hugging Face Username",placeholder="e.g., rootlocalghost")source_repo = gr.Dropdown(choices=["Tongyi-MAI/Z-Image", "Tongyi-MAI/Z-Image-Turbo"],value="Tongyi-MAI/Z-Image-Turbo",5 unmodified lineslabel="Quantization Precision")target_repo = gr.Textbox(label="Target Repository (Auto-generated)",value="your-username/Z-Image-Turbo-FP8",interactive=True)start_btn = gr.Button("Start Quantization & Upload", variant="primary")6 unmodified lines)# Automatically update the target repo name when inputs changeinputs_to_watch = [hf_username, source_repo, precision]for inp in inputs_to_watch:inp.change(fn=update_target_repo,inputs=inputs_to_watch,outputs=[target_repo])start_btn.click(fn=convert_and_upload,
app.py+29-17