File size: 2,663 Bytes
567fe0b
2a8b471
2dfdea1
 
 
 
d053104
2dfdea1
 
d053104
2dfdea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa53876
2dfdea1
2bdaf86
2dfdea1
 
 
 
 
 
 
fa53876
2dfdea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be1973c
2dfdea1
 
 
ef65e58
2dfdea1
 
 
 
 
 
 
 
 
 
1b43832
26b2648
22d3ebc
3b30feb
2dfdea1
 
 
 
 
 
 
 
 
567fe0b
2a8b471
2dfdea1
 
b310409
 
567fe0b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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()