import torch import spaces import gradio as gr from diffusers import DiffusionPipeline from PIL import Image import numpy as np # Load the pipeline once at startup print("Loading Z-Image-Turbo pipeline...") pipe = DiffusionPipeline.from_pretrained( "Tongyi-MAI/Z-Image-Turbo", torch_dtype=torch.bfloat16, low_cpu_mem_usage=False, ) pipe.to("cuda") # AoTI compilation + FA3 pipe.transformer.layers._repeated_blocks = ["ZImageTransformerBlock"]] spaces.aoti_blocks_load(pipe.transformer.layers, "zerogpu-aoti/Z-Image", variant="fa3") print("Pipeline loaded!") @spaces.GPU def generate_image(prompt, height, width, num_inference_steps, seed, randomize_seed, init_image=None, strength=0.75): """Generate an image from the given prompt.""" if randomize_seed: seed = torch.randint(0, 2**32 - 1, (1,)).item() generator = torch.Generator("cuda").manual_seed(int(seed))) if init_image is not None: # Convert to PIL if it's a numpy array if isinstance(init_image, np.ndarray): init_image = Image.fromarray(init_image) image = pipe( prompt=prompt, height=int(height), width=int(width), num_inference_steps=int(num_inference_steps), guidance_scale=0.0, generator=generator, image=init_image, strength=strength, ).images[0] else: image = pipe( prompt=prompt, height=int(height), width=int(width), num_inference_steps=int(num_inference_steps), guidance_scale=0.0, generator=generator, ).images[0] return image, seed # Example prompts examples = [ ["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."], ["A majestic dragon soaring through clouds at sunset, scales shimmering with iridescent colors, detailed fantasy art style"], ["A cozy coffee shop interior, warm lighting, rain on windows, plants on shelves, vintage aesthetic, photorealistic"], ["Portrait of a wise old wizard with a long white beard, holding a glowing crystal staff, magical forest background"], ] # Build the Gradio interface with gr.Blocks() as demo: # Header gr.Markdown( """ # 🎨 Z-Image-Turbo **Ultra-fast AI image generation** • Generate stunning images in just 8 steps """, elem_classes="header-text" ) with gr.Tabs(): with gr.TabItem("Text-to-Image"): with gr.Row(equal_height=False): # Left column - Input controls with gr.Column(scale=1, min_width=320): prompt = gr.Textbox( label="✨ Your Prompt", placeholder="Describe the image you want to create...", lines=5, max_lines=10, autofocus=True, ) with gr.Accordion("⚙️ Advanced Settings", open=False): with gr.Row(): height = gr.Slider( minimum=512, maximum=2048, value=1024, ) width = gr.Slider( minimum=512, maximum=2048, value=1024, ) with gr.Row(): num_inference_steps = gr.Slider( minimum=1, maximum=20, value=9, ) with gr.Row(): randomize_seed = gr.Checkbox( label="🎲 Random Seed", value=True, ) seed = gr.Number( label="Seed", value=42, precision=0, visible=False, ) def toggle_seed(randomize): return gr.Number(visible=not randomize) randomize_seed.change( toggle_seed, inputs=[randomize_seed], outputs=[seed] ) generate_btn = gr.Button( "🚀 Generate Image", variant="primary", size="lg", scale=1 ) # Example prompts gr.Examples( examples=examples, inputs=[prompt], label="💡 Try these prompts", examples_per_page=5, ) # Right column - Output with gr.Column(scale=1, min_width=320): output_image = gr.Image( label="Generated Image", type="pil", height=600, show_label=False, buttons=["download", "share"], ) with gr.TabItem("Image-to-Image"): with gr.Row(equal_height=False): with gr.Column(scale=1, min_width=320): prompt = gr.Textbox( label="✨ Your Prompt", placeholder="Describe the modifications you want to make...", lines=3, ) with gr.Column(scale=1, min_width=320): init_image = gr.Image( label="🖼 Upload Source Image", type="pil", height=300, ) strength = gr.Slider( minimum=0.1, maximum=1.0, value=0.75, step=0.05, label="Edit Strength", info="How much to modify the original image (0.1=subtle, 1.0=complete transformation)", ) img2img_btn = gr.Button( "🎨 Transform Image", variant="primary", size="lg", ) with gr.Row(): init_image_output = gr.Image( label="Transformed Image", type="pil", height=300, buttons=["download", "share"], ) # Footer credits gr.Markdown( """ ---
Model: Tongyi-MAI/Z-Image-Turbo (Apache 2.0 License) • Demo by: @mrfakenameRedesign by: AnyCoder • Optimizations: @multimodalart (FA3 + AoTI)
""", elem_classes="footer-text" ) # Connect the generate button for text-to-image generate_btn.click( fn=generate_image, inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed], outputs=[output_image, seed], api_visibility="public" ) # Connect the image-to-image button img2img_btn.click( fn=generate_image, inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed, init_image, strength], outputs=[output_image, seed], api_visibility="public" ) # Also allow generating by pressing Enter in the prompt box prompt.submit( fn=generate_image, inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed, init_image, strength], outputs=[output_image, seed], api_visibility="public" ) if __name__ == "__main__": demo.launch( theme=gr.themes.Soft( primary_hue="yellow", secondary_hue="amber", neutral_hue="slate", font=gr.themes.GoogleFont("Inter"), text_size="lg", spacing_size="md", radius_size="lg" ), css=""" .header-text h1 { font-size: 2.5rem !important; font-weight: 700 !important; margin-bottom: 0.5rem !important; background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; } .header-text p { font-size: 1.1rem !important; color: #64748b !important; margin-top: 0 !important; } .footer-text { padding: 1rem 0; } .footer-text a { color: #f59e0b !important; text-decoration: none !important; font-weight: 500; } .footer-text a:hover { text-decoration: underline !important; } /* Mobile optimizations */ @media (max-width: 768px) { .header-text h1 { font-size: 1.8rem !important; } .header-text p { font-size: 1rem !important; } /* Smooth transitions */ button, .gr-button { transition: all 0.2s ease !important; } button:hover, .gr-button:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15) !important; } /* Better spacing */ .gradio-container { max-width: 1400px !important; margin: 0 auto !important; } """, footer_links=[ {"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"], "api", "gradio" ] )