Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import os | |
| import subprocess | |
| import zipfile | |
| import requests | |
| from huggingface_hub import hf_hub_download, login | |
| hf_token = os.getenv("HF_TOKEN") | |
| login(token=hf_token) | |
| BASE_DIR = os.getcwd() | |
| SDCPP_DIR = os.path.join(BASE_DIR, "sdcpp") | |
| # ========================= | |
| # 1. Ψ―Ψ§ΩΩΩΨ― stable-diffusion.cpp | |
| # ========================= | |
| def setup_sdcpp(): | |
| if not os.path.exists(SDCPP_DIR): | |
| os.makedirs(SDCPP_DIR, exist_ok=True) | |
| zip_url = "https://github.com/leejet/stable-diffusion.cpp/releases/download/master-586-c97702e/sd-master-c97702e-bin-Linux-Ubuntu-24.04-x86_64.zip" | |
| zip_path = os.path.join(BASE_DIR, "sdcpp.zip") | |
| print("Downloading stable-diffusion.cpp...") | |
| r = requests.get(zip_url) | |
| with open(zip_path, "wb") as f: | |
| f.write(r.content) | |
| print("Extracting...") | |
| with zipfile.ZipFile(zip_path, 'r') as zip_ref: | |
| zip_ref.extractall(SDCPP_DIR) | |
| os.remove(zip_path) | |
| # chmod | |
| subprocess.run(["chmod", "+x", f"{SDCPP_DIR}/sd-cli"]) | |
| # ========================= | |
| # 2. Ψ―Ψ§ΩΩΩΨ― Ω Ψ―ΩβΩΨ§ | |
| # ========================= | |
| def setup_models(): | |
| print("Downloading models...") | |
| model_path = hf_hub_download( | |
| repo_id="unsloth/Z-Image-Turbo-GGUF", | |
| filename="z-image-turbo-Q3_K_M.gguf" | |
| ) | |
| vae_path = hf_hub_download( | |
| repo_id="black-forest-labs/FLUX.1-schnell", | |
| filename="ae.safetensors" | |
| ) | |
| llm_path = hf_hub_download( | |
| repo_id="unsloth/Qwen3-4B-Instruct-2507-GGUF", | |
| filename="Qwen3-4B-Instruct-2507-Q3_K_M.gguf" | |
| ) | |
| return model_path, vae_path, llm_path | |
| MODEL_PATH, VAE_PATH, LLM_PATH = None, None, None | |
| # ========================= | |
| # 3. inference | |
| # ========================= | |
| def generate(prompt): | |
| output_path = os.path.join(BASE_DIR, "output.png") | |
| cmd = [ | |
| f"{SDCPP_DIR}/sd-cli", | |
| "--diffusion-model", MODEL_PATH, | |
| "--vae", VAE_PATH, | |
| "--llm", LLM_PATH, | |
| "-p", prompt, | |
| "--steps", "2", | |
| "--cfg-scale", "1.0", | |
| "-o", output_path | |
| ] | |
| env = os.environ.copy() | |
| env["LD_LIBRARY_PATH"] = SDCPP_DIR | |
| print("Running:", " ".join(cmd)) | |
| subprocess.run(cmd, env=env, check=True) | |
| return output_path | |
| # ========================= | |
| # init | |
| # ========================= | |
| setup_sdcpp() | |
| MODEL_PATH, VAE_PATH, LLM_PATH = setup_models() | |
| # ========================= | |
| # UI | |
| # ========================= | |
| demo = gr.Interface( | |
| fn=generate, | |
| inputs=gr.Textbox(label="Prompt"), | |
| outputs=gr.Image(label="Generated Image"), | |
| title="Z-Image Turbo GGUF (CPU)", | |
| queue=True, | |
| ) | |
| demo.launch() |