File size: 8,605 Bytes
93a4a97
7665754
 
 
 
 
 
 
93a4a97
7665754
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a66448
7665754
 
 
 
 
 
 
 
 
145fc8a
ba4f365
7665754
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dedc3b3
7665754
 
5989cf8
7665754
 
 
 
aaf4035
7665754
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b61eb9
d7195c9
 
 
2b61eb9
 
 
d7195c9
2b61eb9
 
 
 
 
 
d7195c9
 
2b61eb9
 
 
d7195c9
 
2b61eb9
 
d7195c9
 
2b61eb9
 
 
 
d7195c9
2b61eb9
 
 
 
 
79e5760
d7195c9
 
2b61eb9
7665754
d7195c9
 
 
 
 
 
 
2b61eb9
d7195c9
2b61eb9
 
 
d7195c9
 
2b61eb9
7665754
d7195c9
 
7665754
2b61eb9
 
 
d7195c9
 
2b61eb9
 
 
d7195c9
2b61eb9
 
 
7665754
d7195c9
 
 
7665754
d7195c9
 
 
 
 
 
 
 
 
 
 
7665754
d7195c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7665754
 
2b61eb9
 
d7195c9
2b61eb9
7665754
2b61eb9
 
 
 
 
7665754
 
 
 
 
 
 
 
 
 
 
 
 
2b61eb9
 
 
 
 
 
 
 
 
 
d7195c9
2b61eb9
 
 
 
 
 
 
7665754
 
 
 
 
 
d7195c9
2b61eb9
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import spaces
import gradio as gr
import torch
import os
import traceback
from diffusers import ZImagePipeline
from huggingface_hub import list_repo_files
from PIL import Image


# ============================================================
# CONFIG
# ============================================================

MODEL_ID = "Tongyi-MAI/Z-Image-Turbo"
DEFAULT_LORA_REPO = "rahul7star/ZImageLora"

DTYPE = torch.bfloat16
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

# ============================================================
# GLOBAL STATE
# ============================================================

pipe = None
CURRENT_LORA_REPO = None
CURRENT_LORA_FILE = None

# ============================================================
# LOGGING
# ============================================================

def log(msg):
    print(msg)
    return msg

# ============================================================
# PIPELINE BUILD (ONCE)
# ============================================================

try:
    pipe = ZImagePipeline.from_pretrained(
        MODEL_ID,
        torch_dtype=DTYPE,
    )

    

    pipe.to(DEVICE)
    log("✅ Pipeline built successfully")

except Exception as e:
    log("❌ Pipeline build failed")
    log(traceback.format_exc())
    pipe = None



# ============================================================
# HELPERS
# ============================================================

def list_loras_from_repo(repo_id: str):
    try:
        files = list_repo_files(repo_id)
        return [f for f in files if f.endswith(".safetensors")]
    except Exception as e:
        log(f"❌ Failed to list LoRAs: {e}")
        return []

# ============================================================
# IMAGE GENERATION (SAFE LORA LOGIC)
# ============================================================
@spaces.GPU()
def generate_image(prompt, height, width, steps, seed, guidance_scale):
    LOGS = []
    print(prompt)

    if pipe is None:
        return None, [], "❌ Pipeline not initialized"

    generator = torch.Generator().manual_seed(int(seed))
    placeholder = Image.new("RGB", (width, height), (255, 255, 255))
    previews = []

    # ---- Always start clean ----
    try:
        pipe.unload_lora_weights()
    except Exception:
        pass

    # ---- Load LoRA for this run only ----
    if CURRENT_LORA_FILE:
        try:
            pipe.load_lora_weights(
                CURRENT_LORA_REPO,
                weight_name=CURRENT_LORA_FILE
            )
            LOGS.append(f"🧩 LoRA loaded: {CURRENT_LORA_FILE}")
        except Exception as e:
            LOGS.append(f"❌ LoRA load failed: {e}")

    # ---- Preview steps (lightweight) ----
    try:
        num_previews = min(5, steps)
        for i in range(num_previews):
            out = pipe(
                prompt=prompt,
                height=height // 4,
                width=width // 4,
                num_inference_steps=i + 1,
                guidance_scale=guidance_scale,
                generator=generator,
            )
            img = out.images[0].resize((width, height))
            previews.append(img)
            yield None, previews, "\n".join(LOGS)
    except Exception as e:
        LOGS.append(f"⚠️ Preview failed: {e}")

    # ---- Final image ----
    try:
        out = pipe(
            prompt=prompt,
            height=height,
            width=width,
            num_inference_steps=steps,
            guidance_scale=guidance_scale,
            generator=generator,
        )
        final_img = out.images[0]
        previews.append(final_img)
        LOGS.append("✅ Image generated")

        yield final_img, previews, "\n".join(LOGS)

    except Exception as e:
        LOGS.append(f"❌ Generation failed: {e}")
        yield placeholder, previews, "\n".join(LOGS)

    finally:
        # ---- CRITICAL: unload after run ----
        try:
            pipe.unload_lora_weights()
            LOGS.append("🧹 LoRA unloaded")
        except Exception:
            pass

