Spaces:
Running
Running
Update app_lora1.py
Browse files- app_lora1.py +80 -69
app_lora1.py
CHANGED
|
@@ -2,7 +2,6 @@ import spaces
|
|
| 2 |
import os
|
| 3 |
import io
|
| 4 |
import sys
|
| 5 |
-
import json
|
| 6 |
import torch
|
| 7 |
|
| 8 |
import gradio as gr
|
|
@@ -21,7 +20,8 @@ MODEL_ID = "Tongyi-MAI/Z-Image-Turbo"
|
|
| 21 |
|
| 22 |
os.makedirs(LOCAL_SCRIPTS_DIR, exist_ok=True)
|
| 23 |
|
| 24 |
-
|
|
|
|
| 25 |
log_buffer = io.StringIO()
|
| 26 |
|
| 27 |
|
|
@@ -38,7 +38,6 @@ def pipeline_debug_info(pipe):
|
|
| 38 |
Pipeline Info
|
| 39 |
-------------
|
| 40 |
Device: {pipe.device}
|
| 41 |
-
DType: {pipe.unet.dtype if hasattr(pipe, "unet") else "n/a"}
|
| 42 |
Transformer: {pipe.transformer.__class__.__name__}
|
| 43 |
VAE: {pipe.vae.__class__.__name__}
|
| 44 |
"""
|
|
@@ -51,7 +50,7 @@ def latent_shape_info(height, width, pipe):
|
|
| 51 |
|
| 52 |
|
| 53 |
# =========================
|
| 54 |
-
#
|
| 55 |
# =========================
|
| 56 |
def download_scripts():
|
| 57 |
resp = requests.get(SCRIPTS_REPO_API)
|
|
@@ -74,29 +73,47 @@ SCRIPT_NAMES = download_scripts()
|
|
| 74 |
|
| 75 |
|
| 76 |
# =========================
|
| 77 |
-
# PIPELINE BUILDER
|
| 78 |
# =========================
|
| 79 |
-
def
|
| 80 |
-
global
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
|
| 98 |
# =========================
|
| 99 |
-
# IMAGE GENERATION
|
| 100 |
# =========================
|
| 101 |
@spaces.GPU
|
| 102 |
def generate_image(
|
|
@@ -107,34 +124,28 @@ def generate_image(
|
|
| 107 |
seed,
|
| 108 |
randomize_seed,
|
| 109 |
num_images,
|
|
|
|
| 110 |
):
|
| 111 |
-
global
|
| 112 |
|
| 113 |
log_buffer.truncate(0)
|
| 114 |
log_buffer.seek(0)
|
| 115 |
|
| 116 |
-
if
|
| 117 |
-
raise RuntimeError("Pipeline not built
|
|
|
|
|
|
|
| 118 |
|
| 119 |
log("=== NEW GENERATION REQUEST ===")
|
|
|
|
| 120 |
log(f"Prompt: {prompt}")
|
| 121 |
-
log(f"Height: {height}, Width: {width}")
|
| 122 |
-
log(f"Inference Steps: {num_inference_steps}")
|
| 123 |
-
log(f"Num Images: {num_images}")
|
| 124 |
|
| 125 |
if randomize_seed:
|
| 126 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 127 |
-
log(f"Randomized Seed → {seed}")
|
| 128 |
-
else:
|
| 129 |
-
log(f"Seed: {seed}")
|
| 130 |
|
| 131 |
num_images = min(max(1, int(num_images)), 3)
|
| 132 |
-
|
| 133 |
-
log(pipeline_debug_info(pipe))
|
| 134 |
-
|
| 135 |
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 136 |
|
| 137 |
-
log("Running pipeline forward()...")
|
| 138 |
result = pipe(
|
| 139 |
prompt=prompt,
|
| 140 |
height=int(height),
|
|
@@ -147,62 +158,61 @@ def generate_image(
|
|
| 147 |
output_type="pil",
|
| 148 |
)
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
log(f"VAE scaling factor: {pipe.vae.config.scaling_factor}")
|
| 153 |
-
log(f"Transformer latent size: {pipe.transformer.config.sample_size}")
|
| 154 |
-
log(latent_shape_info(height, width, pipe))
|
| 155 |
-
except Exception as e:
|
| 156 |
-
log(f"Latent diagnostics error: {e}")
|
| 157 |
|
| 158 |
-
log("Pipeline finished.")
|
| 159 |
return result.images, seed, log_buffer.getvalue()
|
| 160 |
|
| 161 |
|
| 162 |
# =========================
|
| 163 |
# GRADIO UI
|
| 164 |
# =========================
|
| 165 |
-
with gr.Blocks(title="Z-Image Turbo
|
| 166 |
-
gr.Markdown("## ⚡ Z-Image Turbo
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
)
|
| 173 |
|
| 174 |
-
build_btn = gr.Button("Build
|
| 175 |
-
|
| 176 |
|
| 177 |
build_btn.click(
|
| 178 |
-
|
| 179 |
inputs=[script_selector],
|
| 180 |
-
outputs=[
|
| 181 |
)
|
| 182 |
|
| 183 |
-
gr.
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
| 189 |
|
| 190 |
-
|
| 191 |
-
steps = gr.Slider(1, 8, value=4, step=1, label="Inference Steps")
|
| 192 |
-
images = gr.Slider(1, 3, value=1, step=1, label="Images")
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
-
|
| 199 |
|
| 200 |
-
gallery = gr.Gallery(
|
| 201 |
-
|
| 202 |
-
logs = gr.Textbox(label="Logs"
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
inputs=[
|
| 207 |
prompt,
|
| 208 |
height,
|
|
@@ -211,8 +221,9 @@ with gr.Blocks(title="Z-Image Turbo (ZeroGPU)") as demo:
|
|
| 211 |
seed,
|
| 212 |
random_seed,
|
| 213 |
images,
|
|
|
|
| 214 |
],
|
| 215 |
-
outputs=[gallery,
|
| 216 |
)
|
| 217 |
|
| 218 |
demo.queue()
|
|
|
|
| 2 |
import os
|
| 3 |
import io
|
| 4 |
import sys
|
|
|
|
| 5 |
import torch
|
| 6 |
|
| 7 |
import gradio as gr
|
|
|
|
| 20 |
|
| 21 |
os.makedirs(LOCAL_SCRIPTS_DIR, exist_ok=True)
|
| 22 |
|
| 23 |
+
pipelines = {} # script_name -> pipeline
|
| 24 |
+
active_pipeline = None
|
| 25 |
log_buffer = io.StringIO()
|
| 26 |
|
| 27 |
|
|
|
|
| 38 |
Pipeline Info
|
| 39 |
-------------
|
| 40 |
Device: {pipe.device}
|
|
|
|
| 41 |
Transformer: {pipe.transformer.__class__.__name__}
|
| 42 |
VAE: {pipe.vae.__class__.__name__}
|
| 43 |
"""
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
# =========================
|
| 53 |
+
# DOWNLOAD SCRIPTS
|
| 54 |
# =========================
|
| 55 |
def download_scripts():
|
| 56 |
resp = requests.get(SCRIPTS_REPO_API)
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
# =========================
|
| 76 |
+
# PIPELINE BUILDER (SCRIPT-DRIVEN)
|
| 77 |
# =========================
|
| 78 |
+
def build_pipelines(selected_scripts):
|
| 79 |
+
global pipelines, active_pipeline
|
| 80 |
|
| 81 |
+
pipelines.clear()
|
| 82 |
+
active_pipeline = None
|
| 83 |
|
| 84 |
+
for script_name in selected_scripts:
|
| 85 |
+
log(f"\n=== Building pipeline from {script_name} ===")
|
| 86 |
+
|
| 87 |
+
script_path = os.path.join(LOCAL_SCRIPTS_DIR, script_name)
|
| 88 |
+
|
| 89 |
+
# Each script runs in its own namespace
|
| 90 |
+
namespace = {
|
| 91 |
+
"torch": torch,
|
| 92 |
+
"DiffusionPipeline": DiffusionPipeline,
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
with open(script_path, "r") as f:
|
| 96 |
+
code = f.read()
|
| 97 |
+
|
| 98 |
+
# Execute script (this must define `pipe`)
|
| 99 |
+
exec(code, namespace)
|
| 100 |
+
|
| 101 |
+
if "pipe" not in namespace:
|
| 102 |
+
raise RuntimeError(f"{script_name} did not create `pipe`")
|
| 103 |
|
| 104 |
+
pipelines[script_name] = namespace["pipe"]
|
| 105 |
+
log(f"Loaded pipeline from {script_name}")
|
| 106 |
|
| 107 |
+
# Set default active pipeline
|
| 108 |
+
if pipelines:
|
| 109 |
+
active_pipeline = pipelines[selected_scripts[0]]
|
| 110 |
+
log(f"Active pipeline → {selected_scripts[0]}")
|
| 111 |
+
|
| 112 |
+
return f"Loaded {len(pipelines)} pipeline(s) ✅"
|
| 113 |
|
| 114 |
|
| 115 |
# =========================
|
| 116 |
+
# IMAGE GENERATION (UNCHANGED)
|
| 117 |
# =========================
|
| 118 |
@spaces.GPU
|
| 119 |
def generate_image(
|
|
|
|
| 124 |
seed,
|
| 125 |
randomize_seed,
|
| 126 |
num_images,
|
| 127 |
+
selected_pipeline_name,
|
| 128 |
):
|
| 129 |
+
global active_pipeline
|
| 130 |
|
| 131 |
log_buffer.truncate(0)
|
| 132 |
log_buffer.seek(0)
|
| 133 |
|
| 134 |
+
if selected_pipeline_name not in pipelines:
|
| 135 |
+
raise RuntimeError("Pipeline not built")
|
| 136 |
+
|
| 137 |
+
pipe = pipelines[selected_pipeline_name]
|
| 138 |
|
| 139 |
log("=== NEW GENERATION REQUEST ===")
|
| 140 |
+
log(f"Pipeline: {selected_pipeline_name}")
|
| 141 |
log(f"Prompt: {prompt}")
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
if randomize_seed:
|
| 144 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
num_images = min(max(1, int(num_images)), 3)
|
|
|
|
|
|
|
|
|
|
| 147 |
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 148 |
|
|
|
|
| 149 |
result = pipe(
|
| 150 |
prompt=prompt,
|
| 151 |
height=int(height),
|
|
|
|
| 158 |
output_type="pil",
|
| 159 |
)
|
| 160 |
|
| 161 |
+
log(pipeline_debug_info(pipe))
|
| 162 |
+
log("Done.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
|
|
|
| 164 |
return result.images, seed, log_buffer.getvalue()
|
| 165 |
|
| 166 |
|
| 167 |
# =========================
|
| 168 |
# GRADIO UI
|
| 169 |
# =========================
|
| 170 |
+
with gr.Blocks(title="Z-Image Turbo – Script Pipelines") as demo:
|
| 171 |
+
gr.Markdown("## ⚡ Z-Image Turbo (Script-Driven Pipelines, ZeroGPU)")
|
| 172 |
|
| 173 |
+
script_selector = gr.CheckboxGroup(
|
| 174 |
+
choices=SCRIPT_NAMES,
|
| 175 |
+
label="Select pipeline scripts",
|
| 176 |
+
)
|
|
|
|
| 177 |
|
| 178 |
+
build_btn = gr.Button("Build Pipelines")
|
| 179 |
+
status = gr.Textbox(label="Status")
|
| 180 |
|
| 181 |
build_btn.click(
|
| 182 |
+
build_pipelines,
|
| 183 |
inputs=[script_selector],
|
| 184 |
+
outputs=[status],
|
| 185 |
)
|
| 186 |
|
| 187 |
+
pipeline_picker = gr.Dropdown(
|
| 188 |
+
choices=[],
|
| 189 |
+
label="Active Pipeline",
|
| 190 |
+
)
|
| 191 |
|
| 192 |
+
build_btn.click(
|
| 193 |
+
lambda s: gr.update(choices=s, value=s[0] if s else None),
|
| 194 |
+
inputs=[script_selector],
|
| 195 |
+
outputs=[pipeline_picker],
|
| 196 |
+
)
|
| 197 |
|
| 198 |
+
gr.Markdown("---")
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
prompt = gr.Textbox(label="Prompt", lines=3)
|
| 201 |
+
height = gr.Slider(256, 1024, 512, step=64, label="Height")
|
| 202 |
+
width = gr.Slider(256, 1024, 512, step=64, label="Width")
|
| 203 |
+
steps = gr.Slider(1, 8, 4, step=1, label="Steps")
|
| 204 |
+
images = gr.Slider(1, 3, 1, step=1, label="Images")
|
| 205 |
+
seed = gr.Number(value=0, label="Seed")
|
| 206 |
+
random_seed = gr.Checkbox(value=True, label="Randomize Seed")
|
| 207 |
|
| 208 |
+
run = gr.Button("Generate")
|
| 209 |
|
| 210 |
+
gallery = gr.Gallery(columns=3)
|
| 211 |
+
used_seed = gr.Number(label="Used Seed")
|
| 212 |
+
logs = gr.Textbox(lines=12, label="Logs")
|
| 213 |
|
| 214 |
+
run.click(
|
| 215 |
+
generate_image,
|
| 216 |
inputs=[
|
| 217 |
prompt,
|
| 218 |
height,
|
|
|
|
| 221 |
seed,
|
| 222 |
random_seed,
|
| 223 |
images,
|
| 224 |
+
pipeline_picker,
|
| 225 |
],
|
| 226 |
+
outputs=[gallery, used_seed, logs],
|
| 227 |
)
|
| 228 |
|
| 229 |
demo.queue()
|