Spaces:
Runtime error
Runtime error
File size: 7,191 Bytes
c5840db | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | import spaces
import json
import logging
import os
import random
import re
import sys
import warnings
from PIL import Image
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from diffusers import (
AutoencoderKL,
FlowMatchEulerDiscreteScheduler,
ZImagePipeline
)
from diffusers.models.transformers.transformer_z_image import ZImageTransformer2DModel
# Environment setup
os.environ["TOKENIZERS_PARALLELISM"] = "false"
warnings.filterwarnings("ignore")
logging.getLogger("transformers").setLevel(logging.ERROR)
MODEL_PATH = os.environ.get("MODEL_PATH", "Tongyi-MAI/Z-Image-Turbo")
ENABLE_COMPILE = os.environ.get("ENABLE_COMPILE", "true").lower() == "true"
# Resolution options
RESOLUTION_OPTIONS = {
"1024": [
"1024x1024 (1:1)", "1152x896 (9:7)", "896x1152 (7:9)",
"1152x864 (4:3)", "864x1152 (3:4)", "1248x832 (3:2)",
"832x1248 (2:3)", "1280x720 (16:9)", "720x1280 (9:16)", "1344x576 (21:9)", "576x1344 (9:21)"
],
"1280": [
"1280x1280 (1:1)", "1440x1120 (9:7)", "1120x1440 (7:9)"
],
"1536": [
"1536x1536 (1:1)", "1728x1344 (9:7)", "1344x1728 (7:9)",
"1728x1296 (4:3)", "1296x1728 (3:4)", "1872x1248 (3:2)", "1248x1872 (2:3)",
"2048x1152 (16:9)", "1152x2048 (9:16)", "2016x864 (21:9)", "864x2016 (9:21)"
]
}
RESOLUTION_SET = []
for resolutions in RESOLUTION_OPTIONS.values():
RESOLUTION_SET.extend(resolutions)
EXAMPLE_PROMPTS = [
"一位男士和他的贵宾犬穿着配套的服装参加狗狗秀,室内灯光,背景中有观众。",
"极具氛围感的暗调人像,一位优雅的中国美女在黑暗的房间里。",
"一张中景手机自拍照片拍摄了一位留着长黑发的年轻东亚女子在灯光明亮的电梯内对着镜子自拍。",
]
# Model loading function
def load_model(model_path, enable_compile=False):
print(f"Loading model from {model_path}...")
# Simplified model loading logic
vae = AutoencoderKL.from_pretrained(
f"{model_path}",
subfolder="vae",
torch_dtype=torch.bfloat16,
device_map="cuda",
)
text_encoder = AutoModelForCausalLM.from_pretrained(
f"{model_path}",
subfolder="text_encoder",
torch_dtype=torch.bfloat16,
device_map="cuda",
).eval()
tokenizer = AutoTokenizer.from_pretrained(f"{model_path}", subfolder="tokenizer"))
# Initialize pipeline
pipe = ZImagePipeline(
vae=vae,
text_encoder=text_encoder,
tokenizer=tokenizer,
)
# Load transformer
transformer = ZImageTransformer2DModel.from_pretrained(
f"{model_path}",
subfolder="transformer",
)
pipe.transformer = transformer
pipe.to("cuda", torch.bfloat16)
return pipe
# Image generation function
@spaces.GPU
def generate_image(
pipe,
prompt,
resolution="1024x1024 (1:1)",
seed=42,
guidance_scale=5.0,
num_inference_steps=50,
progress=gr.Progress(track_tqdm=True),
):
"""Generate image using Z-Image model"""
width, height = 1024, 1024 # Default resolution
# Parse resolution string
match = re.search(r"(\d+)\s*[×x]\s*(\d+)", resolution)
if match:
width, height = int(match.group(1))), int(match.group(2)))
generator = torch.Generator("cuda").manual_seed(seed)
scheduler = FlowMatchEulerDiscreteScheduler(
num_train_timesteps=1000,
shift=3.0
)
pipe.scheduler = scheduler
# Generate image
image = pipe(
prompt=prompt,
height=height,
width=width,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
generator=generator,
).images[0]
return image
# Initialize the model
pipe = None
try:
pipe = load_model(MODEL_PATH, enable_compile=ENABLE_COMPILE)
print("Model loaded successfully")
except Exception as e:
print(f"Error loading model: {e}")
# Main application
with gr.Blocks(
title="Z-Image Turbo",
theme=gr.themes.Soft(),
footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"]
) as demo:
# Header section
with gr.Row():
gr.Markdown("""
# Z-Image Turbo
*Efficient Image Generation with Single-Stream Diffusion Transformer*
""")
# Main content area
with gr.Row():
with gr.Column(scale=1):
# Prompt input
prompt_input = gr.Textbox(
label="Describe your image",
placeholder="Enter a detailed description of what you want to generate...",
lines=3
)
# Settings in accordion
with gr.Accordion("⚙️ Advanced Settings", open=False):
with gr.Row():
resolution_dropdown = gr.Dropdown(
choices=RESOLUTION_SET,
value="1024x1024 (1:1)",
label="Resolution"
)
seed_input = gr.Number(
label="Seed",
value=42,
precision=0
)
random_seed_check = gr.Checkbox(
label="Use random seed",
value=True
)
# Generate button
generate_btn = gr.Button(
"Generate Image 🎨",
variant="primary",
size="lg"
)
# Examples
gr.Examples(
examples=EXAMPLE_PROMPTS,
inputs=prompt_input,
label="Try these examples:"
)
with gr.Column(scale=1):
# Output gallery
output_gallery = gr.Gallery(
label="Generated Images",
columns=2,
height=500
)
# Generation handler
def handle_generation(prompt, resolution, seed, use_random_seed):
if not prompt.strip():
raise gr.Error("Please enter a prompt")
if use_random_seed:
actual_seed = random.randint(1, 1000000)
else:
actual_seed = int(seed) if seed != -1 else random.randint(1, 1000000)
# Generate image
image = generate_image(
pipe=pipe,
prompt=prompt,
resolution=resolution,
seed=actual_seed,
)
return [image], str(actual_seed), actual_seed
generate_btn.click(
fn=handle_generation,
inputs=[prompt_input, resolution_dropdown, seed_input, random_seed_check],
outputs=[output_gallery, gr.Textbox(label="Seed Used"), gr.Number(label="Seed Value")],
api_visibility="public"
)
# Mobile optimization CSS
css = """
.gradio-container {
max-width: 100% !important;
padding: 10px !important;
}
.mobile-optimized {
min-height: 400px !important;
}
"""
demo.launch(
css=css,
mcp_server=True
) |