Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,17 +3,22 @@ import torch
|
|
| 3 |
from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
#
|
| 7 |
model_id = "Tongyi-MAI/Z-Image-Turbo"
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
txt2img = AutoPipelineForText2Image.from_pretrained(
|
| 11 |
-
model_id,
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
txt2img.to("cpu")
|
| 14 |
|
|
|
|
| 15 |
img2img = AutoPipelineForImage2Image.from_pipe(txt2img)
|
| 16 |
|
|
|
|
| 17 |
def generate_t2i(prompt, steps, guidance):
|
| 18 |
return txt2img(
|
| 19 |
prompt=prompt,
|
|
@@ -22,7 +27,9 @@ def generate_t2i(prompt, steps, guidance):
|
|
| 22 |
).images[0]
|
| 23 |
|
| 24 |
def generate_i2i(image, prompt, strength, steps, guidance):
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
image = image.convert("RGB").resize((512, 512))
|
| 27 |
return img2img(
|
| 28 |
prompt=prompt,
|
|
@@ -32,17 +39,18 @@ def generate_i2i(image, prompt, strength, steps, guidance):
|
|
| 32 |
guidance_scale=guidance
|
| 33 |
).images[0]
|
| 34 |
|
| 35 |
-
# --- UI
|
| 36 |
-
with gr.Blocks(
|
| 37 |
gr.Markdown("# 🚀 Z-Image Turbo (CPU Edition)")
|
|
|
|
| 38 |
|
| 39 |
with gr.Tabs():
|
| 40 |
# Text to Image Tab
|
| 41 |
with gr.TabItem("Text to Image"):
|
| 42 |
with gr.Row():
|
| 43 |
with gr.Column():
|
| 44 |
-
t2i_prompt = gr.Textbox(label="Prompt", placeholder="A
|
| 45 |
-
t2i_steps = gr.Slider(1, 12, value=8, step=1, label="Steps
|
| 46 |
t2i_guidance = gr.Slider(0.0, 2.0, value=0.0, step=0.1, label="Guidance Scale")
|
| 47 |
t2i_btn = gr.Button("Generate")
|
| 48 |
with gr.Column():
|
|
@@ -50,17 +58,18 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 50 |
t2i_btn.click(generate_t2i, inputs=[t2i_prompt, t2i_steps, t2i_guidance], outputs=t2i_output)
|
| 51 |
|
| 52 |
# Image to Image Tab
|
| 53 |
-
with gr.TabItem("Image to Image
|
| 54 |
with gr.Row():
|
| 55 |
with gr.Column():
|
| 56 |
-
i2i_input = gr.Image(type="pil", label="Upload
|
| 57 |
-
i2i_prompt = gr.Textbox(label="Edit Prompt", placeholder="
|
| 58 |
-
i2i_strength = gr.Slider(0.1, 1.0, value=0.5, step=0.05, label="Edit Strength
|
| 59 |
i2i_steps = gr.Slider(1, 12, value=10, step=1, label="Steps")
|
| 60 |
i2i_guidance = gr.Slider(0.0, 2.0, value=0.0, step=0.1, label="Guidance Scale")
|
| 61 |
-
i2i_btn = gr.Button("
|
| 62 |
with gr.Column():
|
| 63 |
i2i_output = gr.Image(label="Result")
|
| 64 |
i2i_btn.click(generate_i2i, inputs=[i2i_input, i2i_prompt, i2i_strength, i2i_steps, i2i_guidance], outputs=i2i_output)
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
| 3 |
from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# 1. LOAD MODEL (Using bfloat16 to fit in 16GB CPU RAM)
|
| 7 |
model_id = "Tongyi-MAI/Z-Image-Turbo"
|
| 8 |
|
| 9 |
+
print("Loading model... this takes a minute on CPU.")
|
| 10 |
+
# We use bfloat16 and low_cpu_mem_usage to prevent OOM crashes
|
| 11 |
txt2img = AutoPipelineForText2Image.from_pretrained(
|
| 12 |
+
model_id,
|
| 13 |
+
torch_dtype=torch.bfloat16,
|
| 14 |
+
low_cpu_mem_usage=True
|
| 15 |
)
|
| 16 |
txt2img.to("cpu")
|
| 17 |
|
| 18 |
+
# Reuse components for the Edit (i2i) pipeline to save memory
|
| 19 |
img2img = AutoPipelineForImage2Image.from_pipe(txt2img)
|
| 20 |
|
| 21 |
+
# --- FUNCTIONS ---
|
| 22 |
def generate_t2i(prompt, steps, guidance):
|
| 23 |
return txt2img(
|
| 24 |
prompt=prompt,
|
|
|
|
| 27 |
).images[0]
|
| 28 |
|
| 29 |
def generate_i2i(image, prompt, strength, steps, guidance):
|
| 30 |
+
if image is None:
|
| 31 |
+
return None
|
| 32 |
+
# Resize to 512 to keep the CPU from exploding
|
| 33 |
image = image.convert("RGB").resize((512, 512))
|
| 34 |
return img2img(
|
| 35 |
prompt=prompt,
|
|
|
|
| 39 |
guidance_scale=guidance
|
| 40 |
).images[0]
|
| 41 |
|
| 42 |
+
# --- UI LAYOUT ---
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
gr.Markdown("# 🚀 Z-Image Turbo (CPU Edition)")
|
| 45 |
+
gr.Markdown("Note: CPU generation is slow (1-3 mins). Use 512x512 for best results.")
|
| 46 |
|
| 47 |
with gr.Tabs():
|
| 48 |
# Text to Image Tab
|
| 49 |
with gr.TabItem("Text to Image"):
|
| 50 |
with gr.Row():
|
| 51 |
with gr.Column():
|
| 52 |
+
t2i_prompt = gr.Textbox(label="Prompt", placeholder="A liminal beach at night...")
|
| 53 |
+
t2i_steps = gr.Slider(1, 12, value=8, step=1, label="Steps")
|
| 54 |
t2i_guidance = gr.Slider(0.0, 2.0, value=0.0, step=0.1, label="Guidance Scale")
|
| 55 |
t2i_btn = gr.Button("Generate")
|
| 56 |
with gr.Column():
|
|
|
|
| 58 |
t2i_btn.click(generate_t2i, inputs=[t2i_prompt, t2i_steps, t2i_guidance], outputs=t2i_output)
|
| 59 |
|
| 60 |
# Image to Image Tab
|
| 61 |
+
with gr.TabItem("Image to Image"):
|
| 62 |
with gr.Row():
|
| 63 |
with gr.Column():
|
| 64 |
+
i2i_input = gr.Image(type="pil", label="Upload Photo")
|
| 65 |
+
i2i_prompt = gr.Textbox(label="Edit Prompt", placeholder="Make it look like a dark fantasy painting")
|
| 66 |
+
i2i_strength = gr.Slider(0.1, 1.0, value=0.5, step=0.05, label="Edit Strength")
|
| 67 |
i2i_steps = gr.Slider(1, 12, value=10, step=1, label="Steps")
|
| 68 |
i2i_guidance = gr.Slider(0.0, 2.0, value=0.0, step=0.1, label="Guidance Scale")
|
| 69 |
+
i2i_btn = gr.Button("Apply Edits")
|
| 70 |
with gr.Column():
|
| 71 |
i2i_output = gr.Image(label="Result")
|
| 72 |
i2i_btn.click(generate_i2i, inputs=[i2i_input, i2i_prompt, i2i_strength, i2i_steps, i2i_guidance], outputs=i2i_output)
|
| 73 |
|
| 74 |
+
# CRITICAL: theme moved here, ssr_mode=False added to fix the -1 error
|
| 75 |
+
demo.launch(theme=gr.themes.Soft(), ssr_mode=False)
|