Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load models with CPU-specific optimizations
|
| 7 |
+
model_id = "Tongyi-MAI/Z-Image-Turbo"
|
| 8 |
+
|
| 9 |
+
# Initialize both pipelines (they share the same components to save RAM)
|
| 10 |
+
txt2img = AutoPipelineForText2Image.from_pretrained(
|
| 11 |
+
model_id, torch_dtype=torch.float32, use_safetensors=True
|
| 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,
|
| 20 |
+
num_inference_steps=int(steps),
|
| 21 |
+
guidance_scale=guidance
|
| 22 |
+
).images[0]
|
| 23 |
+
|
| 24 |
+
def generate_i2i(image, prompt, strength, steps, guidance):
|
| 25 |
+
# Resize to prevent CPU OOM (Out of Memory)
|
| 26 |
+
image = image.convert("RGB").resize((512, 512))
|
| 27 |
+
return img2img(
|
| 28 |
+
prompt=prompt,
|
| 29 |
+
image=image,
|
| 30 |
+
strength=strength,
|
| 31 |
+
num_inference_steps=int(steps),
|
| 32 |
+
guidance_scale=guidance
|
| 33 |
+
).images[0]
|
| 34 |
+
|
| 35 |
+
# --- UI Layout ---
|
| 36 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 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 dark fantasy castle...")
|
| 45 |
+
t2i_steps = gr.Slider(1, 12, value=8, step=1, label="Steps (Turbo likes 8-10)")
|
| 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():
|
| 49 |
+
t2i_output = gr.Image(label="Result")
|
| 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 (The Editor)"):
|
| 54 |
+
with gr.Row():
|
| 55 |
+
with gr.Column():
|
| 56 |
+
i2i_input = gr.Image(type="pil", label="Upload Source Image")
|
| 57 |
+
i2i_prompt = gr.Textbox(label="Edit Prompt", placeholder="Change the style to...")
|
| 58 |
+
i2i_strength = gr.Slider(0.1, 1.0, value=0.5, step=0.05, label="Edit Strength (Lower = closer to original)")
|
| 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("Transform Image")
|
| 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 |
+
demo.launch()
|