Update app.py
Browse files
app.py
CHANGED
|
@@ -3,29 +3,38 @@ import spaces
|
|
| 3 |
import gradio as gr
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Load the pipeline once at startup
|
| 7 |
print("Loading Z-Image-Turbo pipeline...")
|
| 8 |
pipe = DiffusionPipeline.from_pretrained(
|
| 9 |
"Tongyi-MAI/Z-Image-Turbo",
|
| 10 |
-
torch_dtype=torch.
|
| 11 |
-
low_cpu_mem_usage=
|
| 12 |
)
|
| 13 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
-
pipe.to(device)
|
| 15 |
-
|
| 16 |
-
# ======== AoTI compilation + FA3 ========
|
| 17 |
-
# pipe.transformer.layers._repeated_blocks = ["ZImageTransformerBlock"]
|
| 18 |
-
# spaces.aoti_blocks_load(pipe.transformer.layers, "zerogpu-aoti/Z-Image", variant="fa3")
|
| 19 |
|
|
|
|
| 20 |
print("Pipeline loaded!")
|
| 21 |
|
| 22 |
-
|
| 23 |
-
def generate_image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
"""Generate an image from the given prompt."""
|
| 25 |
if randomize_seed:
|
| 26 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
image = pipe(
|
| 30 |
prompt=prompt,
|
| 31 |
height=int(height),
|
|
@@ -34,9 +43,10 @@ def generate_image(prompt, height, width, num_inference_steps, seed, randomize_s
|
|
| 34 |
guidance_scale=0.0,
|
| 35 |
generator=generator,
|
| 36 |
).images[0]
|
| 37 |
-
|
| 38 |
return image, seed
|
| 39 |
|
|
|
|
| 40 |
# Example prompts
|
| 41 |
examples = [
|
| 42 |
["Young Chinese woman in red Hanfu, intricate embroidery. Impeccable makeup, red floral forehead pattern. Elaborate high bun, golden phoenix headdress, red flowers, beads. Holds round folding fan with lady, trees, bird. Neon lightning-bolt lamp, bright yellow glow, above extended left palm. Soft-lit outdoor night background, silhouetted tiered pagoda, blurred colorful distant lights."],
|
|
@@ -258,4 +268,4 @@ if __name__ == "__main__":
|
|
| 258 |
"gradio"
|
| 259 |
],
|
| 260 |
mcp_server=True
|
| 261 |
-
)
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
|
| 6 |
+
# ====== DEVICE SETUP ======
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
print(f"Using device: {device}")
|
| 9 |
+
|
| 10 |
# Load the pipeline once at startup
|
| 11 |
print("Loading Z-Image-Turbo pipeline...")
|
| 12 |
pipe = DiffusionPipeline.from_pretrained(
|
| 13 |
"Tongyi-MAI/Z-Image-Turbo",
|
| 14 |
+
torch_dtype=torch.float32, # CPU-friendly dtype
|
| 15 |
+
low_cpu_mem_usage=True,
|
| 16 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
pipe.to(device)
|
| 19 |
print("Pipeline loaded!")
|
| 20 |
|
| 21 |
+
|
| 22 |
+
def generate_image(
|
| 23 |
+
prompt,
|
| 24 |
+
height,
|
| 25 |
+
width,
|
| 26 |
+
num_inference_steps,
|
| 27 |
+
seed,
|
| 28 |
+
randomize_seed,
|
| 29 |
+
progress=gr.Progress(track_tqdm=True),
|
| 30 |
+
):
|
| 31 |
"""Generate an image from the given prompt."""
|
| 32 |
if randomize_seed:
|
| 33 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 34 |
+
|
| 35 |
+
gen_device = "cuda" if (device == "cuda") else "cpu"
|
| 36 |
+
generator = torch.Generator(gen_device).manual_seed(int(seed))
|
| 37 |
+
|
| 38 |
image = pipe(
|
| 39 |
prompt=prompt,
|
| 40 |
height=int(height),
|
|
|
|
| 43 |
guidance_scale=0.0,
|
| 44 |
generator=generator,
|
| 45 |
).images[0]
|
| 46 |
+
|
| 47 |
return image, seed
|
| 48 |
|
| 49 |
+
|
| 50 |
# Example prompts
|
| 51 |
examples = [
|
| 52 |
["Young Chinese woman in red Hanfu, intricate embroidery. Impeccable makeup, red floral forehead pattern. Elaborate high bun, golden phoenix headdress, red flowers, beads. Holds round folding fan with lady, trees, bird. Neon lightning-bolt lamp, bright yellow glow, above extended left palm. Soft-lit outdoor night background, silhouetted tiered pagoda, blurred colorful distant lights."],
|
|
|
|
| 268 |
"gradio"
|
| 269 |
],
|
| 270 |
mcp_server=True
|
| 271 |
+
)
|