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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
import spaces
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import torch
|
| 6 |
+
from diffusers import ZImagePipeline
|
| 7 |
+
import os
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
# Load the model directly at startup
|
| 11 |
+
print("Loading Z-Image Turbo model...")
|
| 12 |
+
print("This may take a few minutes on first run while the model downloads...")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Load the pipeline with optimal settings
|
| 16 |
+
pipe = ZImagePipeline.from_pretrained(
|
| 17 |
+
"Tongyi-MAI/Z-Image-Turbo",
|
| 18 |
+
torch_dtype=torch.bfloat16,
|
| 19 |
+
low_cpu_mem_usage=False,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Move to GPU if available
|
| 23 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
+
pipe.to(device)
|
| 25 |
+
print(f"Model loaded on {device}")
|
| 26 |
+
|
| 27 |
+
print("Model loaded successfully!")
|
| 28 |
+
|
| 29 |
+
@spaces.GPU()
|
| 30 |
+
def generate_image(
|
| 31 |
+
prompt,
|
| 32 |
+
progress=gr.Progress(track_tqdm=True)
|
| 33 |
+
):
|
| 34 |
+
"""
|
| 35 |
+
Generate an image using Z-Image Turbo model.
|
| 36 |
+
|
| 37 |
+
Args:
|
| 38 |
+
prompt: Text description of the desired image
|
| 39 |
+
|
| 40 |
+
Returns:
|
| 41 |
+
Generated PIL Image
|
| 42 |
+
"""
|
| 43 |
+
global pipe
|
| 44 |
+
|
| 45 |
+
if pipe is None:
|
| 46 |
+
raise gr.Error("Model failed to load on startup. Please restart the application.")
|
| 47 |
+
|
| 48 |
+
if not prompt.strip():
|
| 49 |
+
raise gr.Error("Please enter a prompt to generate an image.")
|
| 50 |
+
|
| 51 |
+
# Determine device
|
| 52 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 53 |
+
|
| 54 |
+
# Set random seed for reproducibility
|
| 55 |
+
generator = torch.Generator(device).manual_seed(42)
|
| 56 |
+
|
| 57 |
+
# Generate the image with optimal settings
|
| 58 |
+
progress(0.1, desc="Generating image...")
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
result = pipe(
|
| 62 |
+
prompt=prompt,
|
| 63 |
+
negative_prompt=None,
|
| 64 |
+
height=1024,
|
| 65 |
+
width=1024,
|
| 66 |
+
num_inference_steps=9,
|
| 67 |
+
guidance_scale=0.0,
|
| 68 |
+
generator=generator,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
image = result.images[0]
|
| 72 |
+
progress(1.0, desc="Complete!")
|
| 73 |
+
|
| 74 |
+
return image
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
raise gr.Error(f"Generation failed: {str(e)}")
|
| 78 |
+
|
| 79 |
+
# Apple-style CSS
|
| 80 |
+
apple_css = """
|
| 81 |
+
/* Global Styles */
|
| 82 |
+
.gradio-container {
|
| 83 |
+
max-width: 980px !important;
|
| 84 |
+
margin: 0 auto !important;
|
| 85 |
+
padding: 48px 20px !important;
|
| 86 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', 'Roboto', sans-serif !important;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
/* Header */
|
| 90 |
+
.header-container {
|
| 91 |
+
text-align: center;
|
| 92 |
+
margin-bottom: 48px;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
.main-title {
|
| 96 |
+
font-size: 56px !important;
|
| 97 |
+
font-weight: 600 !important;
|
| 98 |
+
letter-spacing: -0.02em !important;
|
| 99 |
+
line-height: 1.07 !important;
|
| 100 |
+
color: #1d1d1f !important;
|
| 101 |
+
margin: 0 0 12px 0 !important;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
.subtitle {
|
| 105 |
+
font-size: 21px !important;
|
| 106 |
+
font-weight: 400 !important;
|
| 107 |
+
line-height: 1.38 !important;
|
| 108 |
+
color: #6e6e73 !important;
|
| 109 |
+
margin: 0 0 24px 0 !important;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
.attribution-link {
|
| 113 |
+
display: inline-block;
|
| 114 |
+
font-size: 14px !important;
|
| 115 |
+
color: #0071e3 !important;
|
| 116 |
+
text-decoration: none !important;
|
| 117 |
+
font-weight: 400 !important;
|
| 118 |
+
transition: color 0.2s ease !important;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
.attribution-link:hover {
|
| 122 |
+
color: #0077ed !important;
|
| 123 |
+
text-decoration: underline !important;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
/* Input Section */
|
| 127 |
+
.input-section {
|
| 128 |
+
background: #ffffff;
|
| 129 |
+
border-radius: 18px;
|
| 130 |
+
padding: 32px;
|
| 131 |
+
margin-bottom: 24px;
|
| 132 |
+
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
/* Textbox */
|
| 136 |
+
textarea {
|
| 137 |
+
font-size: 17px !important;
|
| 138 |
+
line-height: 1.47 !important;
|
| 139 |
+
border-radius: 12px !important;
|
| 140 |
+
border: 1px solid #d2d2d7 !important;
|
| 141 |
+
padding: 12px 16px !important;
|
| 142 |
+
transition: all 0.2s ease !important;
|
| 143 |
+
background: #ffffff !important;
|
| 144 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Inter', sans-serif !important;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
textarea:focus {
|
| 148 |
+
border-color: #0071e3 !important;
|
| 149 |
+
box-shadow: 0 0 0 4px rgba(0, 113, 227, 0.15) !important;
|
| 150 |
+
outline: none !important;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
textarea::placeholder {
|
| 154 |
+
color: #86868b !important;
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
/* Button */
|
| 158 |
+
button.primary {
|
| 159 |
+
font-size: 17px !important;
|
| 160 |
+
font-weight: 400 !important;
|
| 161 |
+
padding: 12px 32px !important;
|
| 162 |
+
border-radius: 980px !important;
|
| 163 |
+
background: #0071e3 !important;
|
| 164 |
+
border: none !important;
|
| 165 |
+
color: #ffffff !important;
|
| 166 |
+
min-height: 44px !important;
|
| 167 |
+
transition: all 0.2s ease !important;
|
| 168 |
+
letter-spacing: -0.01em !important;
|
| 169 |
+
cursor: pointer !important;
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
button.primary:hover {
|
| 173 |
+
background: #0077ed !important;
|
| 174 |
+
transform: scale(1.02) !important;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
button.primary:active {
|
| 178 |
+
transform: scale(0.98) !important;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
/* Output Section */
|
| 182 |
+
.output-section {
|
| 183 |
+
background: #ffffff;
|
| 184 |
+
border-radius: 18px;
|
| 185 |
+
padding: 32px;
|
| 186 |
+
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
|
| 187 |
+
overflow: hidden;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
.output-section img {
|
| 191 |
+
border-radius: 12px !important;
|
| 192 |
+
width: 100% !important;
|
| 193 |
+
height: auto !important;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
/* Footer */
|
| 197 |
+
.footer-text {
|
| 198 |
+
text-align: center;
|
| 199 |
+
margin-top: 48px;
|
| 200 |
+
font-size: 14px !important;
|
| 201 |
+
color: #86868b !important;
|
| 202 |
+
line-height: 1.43 !important;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
/* Progress */
|
| 206 |
+
.progress-bar {
|
| 207 |
+
background: #0071e3 !important;
|
| 208 |
+
border-radius: 4px !important;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
/* Dark Mode */
|
| 212 |
+
.dark .main-title {
|
| 213 |
+
color: #f5f5f7 !important;
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
.dark .subtitle {
|
| 217 |
+
color: #a1a1a6 !important;
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
.dark .input-section,
|
| 221 |
+
.dark .output-section {
|
| 222 |
+
background: #1d1d1f;
|
| 223 |
+
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
.dark textarea {
|
| 227 |
+
background: #1d1d1f !important;
|
| 228 |
+
border-color: #424245 !important;
|
| 229 |
+
color: #f5f5f7 !important;
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
.dark textarea::placeholder {
|
| 233 |
+
color: #86868b !important;
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
/* Responsive */
|
| 237 |
+
@media (max-width: 734px) {
|
| 238 |
+
.main-title {
|
| 239 |
+
font-size: 40px !important;
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
.subtitle {
|
| 243 |
+
font-size: 19px !important;
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
.gradio-container {
|
| 247 |
+
padding: 32px 16px !important;
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
.input-section,
|
| 251 |
+
.output-section {
|
| 252 |
+
padding: 24px !important;
|
| 253 |
+
}
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
/* Remove default Gradio styling */
|
| 257 |
+
.contain {
|
| 258 |
+
padding: 0 !important;
|
| 259 |
+
}
|
| 260 |
+
"""
|
| 261 |
+
|
| 262 |
+
# Create the interface
|
| 263 |
+
with gr.Blocks(
|
| 264 |
+
title="Z-Image Turbo",
|
| 265 |
+
fill_height=False,
|
| 266 |
+
) as demo:
|
| 267 |
+
|
| 268 |
+
# Header
|
| 269 |
+
gr.HTML("""
|
| 270 |
+
<div class="header-container">
|
| 271 |
+
<h1 class="main-title">Z-Image Turbo</h1>
|
| 272 |
+
<p class="subtitle">Transform your ideas into stunning visuals with AI</p>
|
| 273 |
+
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" class="attribution-link">
|
| 274 |
+
Built with anycoder
|
| 275 |
+
</a>
|
| 276 |
+
</div>
|
| 277 |
+
""")
|
| 278 |
+
|
| 279 |
+
# Input Section
|
| 280 |
+
with gr.Column(elem_classes="input-section"):
|
| 281 |
+
prompt = gr.Textbox(
|
| 282 |
+
placeholder="Describe the image you want to create...",
|
| 283 |
+
lines=3,
|
| 284 |
+
max_lines=6,
|
| 285 |
+
label="",
|
| 286 |
+
show_label=False,
|
| 287 |
+
container=False,
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
generate_btn = gr.Button(
|
| 291 |
+
"Generate",
|
| 292 |
+
variant="primary",
|
| 293 |
+
size="lg",
|
| 294 |
+
elem_classes="primary"
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
# Output Section
|
| 298 |
+
with gr.Column(elem_classes="output-section"):
|
| 299 |
+
output_image = gr.Image(
|
| 300 |
+
type="pil",
|
| 301 |
+
label="",
|
| 302 |
+
show_label=False,
|
| 303 |
+
container=False,
|
| 304 |
+
buttons=["download"],
|
| 305 |
+
)
|
| 306 |
+
|
| 307 |
+
# Footer
|
| 308 |
+
gr.HTML("""
|
| 309 |
+
<div class="footer-text">
|
| 310 |
+
<p>Powered by Z-Image Turbo from Tongyi-MAI</p>
|
| 311 |
+
</div>
|
| 312 |
+
""")
|
| 313 |
+
|
| 314 |
+
# Event handlers
|
| 315 |
+
generate_btn.click(
|
| 316 |
+
fn=generate_image,
|
| 317 |
+
inputs=prompt,
|
| 318 |
+
outputs=output_image,
|
| 319 |
+
api_visibility="public"
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
prompt.submit(
|
| 323 |
+
fn=generate_image,
|
| 324 |
+
inputs=prompt,
|
| 325 |
+
outputs=output_image,
|
| 326 |
+
api_visibility="public"
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
if __name__ == "__main__":
|
| 330 |
+
demo.launch(
|
| 331 |
+
share=False,
|
| 332 |
+
show_error=True,
|
| 333 |
+
theme=gr.themes.Soft(
|
| 334 |
+
primary_hue=gr.themes.colors.blue,
|
| 335 |
+
secondary_hue=gr.themes.colors.slate,
|
| 336 |
+
neutral_hue=gr.themes.colors.gray,
|
| 337 |
+
spacing_size=gr.themes.sizes.spacing_lg,
|
| 338 |
+
radius_size=gr.themes.sizes.radius_lg,
|
| 339 |
+
text_size=gr.themes.sizes.text_md,
|
| 340 |
+
font=[gr.themes.GoogleFont("Inter"), "SF Pro Display", "-apple-system", "BlinkMacSystemFont", "system-ui", "sans-serif"],
|
| 341 |
+
font_mono=[gr.themes.GoogleFont("JetBrains Mono"), "SF Mono", "ui-monospace", "monospace"],
|
| 342 |
+
).set(
|
| 343 |
+
body_background_fill='#f5f5f7',
|
| 344 |
+
body_background_fill_dark='#000000',
|
| 345 |
+
button_primary_background_fill='#0071e3',
|
| 346 |
+
button_primary_background_fill_hover='#0077ed',
|
| 347 |
+
button_primary_text_color='#ffffff',
|
| 348 |
+
block_background_fill='#ffffff',
|
| 349 |
+
block_background_fill_dark='#1d1d1f',
|
| 350 |
+
block_border_width='0px',
|
| 351 |
+
block_shadow='0 2px 12px rgba(0, 0, 0, 0.08)',
|
| 352 |
+
block_shadow_dark='0 2px 12px rgba(0, 0, 0, 0.4)',
|
| 353 |
+
input_background_fill='#ffffff',
|
| 354 |
+
input_background_fill_dark='#1d1d1f',
|
| 355 |
+
input_border_width='1px',
|
| 356 |
+
input_border_color='#d2d2d7',
|
| 357 |
+
input_border_color_dark='#424245',
|
| 358 |
+
input_shadow='none',
|
| 359 |
+
input_shadow_focus='0 0 0 4px rgba(0, 113, 227, 0.15)',
|
| 360 |
+
),
|
| 361 |
+
css=apple_css,
|
| 362 |
+
)
|