# ============================================================
# GRADIO UI
# ============================================================
css = """
.gradio-container {
    max-width: 100% !important;
    padding: 16px 32px !important;
}

.section {
    margin-bottom: 12px;
}

.generate-btn {
    background: linear-gradient(90deg, #4b6cb7, #182848) !important;
    color: white !important;
    font-weight: 600;
    height: 46px;
    border-radius: 10px;
}

.secondary-btn {
    height: 42px;
    border-radius: 10px;
}

textarea, input {
    border-radius: 10px !important;
}
"""

with gr.Blocks(
    title="Z-Image-Turbo (Runtime LoRA)",
    css=css,
) as demo:

    gr.Markdown(
        """
        # 🎨 Z-Image-Turbo LORA
        **Runtime LoRA · Safe Mode · Full-Width UI**
        """
    )

    # ======================================================
    # MAIN LAYOUT
    # ======================================================
    with gr.Row():

        # ================= LEFT PANEL =================
        with gr.Column(scale=5):

            # -------- Prompt --------
            prompt = gr.Textbox(
                label="Prompt",
                value="boat in ocean",
                lines=4,
                placeholder="Describe the image you want to generate…",
            )

            # -------- LoRA Controls (NEXT TO PROMPT) --------
            gr.Markdown("### 🧩 LoRA Controls")

            lora_repo = gr.Textbox(
                label="LoRA Repository",
                value=DEFAULT_LORA_REPO,
                lines=2,
                placeholder="username/repo (e.g. rahul7star/ZImageLora)",
            )

            lora_dropdown = gr.Dropdown(
                label="LoRA File",
                choices=[],
                interactive=True,
            )

            with gr.Row():
                refresh_btn = gr.Button("🔄 Refresh LoRA List", elem_classes="secondary-btn")
                clear_lora_btn = gr.Button("❌ Clear LoRA", elem_classes="secondary-btn")

            # -------- Generation Controls --------
            gr.Markdown("### ⚙️ Generation Settings")

            with gr.Row():
                width = gr.Slider(256, 2048, value=1024, step=8, label="Width")
                height = gr.Slider(256, 2048, value=1024, step=8, label="Height")

            with gr.Row():
                steps = gr.Slider(1, 50, value=20, step=1, label="Steps")
                guidance = gr.Slider(0, 10, value=0.0, step=0.5, label="Guidance")
                seed = gr.Number(value=42, label="Seed", precision=0)

            run_btn = gr.Button("🚀 Generate Image", elem_classes="generate-btn")

            logs_box = gr.Textbox(
                label="Logs",
                lines=10,
                interactive=False,
            )

        # ================= RIGHT PANEL =================
        with gr.Column(scale=7):

            final_image = gr.Image(
                label="Final Image",
                height=520,
            )

            gallery = gr.Gallery(
                label="Generation Steps",
                columns=4,
                height=260,
            )

    # ======================================================
    # CALLBACKS
    # ======================================================
    def refresh_loras(repo):
        files = list_loras_from_repo(repo)
        return gr.update(
            choices=files,
            value=files[0] if files else None,
        )

    refresh_btn.click(
        refresh_loras,
        inputs=[lora_repo],
        outputs=[lora_dropdown],
    )

    def select_lora(lora_file, repo):
        global CURRENT_LORA_FILE, CURRENT_LORA_REPO
        CURRENT_LORA_FILE = lora_file
        CURRENT_LORA_REPO = repo
        return f"🧩 Selected LoRA: {lora_file}"

    lora_dropdown.change(
        select_lora,
        inputs=[lora_dropdown, lora_repo],
        outputs=[logs_box],
    )

    def clear_lora():
        global CURRENT_LORA_FILE, CURRENT_LORA_REPO
        CURRENT_LORA_FILE = None
        CURRENT_LORA_REPO = None
        try:
            pipe.unload_lora_weights()
        except Exception:
            pass
        return (
            gr.update(value=None),
            "🧹 LoRA cleared — base model will be used."
        )

    clear_lora_btn.click(
        clear_lora,
        outputs=[lora_dropdown, logs_box],
    )

    run_btn.click(
        generate_image,
        inputs=[prompt, height, width, steps, seed, guidance],
        outputs=[final_image, gallery, logs_box],
    )



demo.launch()