Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,32 @@
|
|
| 1 |
import torch
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
-
from diffusers import DiffusionPipeline
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
"Tongyi-MAI/Z-Image-Turbo",
|
| 10 |
torch_dtype=torch.bfloat16,
|
| 11 |
low_cpu_mem_usage=False,
|
| 12 |
)
|
| 13 |
-
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
print("
|
| 20 |
|
| 21 |
@spaces.GPU
|
| 22 |
-
def
|
| 23 |
-
"""Generate an image from the given prompt."""
|
| 24 |
if randomize_seed:
|
| 25 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 26 |
-
|
| 27 |
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 28 |
-
image =
|
| 29 |
prompt=prompt,
|
| 30 |
height=int(height),
|
| 31 |
width=int(width),
|
|
@@ -33,19 +34,30 @@ def generate_image(prompt, height, width, num_inference_steps, seed, randomize_s
|
|
| 33 |
guidance_scale=0.0,
|
| 34 |
generator=generator,
|
| 35 |
).images[0]
|
| 36 |
-
|
| 37 |
return image, seed
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
["Astronaut riding a horse on Mars, cinematic lighting, sci-fi concept art, highly detailed"],
|
| 45 |
-
["Portrait of a wise old wizard with a long white beard, holding a glowing crystal staff
|
| 46 |
]
|
| 47 |
|
| 48 |
-
# Custom theme with modern aesthetics (Gradio 6)
|
| 49 |
custom_theme = gr.themes.Soft(
|
| 50 |
primary_hue="yellow",
|
| 51 |
secondary_hue="amber",
|
|
@@ -60,135 +72,86 @@ custom_theme = gr.themes.Soft(
|
|
| 60 |
block_title_text_weight="600",
|
| 61 |
)
|
| 62 |
|
| 63 |
-
# Build the Gradio interface
|
| 64 |
with gr.Blocks(fill_height=True) as demo:
|
| 65 |
-
# Header
|
| 66 |
gr.Markdown(
|
| 67 |
"""
|
| 68 |
# π¨ Z-Image-Turbo
|
| 69 |
-
**Ultra-fast AI image generation** β’
|
| 70 |
""",
|
| 71 |
elem_classes="header-text"
|
| 72 |
)
|
| 73 |
-
|
| 74 |
-
with gr.
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
with gr.Row():
|
| 87 |
-
height = gr.Slider(
|
| 88 |
-
minimum=512,
|
| 89 |
-
maximum=2048,
|
| 90 |
-
value=1024,
|
| 91 |
-
step=64,
|
| 92 |
-
label="Height",
|
| 93 |
-
info="Image height in pixels"
|
| 94 |
-
)
|
| 95 |
-
width = gr.Slider(
|
| 96 |
-
minimum=512,
|
| 97 |
-
maximum=2048,
|
| 98 |
-
value=1024,
|
| 99 |
-
step=64,
|
| 100 |
-
label="Width",
|
| 101 |
-
info="Image width in pixels"
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
num_inference_steps = gr.Slider(
|
| 105 |
-
minimum=1,
|
| 106 |
-
maximum=20,
|
| 107 |
-
value=9,
|
| 108 |
-
step=1,
|
| 109 |
-
label="Inference Steps",
|
| 110 |
-
info="9 steps = 8 DiT forwards (recommended)"
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
with gr.Row():
|
| 114 |
-
randomize_seed = gr.Checkbox(
|
| 115 |
-
label="π² Random Seed",
|
| 116 |
-
value=True,
|
| 117 |
)
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
)
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
gr.Examples(
|
| 143 |
-
examples=examples,
|
| 144 |
-
inputs=[prompt],
|
| 145 |
-
label="π‘ Try these prompts",
|
| 146 |
-
examples_per_page=5,
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
# Right column - Output
|
| 150 |
-
with gr.Column(scale=1, min_width=320):
|
| 151 |
-
output_image = gr.Image(
|
| 152 |
-
label="Generated Image",
|
| 153 |
-
type="pil",
|
| 154 |
-
format="png",
|
| 155 |
-
show_label=False,
|
| 156 |
-
height=600,
|
| 157 |
-
buttons=["download", "share"],
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
used_seed = gr.Number(
|
| 161 |
-
label="π² Seed Used",
|
| 162 |
-
interactive=False,
|
| 163 |
-
container=True,
|
| 164 |
-
)
|
| 165 |
-
|
| 166 |
-
# Footer credits
|
| 167 |
gr.Markdown(
|
| 168 |
"""
|
| 169 |
---
|
| 170 |
-
<div style="text-align: center; opacity: 0.7; font-size: 0.9em;
|
| 171 |
-
<strong>Model:</strong>
|
| 172 |
-
<strong>
|
| 173 |
-
<strong>Redesign by:</strong> AnyCoder β’
|
| 174 |
-
<strong>Optimizations:</strong> <a href="https://huggingface.co/multimodalart" target="_blank">@multimodalart</a> (FA3 + AoTI)
|
| 175 |
</div>
|
| 176 |
""",
|
| 177 |
-
elem_classes="footer-text"
|
| 178 |
-
)
|
| 179 |
-
|
| 180 |
-
# Connect the generate button
|
| 181 |
-
generate_btn.click(
|
| 182 |
-
fn=generate_image,
|
| 183 |
-
inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed],
|
| 184 |
-
outputs=[output_image, used_seed],
|
| 185 |
-
)
|
| 186 |
-
|
| 187 |
-
# Also allow generating by pressing Enter in the prompt box
|
| 188 |
-
prompt.submit(
|
| 189 |
-
fn=generate_image,
|
| 190 |
-
inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed],
|
| 191 |
-
outputs=[output_image, used_seed],
|
| 192 |
)
|
| 193 |
|
| 194 |
if __name__ == "__main__":
|
|
@@ -198,63 +161,15 @@ if __name__ == "__main__":
|
|
| 198 |
.header-text h1 {
|
| 199 |
font-size: 2.5rem !important;
|
| 200 |
font-weight: 700 !important;
|
| 201 |
-
margin-bottom: 0.5rem !important;
|
| 202 |
background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
|
| 203 |
-webkit-background-clip: text;
|
| 204 |
-webkit-text-fill-color: transparent;
|
| 205 |
background-clip: text;
|
| 206 |
}
|
| 207 |
-
|
| 208 |
-
.
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
margin-top: 0 !important;
|
| 212 |
-
}
|
| 213 |
-
|
| 214 |
-
.footer-text {
|
| 215 |
-
padding: 1rem 0;
|
| 216 |
-
}
|
| 217 |
-
|
| 218 |
-
.footer-text a {
|
| 219 |
-
color: #f59e0b !important;
|
| 220 |
-
text-decoration: none !important;
|
| 221 |
-
font-weight: 500;
|
| 222 |
-
}
|
| 223 |
-
|
| 224 |
-
.footer-text a:hover {
|
| 225 |
-
text-decoration: underline !important;
|
| 226 |
-
}
|
| 227 |
-
|
| 228 |
-
/* Mobile optimizations */
|
| 229 |
-
@media (max-width: 768px) {
|
| 230 |
-
.header-text h1 {
|
| 231 |
-
font-size: 1.8rem !important;
|
| 232 |
-
}
|
| 233 |
-
|
| 234 |
-
.header-text p {
|
| 235 |
-
font-size: 1rem !important;
|
| 236 |
-
}
|
| 237 |
-
}
|
| 238 |
-
|
| 239 |
-
/* Smooth transitions */
|
| 240 |
-
button, .gr-button {
|
| 241 |
-
transition: all 0.2s ease !important;
|
| 242 |
-
}
|
| 243 |
-
|
| 244 |
-
button:hover, .gr-button:hover {
|
| 245 |
-
transform: translateY(-1px);
|
| 246 |
-
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15) !important;
|
| 247 |
-
}
|
| 248 |
-
|
| 249 |
-
/* Better spacing */
|
| 250 |
-
.gradio-container {
|
| 251 |
-
max-width: 1400px !important;
|
| 252 |
-
margin: 0 auto !important;
|
| 253 |
-
}
|
| 254 |
""",
|
| 255 |
-
footer_links=[
|
| 256 |
-
"api",
|
| 257 |
-
"gradio"
|
| 258 |
-
],
|
| 259 |
mcp_server=True
|
| 260 |
)
|
|
|
|
| 1 |
import torch
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
+
from diffusers import DiffusionPipeline, FluxImg2ImgPipeline
|
| 5 |
|
| 6 |
+
print("Loading pipelines...")
|
| 7 |
+
# Text to image pipeline
|
| 8 |
+
pipe_t2i = DiffusionPipeline.from_pretrained(
|
| 9 |
"Tongyi-MAI/Z-Image-Turbo",
|
| 10 |
torch_dtype=torch.bfloat16,
|
| 11 |
low_cpu_mem_usage=False,
|
| 12 |
)
|
| 13 |
+
pipe_t2i.to("cuda")
|
| 14 |
|
| 15 |
+
# Image to image pipeline
|
| 16 |
+
pipe_i2i = FluxImg2ImgPipeline.from_pretrained(
|
| 17 |
+
"black-forest-labs/FLUX.1-schnell",
|
| 18 |
+
torch_dtype=torch.bfloat16,
|
| 19 |
+
)
|
| 20 |
+
pipe_i2i.to("cuda")
|
| 21 |
|
| 22 |
+
print("Pipelines loaded!")
|
| 23 |
|
| 24 |
@spaces.GPU
|
| 25 |
+
def generate_t2i(prompt, height, width, num_inference_steps, seed, randomize_seed, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 26 |
if randomize_seed:
|
| 27 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
|
|
|
| 28 |
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 29 |
+
image = pipe_t2i(
|
| 30 |
prompt=prompt,
|
| 31 |
height=int(height),
|
| 32 |
width=int(width),
|
|
|
|
| 34 |
guidance_scale=0.0,
|
| 35 |
generator=generator,
|
| 36 |
).images[0]
|
|
|
|
| 37 |
return image, seed
|
| 38 |
|
| 39 |
+
@spaces.GPU
|
| 40 |
+
def generate_i2i(input_image, prompt, strength, num_inference_steps, seed, randomize_seed, progress=gr.Progress(track_tqdm=True)):
|
| 41 |
+
if randomize_seed:
|
| 42 |
+
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 43 |
+
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 44 |
+
image = pipe_i2i(
|
| 45 |
+
prompt=prompt,
|
| 46 |
+
image=input_image,
|
| 47 |
+
strength=float(strength),
|
| 48 |
+
num_inference_steps=int(num_inference_steps),
|
| 49 |
+
generator=generator,
|
| 50 |
+
).images[0]
|
| 51 |
+
return image, seed
|
| 52 |
+
|
| 53 |
+
examples_t2i = [
|
| 54 |
+
["Young Chinese woman in red Hanfu, intricate embroidery, elaborate high bun, golden phoenix headdress"],
|
| 55 |
+
["A majestic dragon soaring through clouds at sunset, scales shimmering with iridescent colors"],
|
| 56 |
+
["Cozy coffee shop interior, warm lighting, rain on windows, plants on shelves, photorealistic"],
|
| 57 |
["Astronaut riding a horse on Mars, cinematic lighting, sci-fi concept art, highly detailed"],
|
| 58 |
+
["Portrait of a wise old wizard with a long white beard, holding a glowing crystal staff"],
|
| 59 |
]
|
| 60 |
|
|
|
|
| 61 |
custom_theme = gr.themes.Soft(
|
| 62 |
primary_hue="yellow",
|
| 63 |
secondary_hue="amber",
|
|
|
|
| 72 |
block_title_text_weight="600",
|
| 73 |
)
|
| 74 |
|
|
|
|
| 75 |
with gr.Blocks(fill_height=True) as demo:
|
|
|
|
| 76 |
gr.Markdown(
|
| 77 |
"""
|
| 78 |
# π¨ Z-Image-Turbo
|
| 79 |
+
**Ultra-fast AI image generation & editing** β’ Text to Image + Image to Image
|
| 80 |
""",
|
| 81 |
elem_classes="header-text"
|
| 82 |
)
|
| 83 |
+
|
| 84 |
+
with gr.Tabs():
|
| 85 |
+
|
| 86 |
+
# ββ Tab 1: Text to Image ββββββββββββββββββββββββββββββββββββββ
|
| 87 |
+
with gr.Tab("β¨ Text to Image"):
|
| 88 |
+
with gr.Row(equal_height=False):
|
| 89 |
+
with gr.Column(scale=1, min_width=320):
|
| 90 |
+
t2i_prompt = gr.Textbox(
|
| 91 |
+
label="β¨ Your Prompt",
|
| 92 |
+
placeholder="Describe the image you want to create...",
|
| 93 |
+
lines=5,
|
| 94 |
+
max_lines=10,
|
| 95 |
+
autofocus=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
)
|
| 97 |
+
with gr.Accordion("βοΈ Advanced Settings", open=False):
|
| 98 |
+
with gr.Row():
|
| 99 |
+
t2i_height = gr.Slider(512, 2048, value=1024, step=64, label="Height")
|
| 100 |
+
t2i_width = gr.Slider(512, 2048, value=1024, step=64, label="Width")
|
| 101 |
+
t2i_steps = gr.Slider(1, 20, value=9, step=1, label="Inference Steps")
|
| 102 |
+
with gr.Row():
|
| 103 |
+
t2i_randomize = gr.Checkbox(label="π² Random Seed", value=True)
|
| 104 |
+
t2i_seed = gr.Number(label="Seed", value=42, precision=0, visible=False)
|
| 105 |
+
t2i_randomize.change(
|
| 106 |
+
lambda r: gr.Number(visible=not r),
|
| 107 |
+
inputs=[t2i_randomize], outputs=[t2i_seed]
|
| 108 |
+
)
|
| 109 |
+
t2i_btn = gr.Button("π Generate Image", variant="primary", size="lg")
|
| 110 |
+
gr.Examples(examples=examples_t2i, inputs=[t2i_prompt], label="π‘ Try these prompts")
|
| 111 |
+
|
| 112 |
+
with gr.Column(scale=1, min_width=320):
|
| 113 |
+
t2i_output = gr.Image(label="Generated Image", type="pil", format="png", show_label=False, height=600)
|
| 114 |
+
t2i_used_seed = gr.Number(label="π² Seed Used", interactive=False)
|
| 115 |
+
|
| 116 |
+
t2i_btn.click(generate_t2i, [t2i_prompt, t2i_height, t2i_width, t2i_steps, t2i_seed, t2i_randomize], [t2i_output, t2i_used_seed])
|
| 117 |
+
t2i_prompt.submit(generate_t2i, [t2i_prompt, t2i_height, t2i_width, t2i_steps, t2i_seed, t2i_randomize], [t2i_output, t2i_used_seed])
|
| 118 |
+
|
| 119 |
+
# ββ Tab 2: Image to Image βββββββββββββββββββββββββββββββββββββ
|
| 120 |
+
with gr.Tab("πΌοΈ Image to Image"):
|
| 121 |
+
with gr.Row(equal_height=False):
|
| 122 |
+
with gr.Column(scale=1, min_width=320):
|
| 123 |
+
i2i_input = gr.Image(label="Upload Image", type="pil")
|
| 124 |
+
i2i_prompt = gr.Textbox(
|
| 125 |
+
label="β¨ Edit Instruction",
|
| 126 |
+
placeholder="Describe how you want to edit the image...",
|
| 127 |
+
lines=4,
|
| 128 |
)
|
| 129 |
+
with gr.Accordion("βοΈ Advanced Settings", open=False):
|
| 130 |
+
i2i_strength = gr.Slider(0.1, 1.0, value=0.75, step=0.05, label="Strength", info="Higher = more change")
|
| 131 |
+
i2i_steps = gr.Slider(1, 8, value=4, step=1, label="Inference Steps")
|
| 132 |
+
with gr.Row():
|
| 133 |
+
i2i_randomize = gr.Checkbox(label="π² Random Seed", value=True)
|
| 134 |
+
i2i_seed = gr.Number(label="Seed", value=42, precision=0, visible=False)
|
| 135 |
+
i2i_randomize.change(
|
| 136 |
+
lambda r: gr.Number(visible=not r),
|
| 137 |
+
inputs=[i2i_randomize], outputs=[i2i_seed]
|
| 138 |
+
)
|
| 139 |
+
i2i_btn = gr.Button("π Edit Image", variant="primary", size="lg")
|
| 140 |
+
|
| 141 |
+
with gr.Column(scale=1, min_width=320):
|
| 142 |
+
i2i_output = gr.Image(label="Result", type="pil", format="png", show_label=False, height=600)
|
| 143 |
+
i2i_used_seed = gr.Number(label="π² Seed Used", interactive=False)
|
| 144 |
+
|
| 145 |
+
i2i_btn.click(generate_i2i, [i2i_input, i2i_prompt, i2i_strength, i2i_steps, i2i_seed, i2i_randomize], [i2i_output, i2i_used_seed])
|
| 146 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
gr.Markdown(
|
| 148 |
"""
|
| 149 |
---
|
| 150 |
+
<div style="text-align: center; opacity: 0.7; font-size: 0.9em;">
|
| 151 |
+
<strong>T2I Model:</strong> Tongyi-MAI/Z-Image-Turbo β’
|
| 152 |
+
<strong>I2I Model:</strong> FLUX.1-schnell
|
|
|
|
|
|
|
| 153 |
</div>
|
| 154 |
""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
)
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
|
|
|
| 161 |
.header-text h1 {
|
| 162 |
font-size: 2.5rem !important;
|
| 163 |
font-weight: 700 !important;
|
|
|
|
| 164 |
background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
|
| 165 |
-webkit-background-clip: text;
|
| 166 |
-webkit-text-fill-color: transparent;
|
| 167 |
background-clip: text;
|
| 168 |
}
|
| 169 |
+
.header-text p { font-size: 1.1rem !important; color: #64748b !important; }
|
| 170 |
+
.gradio-container { max-width: 1400px !important; margin: 0 auto !important; }
|
| 171 |
+
button, .gr-button { transition: all 0.2s ease !important; }
|
| 172 |
+
button:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(0,0,0,0.15) !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
""",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
mcp_server=True
|
| 175 |
)
|