Spaces:
Running
on
Zero
Running
on
Zero
Update app_lora1.py
Browse files- app_lora1.py +194 -20
app_lora1.py
CHANGED
|
@@ -146,40 +146,142 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale):
|
|
| 146 |
# ============================================================
|
| 147 |
# GRADIO UI
|
| 148 |
# ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
with gr.
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
width = gr.Slider(256, 2048, value=1024, step=8, label="Width")
|
|
|
|
|
|
|
|
|
|
| 158 |
steps = gr.Slider(1, 50, value=20, step=1, label="Inference Steps")
|
| 159 |
guidance = gr.Slider(0, 10, value=0.0, step=0.5, label="Guidance Scale")
|
| 160 |
-
seed = gr.Number(value=42, label="Seed")
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
| 166 |
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
-
#
|
|
|
|
|
|
|
| 177 |
|
| 178 |
def refresh_loras(repo):
|
| 179 |
files = list_loras_from_repo(repo)
|
| 180 |
-
return gr.update(
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
-
refresh_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
def select_lora(lora_file, repo):
|
| 185 |
global CURRENT_LORA_FILE, CURRENT_LORA_REPO
|
|
@@ -193,6 +295,24 @@ with gr.Blocks(title="Z-Image-Turbo (LoRA Safe)") as demo:
|
|
| 193 |
outputs=[logs_box],
|
| 194 |
)
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
run_btn.click(
|
| 197 |
generate_image,
|
| 198 |
inputs=[prompt, height, width, steps, seed, guidance],
|
|
@@ -200,3 +320,57 @@ with gr.Blocks(title="Z-Image-Turbo (LoRA Safe)") as demo:
|
|
| 200 |
)
|
| 201 |
|
| 202 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
# ============================================================
|
| 147 |
# GRADIO UI
|
| 148 |
# ============================================================
|
| 149 |
+
css = """
|
| 150 |
+
#container {
|
| 151 |
+
max-width: 1200px;
|
| 152 |
+
margin: auto;
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
.section {
|
| 156 |
+
margin-top: 16px;
|
| 157 |
+
margin-bottom: 8px;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
.generate-btn {
|
| 161 |
+
background: linear-gradient(90deg, #4b6cb7, #182848) !important;
|
| 162 |
+
color: white !important;
|
| 163 |
+
border-radius: 8px !important;
|
| 164 |
+
font-weight: 600;
|
| 165 |
+
height: 44px;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
.secondary-btn {
|
| 169 |
+
border-radius: 8px !important;
|
| 170 |
+
height: 44px;
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
input, textarea {
|
| 174 |
+
border-radius: 8px !important;
|
| 175 |
+
}
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
with gr.Blocks(
|
| 179 |
+
title="Z-Image-Turbo (LoRA Safe)",
|
| 180 |
+
css=css,
|
| 181 |
+
) as demo:
|
| 182 |
+
|
| 183 |
+
gr.Markdown(
|
| 184 |
+
"""
|
| 185 |
+
# 🎨 Z-Image-Turbo
|
| 186 |
+
**Runtime LoRA · Safe Mode · Production UI**
|
| 187 |
+
""",
|
| 188 |
+
elem_classes="section"
|
| 189 |
+
)
|
| 190 |
|
| 191 |
+
with gr.Column(elem_id="container"):
|
| 192 |
+
|
| 193 |
+
# ===============================
|
| 194 |
+
# PROMPT
|
| 195 |
+
# ===============================
|
| 196 |
+
with gr.Column(elem_classes="section"):
|
| 197 |
+
prompt = gr.Textbox(
|
| 198 |
+
label="Prompt",
|
| 199 |
+
value="boat in ocean",
|
| 200 |
+
lines=3,
|
| 201 |
+
placeholder="Describe what you want to generate…",
|
| 202 |
+
)
|
| 203 |
|
| 204 |
+
# ===============================
|
| 205 |
+
# PARAMETERS
|
| 206 |
+
# ===============================
|
| 207 |
+
with gr.Row(elem_classes="section"):
|
| 208 |
width = gr.Slider(256, 2048, value=1024, step=8, label="Width")
|
| 209 |
+
height = gr.Slider(256, 2048, value=1024, step=8, label="Height")
|
| 210 |
+
|
| 211 |
+
with gr.Row():
|
| 212 |
steps = gr.Slider(1, 50, value=20, step=1, label="Inference Steps")
|
| 213 |
guidance = gr.Slider(0, 10, value=0.0, step=0.5, label="Guidance Scale")
|
| 214 |
+
seed = gr.Number(value=42, label="Seed", precision=0)
|
| 215 |
+
|
| 216 |
+
# ===============================
|
| 217 |
+
# ACTION BUTTON
|
| 218 |
+
# ===============================
|
| 219 |
+
with gr.Row(elem_classes="section"):
|
| 220 |
+
run_btn = gr.Button("🚀 Generate Image", elem_classes="generate-btn")
|
| 221 |
+
|
| 222 |
+
# ===============================
|
| 223 |
+
# OUTPUT
|
| 224 |
+
# ===============================
|
| 225 |
+
with gr.Row(elem_classes="section"):
|
| 226 |
+
final_image = gr.Image(
|
| 227 |
+
label="Final Image",
|
| 228 |
+
height=420,
|
| 229 |
+
)
|
| 230 |
|
| 231 |
+
gallery = gr.Gallery(
|
| 232 |
+
label="Generation Steps",
|
| 233 |
+
columns=4,
|
| 234 |
+
height=260,
|
| 235 |
+
)
|
| 236 |
|
| 237 |
+
# ===============================
|
| 238 |
+
# LORA CONTROLS
|
| 239 |
+
# ===============================
|
| 240 |
+
gr.Markdown("## 🧩 LoRA Controls", elem_classes="section")
|
| 241 |
|
| 242 |
+
with gr.Row():
|
| 243 |
+
lora_repo = gr.Textbox(
|
| 244 |
+
label="LoRA Repository",
|
| 245 |
+
value=DEFAULT_LORA_REPO,
|
| 246 |
+
placeholder="username/repo",
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
with gr.Row():
|
| 250 |
+
lora_dropdown = gr.Dropdown(
|
| 251 |
+
label="Available LoRA Files",
|
| 252 |
+
choices=[],
|
| 253 |
+
interactive=True,
|
| 254 |
+
)
|
| 255 |
|
| 256 |
+
with gr.Row():
|
| 257 |
+
refresh_btn = gr.Button("🔄 Refresh LoRA List", elem_classes="secondary-btn")
|
| 258 |
+
clear_lora_btn = gr.Button("❌ Clear LoRA", elem_classes="secondary-btn")
|
| 259 |
+
|
| 260 |
+
# ===============================
|
| 261 |
+
# LOGS
|
| 262 |
+
# ===============================
|
| 263 |
+
logs_box = gr.Textbox(
|
| 264 |
+
label="Logs",
|
| 265 |
+
lines=14,
|
| 266 |
+
interactive=False,
|
| 267 |
+
)
|
| 268 |
|
| 269 |
+
# ==================================================
|
| 270 |
+
# CALLBACKS
|
| 271 |
+
# ==================================================
|
| 272 |
|
| 273 |
def refresh_loras(repo):
|
| 274 |
files = list_loras_from_repo(repo)
|
| 275 |
+
return gr.update(
|
| 276 |
+
choices=files,
|
| 277 |
+
value=files[0] if files else None
|
| 278 |
+
)
|
| 279 |
|
| 280 |
+
refresh_btn.click(
|
| 281 |
+
refresh_loras,
|
| 282 |
+
inputs=[lora_repo],
|
| 283 |
+
outputs=[lora_dropdown],
|
| 284 |
+
)
|
| 285 |
|
| 286 |
def select_lora(lora_file, repo):
|
| 287 |
global CURRENT_LORA_FILE, CURRENT_LORA_REPO
|
|
|
|
| 295 |
outputs=[logs_box],
|
| 296 |
)
|
| 297 |
|
| 298 |
+
def clear_lora():
|
| 299 |
+
global CURRENT_LORA_FILE, CURRENT_LORA_REPO
|
| 300 |
+
CURRENT_LORA_FILE = None
|
| 301 |
+
CURRENT_LORA_REPO = None
|
| 302 |
+
try:
|
| 303 |
+
pipe.unload_lora_weights()
|
| 304 |
+
except Exception:
|
| 305 |
+
pass
|
| 306 |
+
return (
|
| 307 |
+
gr.update(value=None),
|
| 308 |
+
"🧹 LoRA cleared. Next generation will be base model only."
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
clear_lora_btn.click(
|
| 312 |
+
clear_lora,
|
| 313 |
+
outputs=[lora_dropdown, logs_box],
|
| 314 |
+
)
|
| 315 |
+
|
| 316 |
run_btn.click(
|
| 317 |
generate_image,
|
| 318 |
inputs=[prompt, height, width, steps, seed, guidance],
|
|
|
|
| 320 |
)
|
| 321 |
|
| 322 |
demo.launch()
|
| 323 |
+
|
| 324 |
+
# with gr.Blocks(title="Z-Image-Turbo (LoRA Safe)") as demo:
|
| 325 |
+
# gr.Markdown("# 🎨 Z-Image-Turbo — Runtime LoRA (SAFE MODE)")
|
| 326 |
+
|
| 327 |
+
# with gr.Row():
|
| 328 |
+
# with gr.Column(scale=1):
|
| 329 |
+
# prompt = gr.Textbox(label="Prompt", value="boat in ocean")
|
| 330 |
+
# height = gr.Slider(256, 2048, value=1024, step=8, label="Height")
|
| 331 |
+
# width = gr.Slider(256, 2048, value=1024, step=8, label="Width")
|
| 332 |
+
# steps = gr.Slider(1, 50, value=20, step=1, label="Inference Steps")
|
| 333 |
+
# guidance = gr.Slider(0, 10, value=0.0, step=0.5, label="Guidance Scale")
|
| 334 |
+
# seed = gr.Number(value=42, label="Seed")
|
| 335 |
+
# run_btn = gr.Button("🚀 Generate")
|
| 336 |
+
|
| 337 |
+
# with gr.Column(scale=1):
|
| 338 |
+
# final_image = gr.Image(label="Final Image")
|
| 339 |
+
# gallery = gr.Gallery(label="Steps", columns=4, height=256)
|
| 340 |
+
|
| 341 |
+
# gr.Markdown("## 🧩 LoRA Controls")
|
| 342 |
+
|
| 343 |
+
# with gr.Row():
|
| 344 |
+
# lora_repo = gr.Textbox(label="LoRA Repo", value=DEFAULT_LORA_REPO)
|
| 345 |
+
# lora_dropdown = gr.Dropdown(label="LoRA File", choices=[])
|
| 346 |
+
# refresh_btn = gr.Button("🔄 Refresh")
|
| 347 |
+
|
| 348 |
+
# logs_box = gr.Textbox(label="Logs", lines=18)
|
| 349 |
+
|
| 350 |
+
# # ---- Callbacks ----
|
| 351 |
+
|
| 352 |
+
# def refresh_loras(repo):
|
| 353 |
+
# files = list_loras_from_repo(repo)
|
| 354 |
+
# return gr.update(choices=files, value=files[0] if files else None)
|
| 355 |
+
|
| 356 |
+
# refresh_btn.click(refresh_loras, inputs=[lora_repo], outputs=[lora_dropdown])
|
| 357 |
+
|
| 358 |
+
# def select_lora(lora_file, repo):
|
| 359 |
+
# global CURRENT_LORA_FILE, CURRENT_LORA_REPO
|
| 360 |
+
# CURRENT_LORA_FILE = lora_file
|
| 361 |
+
# CURRENT_LORA_REPO = repo
|
| 362 |
+
# return f"🧩 Selected LoRA: {lora_file}"
|
| 363 |
+
|
| 364 |
+
# lora_dropdown.change(
|
| 365 |
+
# select_lora,
|
| 366 |
+
# inputs=[lora_dropdown, lora_repo],
|
| 367 |
+
# outputs=[logs_box],
|
| 368 |
+
# )
|
| 369 |
+
|
| 370 |
+
# run_btn.click(
|
| 371 |
+
# generate_image,
|
| 372 |
+
# inputs=[prompt, height, width, steps, seed, guidance],
|
| 373 |
+
# outputs=[final_image, gallery, logs_box],
|
| 374 |
+
# )
|
| 375 |
+
|
| 376 |
+
demo.launch()
|