Spaces:
Running
on
Zero
Running
on
Zero
Production-Ready v2.0.0 - 1765466791
Browse filesFixed all runtime errors
Modular architecture
Caching system
Monitoring
app.py
CHANGED
|
@@ -1,808 +1,1229 @@
|
|
| 1 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import os
|
|
|
|
| 4 |
import time
|
| 5 |
-
import
|
| 6 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
-
import
|
| 9 |
-
import
|
| 10 |
-
import base64
|
| 11 |
-
from typing import Tuple, Optional, Union
|
| 12 |
from PIL import Image
|
| 13 |
-
|
| 14 |
-
from
|
| 15 |
-
from
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
logger = logging.getLogger(__name__)
|
| 26 |
|
| 27 |
-
# Performance cache
|
| 28 |
-
class SimpleCache:
|
| 29 |
-
def __init__(self, size=100):
|
| 30 |
-
self.cache = {}
|
| 31 |
-
self.size = size
|
| 32 |
-
|
| 33 |
-
def get(self, key):
|
| 34 |
-
return self.cache.get(key)
|
| 35 |
-
|
| 36 |
-
def set(self, key, value):
|
| 37 |
-
if len(self.cache) >= self.size:
|
| 38 |
-
# Remove oldest entry
|
| 39 |
-
oldest = next(iter(self.cache))
|
| 40 |
-
del self.cache[oldest]
|
| 41 |
-
self.cache[key] = value
|
| 42 |
-
|
| 43 |
-
# Initialize caches
|
| 44 |
-
prompt_cache = SimpleCache(100)
|
| 45 |
-
image_cache = SimpleCache(50)
|
| 46 |
-
|
| 47 |
-
def get_deepseek_client():
|
| 48 |
-
"""Get DeepSeek API client."""
|
| 49 |
-
api_key = os.environ.get("DEEPSEEK_API_KEY")
|
| 50 |
-
if not api_key:
|
| 51 |
-
return None
|
| 52 |
-
return OpenAI(
|
| 53 |
-
base_url="https://api.deepseek.com/",
|
| 54 |
-
api_key=api_key,
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
def polish_prompt(original_prompt: str, mode: str = "generate") -> str:
|
| 58 |
-
"""Expand short prompts with caching."""
|
| 59 |
-
if not original_prompt or not original_prompt.strip():
|
| 60 |
-
return "Ultra HD, 4K, cinematic composition, highly detailed" if mode == "generate" else "high quality, enhanced details"
|
| 61 |
-
|
| 62 |
-
# Check cache
|
| 63 |
-
cache_key = f"{mode}:{original_prompt}"
|
| 64 |
-
cached = prompt_cache.get(cache_key)
|
| 65 |
-
if cached:
|
| 66 |
-
return cached
|
| 67 |
-
|
| 68 |
-
# For now, basic enhancement (can be replaced with actual API call)
|
| 69 |
-
if mode == "generate":
|
| 70 |
-
enhanced = f"{original_prompt}, highly detailed, professional quality, 8k resolution"
|
| 71 |
-
else:
|
| 72 |
-
enhanced = f"Transform into {original_prompt}, artistic style, enhanced details"
|
| 73 |
-
|
| 74 |
-
# Cache result
|
| 75 |
-
prompt_cache.set(cache_key, enhanced)
|
| 76 |
-
return enhanced
|
| 77 |
-
|
| 78 |
-
print("Loading Z-Image-Turbo pipeline with optimizations...")
|
| 79 |
-
|
| 80 |
-
# Load pipeline with optimizations (FIXED: removed variant="fp16")
|
| 81 |
-
try:
|
| 82 |
-
pipe_t2i = DiffusionPipeline.from_pretrained(
|
| 83 |
-
"Tongyi-MAI/Z-Image-Turbo",
|
| 84 |
-
torch_dtype=torch.bfloat16,
|
| 85 |
-
use_safetensors=True
|
| 86 |
-
# REMOVED: variant="fp16" - this was causing the error
|
| 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 |
STYLES = ["None", "Photorealistic", "Cinematic", "Anime", "Digital Art",
|
| 125 |
"Oil Painting", "Watercolor", "3D Render", "Fantasy", "Sci-Fi"]
|
| 126 |
|
| 127 |
-
STYLE_SUFFIXES = {
|
| 128 |
-
"None": "",
|
| 129 |
-
"Photorealistic": ", photorealistic, ultra detailed, 8k, professional photography, sharp focus",
|
| 130 |
-
"Cinematic": ", cinematic lighting, movie scene, dramatic atmosphere, film grain, anamorphic lens flare",
|
| 131 |
-
"Anime": ", anime style, vibrant colors, cel shaded, studio ghibli inspired, detailed line art",
|
| 132 |
-
"Digital Art": ", digital art, artstation trending, concept art, highly detailed, octane render",
|
| 133 |
-
"Oil Painting": ", oil painting style, classical art, visible brush strokes, rich textures, canvas",
|
| 134 |
-
"Watercolor": ", watercolor painting, soft edges, artistic, delicate colors, wet on wet technique",
|
| 135 |
-
"3D Render": ", 3D render, octane render, unreal engine 5, ray tracing, subsurface scattering",
|
| 136 |
-
"Fantasy": ", fantasy art, magical, ethereal glow, mystical atmosphere, epic composition",
|
| 137 |
-
"Sci-Fi": ", science fiction, futuristic, advanced technology, neon accents, cyberpunk",
|
| 138 |
-
}
|
| 139 |
-
|
| 140 |
RATIOS = [
|
| 141 |
-
"1:1 Square (1024x1024)",
|
| 142 |
-
"
|
| 143 |
-
"
|
| 144 |
-
"
|
| 145 |
-
"1:1 XL (1536x1536)", "16:9 XL (1920x1088)",
|
| 146 |
-
"9:16 XL (1088x1920)", "4:3 XL (1536x1152)",
|
| 147 |
-
"3:4 XL (1152x1536)", "1:1 MAX (2048x2048)",
|
| 148 |
-
"16:9 MAX (2048x1152)", "9:16 MAX (1152x2048)"
|
| 149 |
]
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
"16:9 Landscape (1344x768)": (1344, 768),
|
| 154 |
-
"9:16 Portrait (768x1344)": (768, 1344),
|
| 155 |
-
"4:3 Standard (1152x896)": (1152, 896),
|
| 156 |
-
"3:4 Vertical (896x1152)": (896, 1152),
|
| 157 |
-
"21:9 Cinematic (1536x640)": (1536, 640),
|
| 158 |
-
"3:2 Photo (1216x832)": (1216, 832),
|
| 159 |
-
"2:3 Photo Portrait (832x1216)": (832, 1216),
|
| 160 |
-
"1:1 XL (1536x1536)": (1536, 1536),
|
| 161 |
-
"16:9 XL (1920x1088)": (1920, 1088),
|
| 162 |
-
"9:16 XL (1088x1920)": (1088, 1920),
|
| 163 |
-
"4:3 XL (1536x1152)": (1536, 1152),
|
| 164 |
-
"3:4 XL (1152x1536)": (1152, 1536),
|
| 165 |
-
"1:1 MAX (2048x2048)": (2048, 2048),
|
| 166 |
-
"16:9 MAX (2048x1152)": (2048, 1152),
|
| 167 |
-
"9:16 MAX (1152x2048)": (1152, 2048),
|
| 168 |
-
}
|
| 169 |
-
|
| 170 |
-
def optimize_steps(prompt: str, base_steps: int) -> int:
|
| 171 |
-
"""Optimize steps based on prompt complexity"""
|
| 172 |
-
complexity = len(prompt.split()) + prompt.count(',') + prompt.count('.')
|
| 173 |
-
|
| 174 |
-
if complexity < 10:
|
| 175 |
-
return max(4, base_steps - 2) # Simple prompts need fewer steps
|
| 176 |
-
elif complexity > 30:
|
| 177 |
-
return min(16, base_steps + 2) # Complex prompts need more steps
|
| 178 |
-
else:
|
| 179 |
-
return base_steps
|
| 180 |
-
|
| 181 |
-
@spaces.GPU(duration=120)
|
| 182 |
-
def generate(prompt: str, style: str, ratio: str, steps: int, seed: int, randomize: bool, progress=gr.Progress(track_tqdm=True)) -> Tuple[Optional[Image.Image], int]:
|
| 183 |
-
"""Optimized generation function"""
|
| 184 |
-
start_time = time.time()
|
| 185 |
-
|
| 186 |
-
try:
|
| 187 |
-
if randomize:
|
| 188 |
-
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 189 |
-
seed = int(seed)
|
| 190 |
-
|
| 191 |
-
if not prompt or not prompt.strip():
|
| 192 |
-
return None, seed
|
| 193 |
-
|
| 194 |
-
# Generate cache key
|
| 195 |
-
cache_key = f"gen:{prompt}:{style}:{ratio}:{steps}:{seed}"
|
| 196 |
-
|
| 197 |
-
# Check cache first
|
| 198 |
-
cached_result = image_cache.get(cache_key)
|
| 199 |
-
if cached_result:
|
| 200 |
-
logger.info("Returning cached result")
|
| 201 |
-
return cached_result, seed
|
| 202 |
-
|
| 203 |
-
# Polish prompt
|
| 204 |
-
polished = polish_prompt(prompt.strip(), mode="generate")
|
| 205 |
-
final_prompt = polished + STYLE_SUFFIXES.get(style, "")
|
| 206 |
-
|
| 207 |
-
w, h = RATIO_DIMS.get(ratio, (1024, 1024))
|
| 208 |
-
|
| 209 |
-
generator = torch.Generator("cuda").manual_seed(seed)
|
| 210 |
-
|
| 211 |
-
# Optimize steps
|
| 212 |
-
optimized_steps = optimize_steps(final_prompt, steps)
|
| 213 |
-
|
| 214 |
-
# Clear cache before generation
|
| 215 |
-
torch.cuda.empty_cache()
|
| 216 |
-
|
| 217 |
-
logger.info(f"Generating: {w}x{h}, {optimized_steps} steps")
|
| 218 |
-
|
| 219 |
-
image = pipe_t2i(
|
| 220 |
-
prompt=final_prompt,
|
| 221 |
-
height=h,
|
| 222 |
-
width=w,
|
| 223 |
-
num_inference_steps=optimized_steps,
|
| 224 |
-
guidance_scale=0.0,
|
| 225 |
-
generator=generator,
|
| 226 |
-
output_type="pil"
|
| 227 |
-
).images[0]
|
| 228 |
-
|
| 229 |
-
# Cache the result
|
| 230 |
-
image_cache.set(cache_key, image)
|
| 231 |
-
|
| 232 |
-
gen_time = time.time() - start_time
|
| 233 |
-
logger.info(f"✅ Generated in {gen_time:.2f}s")
|
| 234 |
-
|
| 235 |
-
return image, seed
|
| 236 |
-
|
| 237 |
-
except Exception as e:
|
| 238 |
-
logger.error(f"Generation failed: {e}")
|
| 239 |
-
return None, seed
|
| 240 |
-
|
| 241 |
-
@spaces.GPU(duration=120)
|
| 242 |
-
def transform(input_image: Optional[Image.Image], prompt: str, style: str, strength: float, steps: int, seed: int, randomize: bool, progress=gr.Progress(track_tqdm=True)) -> Tuple[Optional[Image.Image], int]:
|
| 243 |
-
"""Optimized transform function"""
|
| 244 |
-
try:
|
| 245 |
-
if input_image is None:
|
| 246 |
-
return None, 0
|
| 247 |
-
|
| 248 |
-
if randomize:
|
| 249 |
-
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 250 |
-
seed = int(seed)
|
| 251 |
-
|
| 252 |
-
if not prompt or not prompt.strip():
|
| 253 |
-
prompt = "high quality image, enhanced details"
|
| 254 |
-
|
| 255 |
-
# Preprocess image
|
| 256 |
-
input_image = input_image.convert("RGB")
|
| 257 |
-
w, h = input_image.size
|
| 258 |
-
w = (w // 16) * 16
|
| 259 |
-
h = (h // 16) * 16
|
| 260 |
-
w = max(512, min(2048, w))
|
| 261 |
-
h = max(512, min(2048, h))
|
| 262 |
-
input_image = input_image.resize((w, h), Image.LANCZOS)
|
| 263 |
-
|
| 264 |
-
# Polish prompt
|
| 265 |
-
polished = polish_prompt(prompt.strip(), mode="transform")
|
| 266 |
-
final_prompt = polished + STYLE_SUFFIXES.get(style, "")
|
| 267 |
-
|
| 268 |
-
strength = float(strength)
|
| 269 |
-
effective_steps = max(4, int(steps / strength)) if strength > 0 else int(steps)
|
| 270 |
-
|
| 271 |
-
generator = torch.Generator("cuda").manual_seed(seed)
|
| 272 |
-
|
| 273 |
-
# Clear cache
|
| 274 |
-
torch.cuda.empty_cache()
|
| 275 |
-
|
| 276 |
-
logger.info(f"Transforming: strength={strength}, steps={effective_steps}")
|
| 277 |
-
|
| 278 |
-
image = pipe_i2i(
|
| 279 |
-
prompt=final_prompt,
|
| 280 |
-
image=input_image,
|
| 281 |
-
strength=strength,
|
| 282 |
-
num_inference_steps=effective_steps,
|
| 283 |
-
guidance_scale=0.0,
|
| 284 |
-
generator=generator,
|
| 285 |
-
output_type="pil"
|
| 286 |
-
).images[0]
|
| 287 |
-
|
| 288 |
-
return image, seed
|
| 289 |
-
|
| 290 |
-
except Exception as e:
|
| 291 |
-
logger.error(f"Transform failed: {e}")
|
| 292 |
-
return None, seed
|
| 293 |
-
|
| 294 |
-
# Enhanced CSS with performance optimizations
|
| 295 |
-
css = """
|
| 296 |
:root {
|
| 297 |
-
--primary
|
| 298 |
-
--dark
|
| 299 |
-
--
|
| 300 |
-
--
|
| 301 |
-
--
|
| 302 |
-
--
|
| 303 |
-
--
|
| 304 |
-
--
|
| 305 |
-
--shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
| 306 |
-
--shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
| 307 |
--border-radius: 12px;
|
| 308 |
-
--
|
| 309 |
-
}
|
| 310 |
-
|
| 311 |
-
/* Global optimizations */
|
| 312 |
-
* {
|
| 313 |
-
box-sizing: border-box;
|
| 314 |
}
|
| 315 |
|
| 316 |
-
/*
|
| 317 |
.gradio-container {
|
| 318 |
-
font-family:
|
| 319 |
-
background:
|
| 320 |
-
min-height: 100vh;
|
| 321 |
-
padding: 1rem;
|
| 322 |
-
}
|
| 323 |
-
|
| 324 |
-
/* Enhanced button styling */
|
| 325 |
-
button {
|
| 326 |
-
border-radius: var(--border-radius);
|
| 327 |
-
font-weight: 600;
|
| 328 |
-
transition: var(--transition);
|
| 329 |
-
position: relative;
|
| 330 |
-
overflow: hidden;
|
| 331 |
-
}
|
| 332 |
-
|
| 333 |
-
button.primary {
|
| 334 |
-
background: linear-gradient(135deg, var(--primary-blue) 0%, var(--dark-blue) 100%);
|
| 335 |
-
color: white;
|
| 336 |
-
border: none;
|
| 337 |
-
padding: 0.75rem 2rem;
|
| 338 |
-
box-shadow: var(--shadow-md);
|
| 339 |
-
}
|
| 340 |
-
|
| 341 |
-
button.primary:hover {
|
| 342 |
-
transform: translateY(-2px);
|
| 343 |
-
box-shadow: var(--shadow-lg);
|
| 344 |
}
|
| 345 |
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
color:
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
button.secondary:hover {
|
| 354 |
-
background: var(--primary-blue);
|
| 355 |
-
color: white;
|
| 356 |
}
|
| 357 |
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
|
|
|
|
|
|
| 361 |
}
|
| 362 |
|
| 363 |
-
|
| 364 |
-
|
| 365 |
border-radius: var(--border-radius);
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
box-shadow: var(--shadow-sm);
|
| 369 |
-
}
|
| 370 |
-
|
| 371 |
-
.tab-nav > button {
|
| 372 |
-
background: transparent;
|
| 373 |
-
color: var(--dark-blue);
|
| 374 |
-
border-radius: 8px;
|
| 375 |
-
padding: 0.75rem 1.5rem;
|
| 376 |
-
font-weight: 500;
|
| 377 |
-
transition: var(--transition);
|
| 378 |
}
|
| 379 |
|
| 380 |
-
.
|
| 381 |
-
background:
|
|
|
|
| 382 |
}
|
| 383 |
|
| 384 |
-
.
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
box-shadow: var(--shadow-sm);
|
| 389 |
}
|
| 390 |
|
| 391 |
-
/*
|
| 392 |
-
.
|
| 393 |
-
|
| 394 |
-
border-radius: var(--border-radius);
|
| 395 |
-
|
| 396 |
-
backdrop-filter: blur(10px);
|
| 397 |
-
border: 1px solid rgba(255, 255, 255, 0.5);
|
| 398 |
}
|
| 399 |
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
|
|
|
| 403 |
}
|
| 404 |
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
border: 2px solid #e5e7eb;
|
| 409 |
-
border-radius: 8px;
|
| 410 |
-
transition: var(--transition);
|
| 411 |
}
|
| 412 |
|
| 413 |
-
.
|
| 414 |
-
|
| 415 |
-
|
| 416 |
}
|
| 417 |
|
| 418 |
-
/*
|
| 419 |
-
.
|
|
|
|
|
|
|
| 420 |
border-radius: var(--border-radius);
|
| 421 |
-
|
| 422 |
-
box-shadow: var(--shadow-md);
|
| 423 |
}
|
| 424 |
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
background: linear-gradient(135deg, var(--primary-blue) 0%, var(--dark-blue) 100%);
|
| 428 |
-
-webkit-background-clip: text;
|
| 429 |
-
-webkit-text-fill-color: transparent;
|
| 430 |
font-weight: 700;
|
| 431 |
-
|
| 432 |
-
margin-bottom: 0.5rem;
|
| 433 |
}
|
| 434 |
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
border-radius: var(--border-radius);
|
| 440 |
-
padding: 1rem;
|
| 441 |
-
margin-top: 2rem;
|
| 442 |
}
|
| 443 |
|
| 444 |
-
/*
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
}
|
| 449 |
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
scroll-behavior: smooth;
|
| 453 |
-
}
|
| 454 |
-
|
| 455 |
-
/* Loading animation */
|
| 456 |
-
.loading-spinner {
|
| 457 |
-
animation: spin 1s linear infinite;
|
| 458 |
-
}
|
| 459 |
-
|
| 460 |
-
@keyframes spin {
|
| 461 |
-
from { transform: rotate(0deg); }
|
| 462 |
-
to { transform: rotate(360deg); }
|
| 463 |
}
|
| 464 |
|
| 465 |
/* Responsive design */
|
| 466 |
@media (max-width: 768px) {
|
| 467 |
-
.gradio-
|
| 468 |
-
|
| 469 |
-
}
|
| 470 |
-
|
| 471 |
-
h1 {
|
| 472 |
-
font-size: 2rem;
|
| 473 |
-
}
|
| 474 |
-
|
| 475 |
-
button.primary {
|
| 476 |
-
padding: 0.5rem 1.5rem;
|
| 477 |
-
font-size: 0.9rem;
|
| 478 |
}
|
| 479 |
}
|
| 480 |
"""
|
| 481 |
|
| 482 |
-
# Create enhanced interface
|
| 483 |
-
with gr.Blocks(
|
| 484 |
-
title="Z Image Turbo - Enhanced & Optimized",
|
| 485 |
-
css=css,
|
| 486 |
-
theme=gr.themes.Soft(),
|
| 487 |
-
analytics_enabled=False
|
| 488 |
-
) as demo:
|
| 489 |
-
gr.HTML("""
|
| 490 |
-
<div style="text-align: center; padding: 2rem 1rem 1rem 1rem;">
|
| 491 |
-
<h1 style="color: #1e40af; font-size: 2.5rem; margin-bottom: 0.5rem;">⚡ Z Image Turbo Enhanced</h1>
|
| 492 |
-
<p style="color: #4b5563; font-size: 1.2rem; margin: 0;">
|
| 493 |
-
Ultra-fast AI image generation with advanced optimizations
|
| 494 |
-
</p>
|
| 495 |
-
<p style="color: #10b981; font-size: 1rem; margin-top: 0.5rem;">
|
| 496 |
-
✓ 50-70% faster • ✓ Smart caching • ✓ Auto-optimized steps • ✓ Enhanced UI
|
| 497 |
-
</p>
|
| 498 |
-
<p style="color: #ec4899; font-size: 0.95em; margin-top: 1rem;">
|
| 499 |
-
If you enjoy using this space, please give it a ❤️ Thank you!
|
| 500 |
-
</p>
|
| 501 |
-
</div>
|
| 502 |
-
""")
|
| 503 |
-
|
| 504 |
-
with gr.Tabs(elem_classes=["main-tabs"]):
|
| 505 |
-
# Generate Tab
|
| 506 |
-
with gr.Tab("🎨 Generate", elem_classes=["generate-tab"]):
|
| 507 |
-
gr.Markdown("""
|
| 508 |
-
### Create stunning images from text descriptions
|
| 509 |
-
|
| 510 |
-
**Tips:**
|
| 511 |
-
- Be descriptive about subjects, lighting, and style
|
| 512 |
-
- Use the ✨ Smart Enhancement feature for better results
|
| 513 |
-
- Try different aspect ratios for various use cases
|
| 514 |
-
""")
|
| 515 |
-
|
| 516 |
-
with gr.Row():
|
| 517 |
-
with gr.Column(scale=1):
|
| 518 |
-
gen_prompt = gr.Textbox(
|
| 519 |
-
label="Prompt",
|
| 520 |
-
placeholder="A majestic dragon perched atop a crystal mountain at sunset, digital art style...",
|
| 521 |
-
lines=3,
|
| 522 |
-
max_lines=5,
|
| 523 |
-
info="Describe what you want to create"
|
| 524 |
-
)
|
| 525 |
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
)
|
|
|
|
| 532 |
gen_style = gr.Dropdown(
|
| 533 |
choices=STYLES,
|
| 534 |
value="None",
|
| 535 |
-
label="Style
|
| 536 |
-
info="Apply
|
| 537 |
)
|
| 538 |
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
info="Choose the image dimensions"
|
| 544 |
-
)
|
| 545 |
-
|
| 546 |
-
gen_steps = gr.Slider(
|
| 547 |
-
minimum=4,
|
| 548 |
-
maximum=16,
|
| 549 |
-
value=9,
|
| 550 |
-
step=1,
|
| 551 |
-
label="Inference Steps",
|
| 552 |
-
info="Auto-optimized based on prompt complexity",
|
| 553 |
-
interactive=True
|
| 554 |
-
)
|
| 555 |
-
|
| 556 |
-
with gr.Row():
|
| 557 |
-
gen_seed = gr.Number(
|
| 558 |
-
label="Seed",
|
| 559 |
-
value=42,
|
| 560 |
-
precision=0,
|
| 561 |
-
info="Reproducible results with the same seed"
|
| 562 |
-
)
|
| 563 |
-
gen_randomize = gr.Checkbox(
|
| 564 |
-
label="Random Seed",
|
| 565 |
-
value=True,
|
| 566 |
-
info="Generate unique images each time"
|
| 567 |
)
|
| 568 |
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
)
|
| 587 |
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
|
|
|
| 593 |
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
["Cyberpunk cityscape at night with neon lights and flying vehicles", "Digital Art", "21:9 Cinematic (1536x640)", 9, 42],
|
| 599 |
-
["A cozy cabin in the woods with warm lights coming from windows", "Oil Painting", "4:3 Standard (1152x896)", 9, 42],
|
| 600 |
-
["Magical unicorn in an enchanted forest with glowing particles", "Fantasy", "1:1 Square (1024x1024)", 9, 42],
|
| 601 |
-
["Futuristic spaceship approaching an alien planet with two moons", "Sci-Fi", "16:9 MAX (2048x1152)", 9, 42],
|
| 602 |
-
],
|
| 603 |
-
inputs=[gen_prompt, gen_style, gen_ratio, gen_steps, gen_seed],
|
| 604 |
-
label="Click to use example"
|
| 605 |
)
|
| 606 |
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 612 |
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
Upload an image and describe how you want to transform it. Lower strength preserves more of the original, while higher strength creates dramatic changes.
|
| 619 |
-
""")
|
| 620 |
-
|
| 621 |
-
with gr.Row():
|
| 622 |
-
with gr.Column(scale=1):
|
| 623 |
-
trans_input = gr.Image(
|
| 624 |
-
label="Upload Image",
|
| 625 |
-
type="pil",
|
| 626 |
-
sources=["upload", "webcam", "clipboard"],
|
| 627 |
-
elem_classes=["upload-area"]
|
| 628 |
-
)
|
| 629 |
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
info="Describe how you want to transform the image"
|
| 636 |
-
)
|
| 637 |
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
label="Steps",
|
| 660 |
-
info="Number of inference steps"
|
| 661 |
-
)
|
| 662 |
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 669 |
)
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 673 |
)
|
| 674 |
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 679 |
)
|
| 680 |
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 689 |
)
|
| 690 |
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
|
| 696 |
-
with gr.Accordion("Transformation Examples", open=True):
|
| 697 |
-
gr.Examples(
|
| 698 |
-
examples=[
|
| 699 |
-
["Oil painting style with visible brush strokes", "Oil Painting", 0.8, 9],
|
| 700 |
-
["Anime style with vibrant colors", "Anime", 0.9, 9],
|
| 701 |
-
["Photorealistic with sharp details", "Photorealistic", 0.6, 9],
|
| 702 |
-
["Add cinematic lighting and mood", "Cinematic", 0.5, 9],
|
| 703 |
-
["Convert to watercolor painting", "Watercolor", 0.7, 9],
|
| 704 |
-
],
|
| 705 |
-
inputs=[trans_prompt, trans_style, trans_strength, trans_steps],
|
| 706 |
-
label="Click to apply example"
|
| 707 |
-
)
|
| 708 |
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
outputs=[trans_output, trans_seed_used]
|
| 713 |
-
)
|
| 714 |
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
- Image-to-Image transformation
|
| 734 |
-
- Multiple aspect ratios (up to 2048x2048)
|
| 735 |
-
- Style presets (Photorealistic, Anime, Oil Painting, etc.)
|
| 736 |
-
- Smart prompt enhancement
|
| 737 |
-
- Seed control for reproducible results
|
| 738 |
-
|
| 739 |
-
## 📊 Technical Stack
|
| 740 |
-
|
| 741 |
-
- **Model**: Tongyi-MAI/Z-Image-Turbo
|
| 742 |
-
- **Framework**: Diffusers + PyTorch
|
| 743 |
-
- **UI**: Gradio 4.20+
|
| 744 |
-
- **Hardware**: ZeroGPU optimization
|
| 745 |
-
|
| 746 |
-
## 🤝 Credits
|
| 747 |
-
|
| 748 |
-
- Base model: [Tongyi-MAI/Z-Image-Turbo](https://huggingface.co/Tongyi-MAI/Z-Image-Turbo)
|
| 749 |
-
- Enhancements by: [@lulavc](https://huggingface.co/lulavc)
|
| 750 |
-
|
| 751 |
-
## 💝 Support
|
| 752 |
-
|
| 753 |
-
If you find this space useful, please:
|
| 754 |
-
- Give it a ❤️
|
| 755 |
-
- Share it with others
|
| 756 |
-
- Report any issues or suggestions
|
| 757 |
-
""")
|
| 758 |
-
|
| 759 |
-
gr.HTML("""
|
| 760 |
-
<div style="text-align: center; margin-top: 2rem; padding: 1rem; background: rgba(59, 130, 246, 0.1); border-radius: 12px;">
|
| 761 |
-
<p style="margin: 0; color: #1e40af; font-weight: 600;">
|
| 762 |
-
Made with ❤️ by the community
|
| 763 |
-
</p>
|
| 764 |
-
</div>
|
| 765 |
-
""")
|
| 766 |
-
|
| 767 |
-
# Footer with enhanced styling
|
| 768 |
-
gr.HTML("""
|
| 769 |
-
<div style="
|
| 770 |
-
text-align: center;
|
| 771 |
-
width: 100%;
|
| 772 |
-
padding: 1.5rem;
|
| 773 |
-
margin-top: 2rem;
|
| 774 |
-
background: linear-gradient(135deg, rgba(59, 130, 246, 0.1) 0%, rgba(147, 197, 253, 0.1) 100%);
|
| 775 |
-
border-radius: 12px;
|
| 776 |
-
color: #1e3a5f;
|
| 777 |
-
border: 1px solid rgba(59, 130, 246, 0.2);
|
| 778 |
-
">
|
| 779 |
-
<p style="margin: 0; font-size: 0.9rem;">
|
| 780 |
-
<strong>Models:</strong>
|
| 781 |
-
<a href="https://huggingface.co/Tongyi-MAI/Z-Image-Turbo" target="_blank" style="color: #1d4ed8; font-weight: 600; text-decoration: none;">Z-Image-Turbo</a>
|
| 782 |
-
|
|
| 783 |
-
<strong>Performance:</strong>
|
| 784 |
-
<span style="color: #059669;">✓ 50-70% faster</span>
|
| 785 |
-
<span style="color: #059669;">✓ Smart caching</span>
|
| 786 |
-
<span style="color: #059669;">✓ Memory optimized</span>
|
| 787 |
-
<br><br>
|
| 788 |
-
<strong>Enhanced by:</strong>
|
| 789 |
-
<a href="https://huggingface.co/lulavc" target="_blank" style="color: #1d4ed8; font-weight: 600; text-decoration: none;">@lulavc</a>
|
| 790 |
-
</p>
|
| 791 |
-
</div>
|
| 792 |
-
""")
|
| 793 |
-
|
| 794 |
-
# Configure for optimal performance
|
| 795 |
-
demo.queue(
|
| 796 |
-
api_open=False,
|
| 797 |
-
max_size=20,
|
| 798 |
-
default_concurrency_limit=1
|
| 799 |
-
)
|
| 800 |
|
| 801 |
-
|
| 802 |
-
demo.launch(
|
| 803 |
-
share=False,
|
| 804 |
-
show_error=True,
|
| 805 |
-
max_threads=40,
|
| 806 |
-
prevent_thread_lock=False,
|
| 807 |
-
enable_queue=True
|
| 808 |
-
)
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Z Image Turbo - Production-Ready Application
|
| 3 |
+
===========================================
|
| 4 |
+
|
| 5 |
+
A robust, modular image generation and transformation application with:
|
| 6 |
+
- PyTorch 2.0+ compilation with graceful fallback
|
| 7 |
+
- xformers optimization with CPU fallback
|
| 8 |
+
- Comprehensive error handling and logging
|
| 9 |
+
- Clean separation of concerns
|
| 10 |
+
- Performance monitoring and caching
|
| 11 |
+
- Production-ready resource management
|
| 12 |
+
|
| 13 |
+
Author: AI Agent Framework Specialist
|
| 14 |
+
Version: 2.0.0 Production
|
| 15 |
+
"""
|
| 16 |
|
| 17 |
import os
|
| 18 |
+
import sys
|
| 19 |
import time
|
| 20 |
+
import asyncio
|
| 21 |
+
import hashlib
|
| 22 |
+
import logging
|
| 23 |
+
import traceback
|
| 24 |
+
import warnings
|
| 25 |
+
from typing import Optional, Tuple, Dict, Any, Union, List
|
| 26 |
+
from contextlib import asynccontextmanager
|
| 27 |
+
from dataclasses import dataclass
|
| 28 |
+
from enum import Enum
|
| 29 |
+
|
| 30 |
+
# Third-party imports
|
| 31 |
import gradio as gr
|
| 32 |
+
import torch
|
| 33 |
+
import numpy as np
|
|
|
|
|
|
|
| 34 |
from PIL import Image
|
| 35 |
+
import psutil
|
| 36 |
+
from functools import lru_cache
|
| 37 |
+
from datetime import datetime, timedelta
|
| 38 |
+
|
| 39 |
+
# Diffusers and model imports
|
| 40 |
+
from diffusers import DiffusionPipeline, StableDiffusionImg2ImgPipeline
|
| 41 |
+
from diffusers.utils import logging as diffusers_logging
|
| 42 |
+
from spaces import GPU
|
| 43 |
+
|
| 44 |
+
# Suppress noisy warnings
|
| 45 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
| 46 |
+
diffusers_logging.set_verbosity_error()
|
| 47 |
+
|
| 48 |
+
# Configure logging early
|
| 49 |
+
logging.basicConfig(
|
| 50 |
+
level=logging.INFO,
|
| 51 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 52 |
+
handlers=[
|
| 53 |
+
logging.StreamHandler(sys.stdout),
|
| 54 |
+
logging.FileHandler('z_image_turbo.log', mode='a')
|
| 55 |
+
]
|
| 56 |
+
)
|
| 57 |
logger = logging.getLogger(__name__)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
class ErrorCode(Enum):
|
| 61 |
+
"""Enumeration of application error codes"""
|
| 62 |
+
SUCCESS = 0
|
| 63 |
+
MODEL_LOAD_ERROR = 1001
|
| 64 |
+
GENERATION_ERROR = 1002
|
| 65 |
+
TRANSFORM_ERROR = 1003
|
| 66 |
+
INVALID_INPUT = 2001
|
| 67 |
+
RESOURCE_ERROR = 3001
|
| 68 |
+
NETWORK_ERROR = 4001
|
| 69 |
+
CACHE_ERROR = 5001
|
| 70 |
+
UNKNOWN_ERROR = 9999
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
@dataclass
|
| 74 |
+
class GenerationResult:
|
| 75 |
+
"""Data class for generation results"""
|
| 76 |
+
success: bool
|
| 77 |
+
image: Optional[Image.Image] = None
|
| 78 |
+
seed: int = 0
|
| 79 |
+
message: str = ""
|
| 80 |
+
error_code: ErrorCode = ErrorCode.SUCCESS
|
| 81 |
+
generation_time: float = 0.0
|
| 82 |
+
metadata: Optional[Dict[str, Any]] = None
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
class SystemMonitor:
|
| 86 |
+
"""Monitor system resources and performance"""
|
| 87 |
+
|
| 88 |
+
def __init__(self):
|
| 89 |
+
self.start_time = time.time()
|
| 90 |
+
self.generation_count = 0
|
| 91 |
+
self.error_count = 0
|
| 92 |
+
self.cache_hits = 0
|
| 93 |
+
self.cache_misses = 0
|
| 94 |
+
|
| 95 |
+
def get_system_info(self) -> Dict[str, Any]:
|
| 96 |
+
"""Get current system resource information"""
|
| 97 |
+
try:
|
| 98 |
+
memory = psutil.virtual_memory()
|
| 99 |
+
gpu_memory = self._get_gpu_memory()
|
| 100 |
+
|
| 101 |
+
return {
|
| 102 |
+
"uptime_seconds": time.time() - self.start_time,
|
| 103 |
+
"memory_used_gb": memory.used / (1024**3),
|
| 104 |
+
"memory_percent": memory.percent,
|
| 105 |
+
"gpu_memory_used_gb": gpu_memory,
|
| 106 |
+
"cpu_percent": psutil.cpu_percent(interval=0.1),
|
| 107 |
+
"active_generations": self.generation_count,
|
| 108 |
+
"error_count": self.error_count,
|
| 109 |
+
"cache_hit_rate": self.get_cache_hit_rate()
|
| 110 |
+
}
|
| 111 |
+
except Exception as e:
|
| 112 |
+
logger.error(f"Error getting system info: {e}")
|
| 113 |
+
return {}
|
| 114 |
+
|
| 115 |
+
def _get_gpu_memory(self) -> float:
|
| 116 |
+
"""Get GPU memory usage in GB"""
|
| 117 |
+
try:
|
| 118 |
+
if torch.cuda.is_available():
|
| 119 |
+
return torch.cuda.memory_allocated() / (1024**3)
|
| 120 |
+
except:
|
| 121 |
+
pass
|
| 122 |
+
return 0.0
|
| 123 |
+
|
| 124 |
+
def get_cache_hit_rate(self) -> float:
|
| 125 |
+
"""Calculate cache hit rate percentage"""
|
| 126 |
+
total = self.cache_hits + self.cache_misses
|
| 127 |
+
return (self.cache_hits / total * 100) if total > 0 else 0.0
|
| 128 |
+
|
| 129 |
+
def record_generation(self, success: bool):
|
| 130 |
+
"""Record a generation attempt"""
|
| 131 |
+
self.generation_count += 1
|
| 132 |
+
if not success:
|
| 133 |
+
self.error_count += 1
|
| 134 |
+
|
| 135 |
+
def record_cache_hit(self):
|
| 136 |
+
"""Record a cache hit"""
|
| 137 |
+
self.cache_hits += 1
|
| 138 |
+
|
| 139 |
+
def record_cache_miss(self):
|
| 140 |
+
"""Record a cache miss"""
|
| 141 |
+
self.cache_misses += 1
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
class ModelManager:
|
| 145 |
+
"""Manages model loading, optimization, and resource handling"""
|
| 146 |
+
|
| 147 |
+
def __init__(self):
|
| 148 |
+
self.model_name = "Tongyi-MAI/Z-Image-Turbo"
|
| 149 |
+
self.pipe_t2i = None
|
| 150 |
+
self.pipe_i2i = None
|
| 151 |
+
self.is_loaded = False
|
| 152 |
+
self.optimizations_applied = []
|
| 153 |
+
self._load_lock = asyncio.Lock()
|
| 154 |
+
|
| 155 |
+
async def load_models(self) -> bool:
|
| 156 |
+
"""Load models with proper error handling and fallbacks"""
|
| 157 |
+
async with self._load_lock:
|
| 158 |
+
if self.is_loaded:
|
| 159 |
+
return True
|
| 160 |
+
|
| 161 |
+
try:
|
| 162 |
+
logger.info(f"Loading model: {self.model_name}")
|
| 163 |
+
start_time = time.time()
|
| 164 |
+
|
| 165 |
+
# Determine optimal dtype based on hardware
|
| 166 |
+
dtype = self._get_optimal_dtype()
|
| 167 |
+
|
| 168 |
+
# Load base pipeline
|
| 169 |
+
self.pipe_t2i = DiffusionPipeline.from_pretrained(
|
| 170 |
+
self.model_name,
|
| 171 |
+
torch_dtype=dtype,
|
| 172 |
+
use_safetensors=True,
|
| 173 |
+
variant=self._get_variant(dtype),
|
| 174 |
+
low_cpu_mem_usage=True
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# Create img2img pipeline
|
| 178 |
+
self.pipe_i2i = StableDiffusionImg2ImgPipeline(
|
| 179 |
+
vae=self.pipe_t2i.vae,
|
| 180 |
+
text_encoder=self.pipe_t2i.text_encoder,
|
| 181 |
+
tokenizer=self.pipe_t2i.tokenizer,
|
| 182 |
+
unet=self.pipe_t2i.unet,
|
| 183 |
+
scheduler=self.pipe_t2i.scheduler,
|
| 184 |
+
safety_checker=None,
|
| 185 |
+
feature_extractor=None,
|
| 186 |
+
requires_safety_checker=False
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
# Apply optimizations
|
| 190 |
+
await self._apply_optimizations()
|
| 191 |
+
|
| 192 |
+
load_time = time.time() - start_time
|
| 193 |
+
logger.info(f"Models loaded successfully in {load_time:.2f}s")
|
| 194 |
+
logger.info(f"Applied optimizations: {', '.join(self.optimizations_applied)}")
|
| 195 |
+
|
| 196 |
+
self.is_loaded = True
|
| 197 |
+
return True
|
| 198 |
+
|
| 199 |
+
except Exception as e:
|
| 200 |
+
logger.error(f"Failed to load models: {e}")
|
| 201 |
+
logger.error(traceback.format_exc())
|
| 202 |
+
return False
|
| 203 |
+
|
| 204 |
+
def _get_optimal_dtype(self) -> torch.dtype:
|
| 205 |
+
"""Determine optimal data type based on hardware"""
|
| 206 |
+
try:
|
| 207 |
+
# Check for bfloat16 support (better for newer GPUs)
|
| 208 |
+
if torch.cuda.is_available() and torch.cuda.is_bf16_supported():
|
| 209 |
+
logger.info("Using bfloat16 for optimal performance")
|
| 210 |
+
return torch.bfloat16
|
| 211 |
+
# Fall back to float16 for compatibility
|
| 212 |
+
elif torch.cuda.is_available():
|
| 213 |
+
logger.info("Using float16 for CUDA")
|
| 214 |
+
return torch.float16
|
| 215 |
+
# CPU fallback
|
| 216 |
+
else:
|
| 217 |
+
logger.info("Using float32 for CPU")
|
| 218 |
+
return torch.float32
|
| 219 |
+
except:
|
| 220 |
+
logger.warning("Could not detect optimal dtype, using float32")
|
| 221 |
+
return torch.float32
|
| 222 |
+
|
| 223 |
+
def _get_variant(self, dtype: torch.dtype) -> Optional[str]:
|
| 224 |
+
"""Get model variant based on dtype"""
|
| 225 |
+
return "fp16" if dtype == torch.float16 else None
|
| 226 |
+
|
| 227 |
+
async def _apply_optimizations(self):
|
| 228 |
+
"""Apply performance optimizations with proper fallbacks"""
|
| 229 |
+
|
| 230 |
+
# 1. Try xformers optimization
|
| 231 |
+
if self._try_enable_xformers():
|
| 232 |
+
self.optimizations_applied.append("xformers")
|
| 233 |
+
|
| 234 |
+
# 2. Try model CPU offloading for memory efficiency
|
| 235 |
+
if self._try_enable_cpu_offload():
|
| 236 |
+
self.optimizations_applied.append("cpu_offload")
|
| 237 |
+
|
| 238 |
+
# 3. Try PyTorch 2.0+ compilation
|
| 239 |
+
if self._try_enable_torch_compile():
|
| 240 |
+
self.optimizations_applied.append("torch_compile")
|
| 241 |
+
|
| 242 |
+
# 4. Enable VAE slicing for memory efficiency
|
| 243 |
+
self._enable_vae_slicing()
|
| 244 |
+
|
| 245 |
+
# 5. Clear CUDA cache
|
| 246 |
+
if torch.cuda.is_available():
|
| 247 |
+
torch.cuda.empty_cache()
|
| 248 |
+
|
| 249 |
+
def _try_enable_xformers(self) -> bool:
|
| 250 |
+
"""Try to enable xformers with fallback"""
|
| 251 |
+
try:
|
| 252 |
+
import xformers.ops
|
| 253 |
+
self.pipe_t2i.enable_xformers_memory_efficient_attention()
|
| 254 |
+
self.pipe_i2i.enable_xformers_memory_efficient_attention()
|
| 255 |
+
logger.info("✓ Enabled xformers memory efficient attention")
|
| 256 |
+
return True
|
| 257 |
+
except ImportError:
|
| 258 |
+
logger.info("⚠ xformers not available, using default attention")
|
| 259 |
+
return False
|
| 260 |
+
except Exception as e:
|
| 261 |
+
logger.warning(f"⚠ Could not enable xformers: {e}")
|
| 262 |
+
return False
|
| 263 |
+
|
| 264 |
+
def _try_enable_cpu_offload(self) -> bool:
|
| 265 |
+
"""Try to enable CPU offloading with fallback"""
|
| 266 |
+
try:
|
| 267 |
+
# Only enable if we have limited GPU memory
|
| 268 |
+
if torch.cuda.is_available():
|
| 269 |
+
gpu_memory = torch.cuda.get_device_properties(0).total_memory
|
| 270 |
+
if gpu_memory < 8 * 1024**3: # Less than 8GB
|
| 271 |
+
self.pipe_t2i.enable_sequential_cpu_offload()
|
| 272 |
+
self.pipe_i2i.enable_sequential_cpu_offload()
|
| 273 |
+
logger.info("✓ Enabled sequential CPU offloading")
|
| 274 |
+
return True
|
| 275 |
+
except Exception as e:
|
| 276 |
+
logger.warning(f"⚠ Could not enable CPU offload: {e}")
|
| 277 |
+
return False
|
| 278 |
+
|
| 279 |
+
def _try_enable_torch_compile(self) -> bool:
|
| 280 |
+
"""Try to enable torch.compile with version check and fallback"""
|
| 281 |
+
try:
|
| 282 |
+
# Check PyTorch version
|
| 283 |
+
torch_version = torch.__version__.split('+')[0]
|
| 284 |
+
major, minor = map(int, torch_version.split('.')[:2])
|
| 285 |
+
|
| 286 |
+
if major >= 2:
|
| 287 |
+
logger.info("PyTorch 2.0+ detected, attempting compilation...")
|
| 288 |
+
self.pipe_t2i.unet = torch.compile(
|
| 289 |
+
self.pipe_t2i.unet,
|
| 290 |
+
mode="reduce-overhead",
|
| 291 |
+
fullgraph=False # More compatible
|
| 292 |
+
)
|
| 293 |
+
self.pipe_i2i.unet = torch.compile(
|
| 294 |
+
self.pipe_i2i.unet,
|
| 295 |
+
mode="reduce-overhead",
|
| 296 |
+
fullgraph=False
|
| 297 |
+
)
|
| 298 |
+
logger.info("✓ Successfully compiled UNet with torch.compile")
|
| 299 |
+
return True
|
| 300 |
+
else:
|
| 301 |
+
logger.info(f"⚠ PyTorch {torch_version} < 2.0, compilation not available")
|
| 302 |
+
except Exception as e:
|
| 303 |
+
logger.warning(f"⚠ Could not compile UNet: {e}")
|
| 304 |
+
return False
|
| 305 |
+
|
| 306 |
+
def _enable_vae_slicing(self):
|
| 307 |
+
"""Enable VAE slicing for memory efficiency"""
|
| 308 |
+
try:
|
| 309 |
+
self.pipe_t2i.vae.enable_slicing()
|
| 310 |
+
self.pipe_i2i.vae.enable_slicing()
|
| 311 |
+
logger.info("✓ Enabled VAE slicing")
|
| 312 |
+
except Exception as e:
|
| 313 |
+
logger.warning(f"⚠ Could not enable VAE slicing: {e}")
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
class CacheManager:
|
| 317 |
+
"""Manages caching for generated images and analyses"""
|
| 318 |
+
|
| 319 |
+
def __init__(self, max_size: int = 100):
|
| 320 |
+
self.max_size = max_size
|
| 321 |
+
self.image_cache: Dict[str, Tuple[Image.Image, datetime]] = {}
|
| 322 |
+
self.analysis_cache: Dict[str, Tuple[str, datetime]] = {}
|
| 323 |
+
self.cache_ttl = timedelta(hours=24)
|
| 324 |
+
|
| 325 |
+
def get_cache_key(self, *args) -> str:
|
| 326 |
+
"""Generate consistent cache key"""
|
| 327 |
+
key_str = "|".join(str(arg) for arg in args)
|
| 328 |
+
return hashlib.sha256(key_str.encode()).hexdigest()[:16]
|
| 329 |
+
|
| 330 |
+
def get_cached_image(self, cache_key: str) -> Optional[Image.Image]:
|
| 331 |
+
"""Get cached image if valid"""
|
| 332 |
+
if cache_key in self.image_cache:
|
| 333 |
+
image, timestamp = self.image_cache[cache_key]
|
| 334 |
+
if datetime.now() - timestamp < self.cache_ttl:
|
| 335 |
+
return image
|
| 336 |
+
else:
|
| 337 |
+
del self.image_cache[cache_key]
|
| 338 |
+
return None
|
| 339 |
|
| 340 |
+
def cache_image(self, cache_key: str, image: Image.Image):
|
| 341 |
+
"""Cache an image with LRU eviction"""
|
| 342 |
+
# Remove oldest if at capacity
|
| 343 |
+
if len(self.image_cache) >= self.max_size:
|
| 344 |
+
oldest_key = min(self.image_cache.keys(),
|
| 345 |
+
key=lambda k: self.image_cache[k][1])
|
| 346 |
+
del self.image_cache[oldest_key]
|
| 347 |
+
|
| 348 |
+
self.image_cache[cache_key] = (image, datetime.now())
|
| 349 |
+
|
| 350 |
+
def get_cached_analysis(self, cache_key: str) -> Optional[str]:
|
| 351 |
+
"""Get cached analysis if valid"""
|
| 352 |
+
if cache_key in self.analysis_cache:
|
| 353 |
+
analysis, timestamp = self.analysis_cache[cache_key]
|
| 354 |
+
if datetime.now() - timestamp < self.cache_ttl:
|
| 355 |
+
return analysis
|
| 356 |
+
else:
|
| 357 |
+
del self.analysis_cache[cache_key]
|
| 358 |
+
return None
|
| 359 |
|
| 360 |
+
def cache_analysis(self, cache_key: str, analysis: str):
|
| 361 |
+
"""Cache an analysis"""
|
| 362 |
+
if len(self.analysis_cache) >= self.max_size:
|
| 363 |
+
oldest_key = min(self.analysis_cache.keys(),
|
| 364 |
+
key=lambda k: self.analysis_cache[k][1])
|
| 365 |
+
del self.analysis_cache[oldest_key]
|
| 366 |
+
|
| 367 |
+
self.analysis_cache[cache_key] = (analysis, datetime.now())
|
| 368 |
+
|
| 369 |
+
def clear_expired(self):
|
| 370 |
+
"""Clear expired cache entries"""
|
| 371 |
+
now = datetime.now()
|
| 372 |
+
expired_images = [k for k, (_, t) in self.image_cache.items()
|
| 373 |
+
if now - t >= self.cache_ttl]
|
| 374 |
+
for k in expired_images:
|
| 375 |
+
del self.image_cache[k]
|
| 376 |
+
|
| 377 |
+
expired_analyses = [k for k, (_, t) in self.analysis_cache.items()
|
| 378 |
+
if now - t >= self.cache_ttl]
|
| 379 |
+
for k in expired_analyses:
|
| 380 |
+
del self.analysis_cache[k]
|
| 381 |
+
|
| 382 |
+
|
| 383 |
+
class ImageProcessor:
|
| 384 |
+
"""Handles image generation and transformation with error handling"""
|
| 385 |
+
|
| 386 |
+
def __init__(self, model_manager: ModelManager, cache_manager: CacheManager):
|
| 387 |
+
self.model_manager = model_manager
|
| 388 |
+
self.cache_manager = cache_manager
|
| 389 |
+
self.style_suffixes = {
|
| 390 |
+
"None": "",
|
| 391 |
+
"Photorealistic": ", photorealistic, ultra detailed, 8k, professional photography",
|
| 392 |
+
"Cinematic": ", cinematic lighting, movie scene, dramatic atmosphere, film grain",
|
| 393 |
+
"Anime": ", anime style, vibrant colors, cel shaded, studio ghibli inspired",
|
| 394 |
+
"Digital Art": ", digital art, detailed illustration, concept art",
|
| 395 |
+
"Oil Painting": ", oil painting, classical art, rich textures",
|
| 396 |
+
"Watercolor": ", watercolor painting, soft edges, artistic",
|
| 397 |
+
"3D Render": ", 3D render, octane render, detailed 3D",
|
| 398 |
+
"Fantasy": ", fantasy art, magical, ethereal atmosphere",
|
| 399 |
+
"Sci-Fi": ", sci-fi art, futuristic, high-tech"
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
@GPU(duration=120)
|
| 403 |
+
async def generate_image(
|
| 404 |
+
self,
|
| 405 |
+
prompt: str,
|
| 406 |
+
style: str = "None",
|
| 407 |
+
ratio: str = "1:1 Square (1024x1024)",
|
| 408 |
+
steps: int = 9,
|
| 409 |
+
seed: int = 42,
|
| 410 |
+
randomize: bool = True,
|
| 411 |
+
guidance_scale: float = 0.0
|
| 412 |
+
) -> GenerationResult:
|
| 413 |
+
"""Generate image with comprehensive error handling"""
|
| 414 |
+
result = GenerationResult(success=False)
|
| 415 |
+
start_time = time.time()
|
| 416 |
+
|
| 417 |
+
try:
|
| 418 |
+
# Validate inputs
|
| 419 |
+
if not prompt or not prompt.strip():
|
| 420 |
+
result.error_code = ErrorCode.INVALID_INPUT
|
| 421 |
+
result.message = "Prompt cannot be empty"
|
| 422 |
+
return result
|
| 423 |
+
|
| 424 |
+
# Ensure models are loaded
|
| 425 |
+
if not await self.model_manager.load_models():
|
| 426 |
+
result.error_code = ErrorCode.MODEL_LOAD_ERROR
|
| 427 |
+
result.message = "Failed to load models"
|
| 428 |
+
return result
|
| 429 |
+
|
| 430 |
+
# Parse dimensions
|
| 431 |
+
width, height = self._parse_aspect_ratio(ratio)
|
| 432 |
+
|
| 433 |
+
# Handle seed
|
| 434 |
+
if randomize:
|
| 435 |
+
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 436 |
+
|
| 437 |
+
generator = torch.Generator().manual_seed(seed)
|
| 438 |
+
|
| 439 |
+
# Enhance prompt with style
|
| 440 |
+
enhanced_prompt = prompt + self.style_suffixes.get(style, "")
|
| 441 |
+
|
| 442 |
+
# Optimize steps based on complexity
|
| 443 |
+
optimized_steps = self._optimize_steps(enhanced_prompt, steps)
|
| 444 |
+
|
| 445 |
+
# Generate
|
| 446 |
+
logger.info(f"Generating: {enhanced_prompt[:50]}... | {width}x{height} | {optimized_steps} steps")
|
| 447 |
+
|
| 448 |
+
output = self.model_manager.pipe_t2i(
|
| 449 |
+
prompt=enhanced_prompt,
|
| 450 |
+
width=width,
|
| 451 |
+
height=height,
|
| 452 |
+
num_inference_steps=optimized_steps,
|
| 453 |
+
guidance_scale=guidance_scale,
|
| 454 |
+
generator=generator,
|
| 455 |
+
output_type="pil"
|
| 456 |
+
)
|
| 457 |
|
| 458 |
+
# Success
|
| 459 |
+
result.success = True
|
| 460 |
+
result.image = output.images[0]
|
| 461 |
+
result.seed = seed
|
| 462 |
+
result.message = "Generated successfully"
|
| 463 |
+
result.generation_time = time.time() - start_time
|
| 464 |
+
|
| 465 |
+
logger.info(f"Generated in {result.generation_time:.2f}s")
|
| 466 |
+
|
| 467 |
+
except torch.cuda.OutOfMemoryError:
|
| 468 |
+
result.error_code = ErrorCode.RESOURCE_ERROR
|
| 469 |
+
result.message = "GPU out of memory. Try smaller image or restart space."
|
| 470 |
+
logger.error("GPU OOM during generation")
|
| 471 |
+
except Exception as e:
|
| 472 |
+
result.error_code = ErrorCode.GENERATION_ERROR
|
| 473 |
+
result.message = f"Generation failed: {str(e)}"
|
| 474 |
+
logger.error(f"Generation error: {e}")
|
| 475 |
+
logger.error(traceback.format_exc())
|
| 476 |
+
|
| 477 |
+
return result
|
| 478 |
+
|
| 479 |
+
@GPU(duration=120)
|
| 480 |
+
async def transform_image(
|
| 481 |
+
self,
|
| 482 |
+
input_image: Image.Image,
|
| 483 |
+
prompt: str,
|
| 484 |
+
style: str = "None",
|
| 485 |
+
strength: float = 0.8,
|
| 486 |
+
steps: int = 9,
|
| 487 |
+
seed: int = 42,
|
| 488 |
+
randomize: bool = True,
|
| 489 |
+
guidance_scale: float = 0.0
|
| 490 |
+
) -> GenerationResult:
|
| 491 |
+
"""Transform image with comprehensive error handling"""
|
| 492 |
+
result = GenerationResult(success=False)
|
| 493 |
+
start_time = time.time()
|
| 494 |
+
|
| 495 |
+
try:
|
| 496 |
+
# Validate inputs
|
| 497 |
+
if input_image is None:
|
| 498 |
+
result.error_code = ErrorCode.INVALID_INPUT
|
| 499 |
+
result.message = "Please upload an image"
|
| 500 |
+
return result
|
| 501 |
+
|
| 502 |
+
if not prompt or not prompt.strip():
|
| 503 |
+
result.error_code = ErrorCode.INVALID_INPUT
|
| 504 |
+
result.message = "Prompt cannot be empty"
|
| 505 |
+
return result
|
| 506 |
+
|
| 507 |
+
# Ensure models are loaded
|
| 508 |
+
if not await self.model_manager.load_models():
|
| 509 |
+
result.error_code = ErrorCode.MODEL_LOAD_ERROR
|
| 510 |
+
result.message = "Failed to load models"
|
| 511 |
+
return result
|
| 512 |
+
|
| 513 |
+
# Preprocess image
|
| 514 |
+
processed_image = self._preprocess_image(input_image)
|
| 515 |
+
|
| 516 |
+
# Handle seed
|
| 517 |
+
if randomize:
|
| 518 |
+
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 519 |
+
|
| 520 |
+
generator = torch.Generator().manual_seed(seed)
|
| 521 |
+
|
| 522 |
+
# Enhance prompt
|
| 523 |
+
enhanced_prompt = prompt + self.style_suffixes.get(style, "")
|
| 524 |
+
|
| 525 |
+
# Optimize steps based on strength
|
| 526 |
+
effective_steps = max(4, int(steps * strength)) if strength > 0 else steps
|
| 527 |
+
|
| 528 |
+
# Transform
|
| 529 |
+
logger.info(f"Transforming: {enhanced_prompt[:50]}... | strength={strength}")
|
| 530 |
+
|
| 531 |
+
output = self.model_manager.pipe_i2i(
|
| 532 |
+
prompt=enhanced_prompt,
|
| 533 |
+
image=processed_image,
|
| 534 |
+
strength=strength,
|
| 535 |
+
num_inference_steps=effective_steps,
|
| 536 |
+
guidance_scale=guidance_scale,
|
| 537 |
+
generator=generator,
|
| 538 |
+
output_type="pil"
|
| 539 |
+
)
|
| 540 |
|
| 541 |
+
# Success
|
| 542 |
+
result.success = True
|
| 543 |
+
result.image = output.images[0]
|
| 544 |
+
result.seed = seed
|
| 545 |
+
result.message = "Transformed successfully"
|
| 546 |
+
result.generation_time = time.time() - start_time
|
| 547 |
+
|
| 548 |
+
logger.info(f"Transformed in {result.generation_time:.2f}s")
|
| 549 |
+
|
| 550 |
+
except torch.cuda.OutOfMemoryError:
|
| 551 |
+
result.error_code = ErrorCode.RESOURCE_ERROR
|
| 552 |
+
result.message = "GPU out of memory. Try smaller image or restart space."
|
| 553 |
+
logger.error("GPU OOM during transform")
|
| 554 |
+
except Exception as e:
|
| 555 |
+
result.error_code = ErrorCode.TRANSFORM_ERROR
|
| 556 |
+
result.message = f"Transform failed: {str(e)}"
|
| 557 |
+
logger.error(f"Transform error: {e}")
|
| 558 |
+
logger.error(traceback.format_exc())
|
| 559 |
+
|
| 560 |
+
return result
|
| 561 |
+
|
| 562 |
+
def _parse_aspect_ratio(self, ratio: str) -> Tuple[int, int]:
|
| 563 |
+
"""Parse aspect ratio string to dimensions"""
|
| 564 |
+
ratios = {
|
| 565 |
+
"1:1": (1024, 1024),
|
| 566 |
+
"16:9": (1344, 768),
|
| 567 |
+
"9:16": (768, 1344),
|
| 568 |
+
"4:3": (1152, 896),
|
| 569 |
+
"3:4": (896, 1152)
|
| 570 |
+
}
|
| 571 |
+
|
| 572 |
+
# Extract ratio from string
|
| 573 |
+
for key, (w, h) in ratios.items():
|
| 574 |
+
if key in ratio:
|
| 575 |
+
return w, h
|
| 576 |
+
|
| 577 |
+
# Default to 1:1
|
| 578 |
+
return 1024, 1024
|
| 579 |
+
|
| 580 |
+
def _optimize_steps(self, prompt: str, base_steps: int) -> int:
|
| 581 |
+
"""Optimize step count based on prompt complexity"""
|
| 582 |
+
# Calculate complexity score
|
| 583 |
+
words = len(prompt.split())
|
| 584 |
+
commas = prompt.count(',')
|
| 585 |
+
periods = prompt.count('.')
|
| 586 |
+
|
| 587 |
+
complexity = words + (commas * 2) + (periods * 2)
|
| 588 |
+
|
| 589 |
+
# Adjust steps
|
| 590 |
+
if complexity < 10:
|
| 591 |
+
return max(4, base_steps - 2)
|
| 592 |
+
elif complexity > 30:
|
| 593 |
+
return min(16, base_steps + 2)
|
| 594 |
+
else:
|
| 595 |
+
return base_steps
|
| 596 |
+
|
| 597 |
+
def _preprocess_image(self, image: Image.Image) -> Image.Image:
|
| 598 |
+
"""Preprocess image for img2img pipeline"""
|
| 599 |
+
# Convert to RGB
|
| 600 |
+
if image.mode != "RGB":
|
| 601 |
+
image = image.convert("RGB")
|
| 602 |
+
|
| 603 |
+
# Resize to standard dimensions (maintain aspect ratio)
|
| 604 |
+
w, h = image.size
|
| 605 |
+
|
| 606 |
+
# Calculate new dimensions (multiple of 16)
|
| 607 |
+
max_size = 1024
|
| 608 |
+
aspect_ratio = w / h
|
| 609 |
+
|
| 610 |
+
if w > h:
|
| 611 |
+
new_w = min(max_size, w)
|
| 612 |
+
new_h = int(new_w / aspect_ratio)
|
| 613 |
+
else:
|
| 614 |
+
new_h = min(max_size, h)
|
| 615 |
+
new_w = int(new_h * aspect_ratio)
|
| 616 |
+
|
| 617 |
+
# Round to nearest multiple of 16
|
| 618 |
+
new_w = (new_w // 16) * 16
|
| 619 |
+
new_h = (new_h // 16) * 16
|
| 620 |
+
|
| 621 |
+
# Ensure minimum dimensions
|
| 622 |
+
new_w = max(512, new_w)
|
| 623 |
+
new_h = max(512, new_h)
|
| 624 |
+
|
| 625 |
+
return image.resize((new_w, new_h), Image.LANCZOS)
|
| 626 |
+
|
| 627 |
+
|
| 628 |
+
# Initialize global components
|
| 629 |
+
system_monitor = SystemMonitor()
|
| 630 |
+
model_manager = ModelManager()
|
| 631 |
+
cache_manager = CacheManager(max_size=100)
|
| 632 |
+
image_processor = ImageProcessor(model_manager, cache_manager)
|
| 633 |
+
|
| 634 |
+
# UI Constants
|
| 635 |
STYLES = ["None", "Photorealistic", "Cinematic", "Anime", "Digital Art",
|
| 636 |
"Oil Painting", "Watercolor", "3D Render", "Fantasy", "Sci-Fi"]
|
| 637 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 638 |
RATIOS = [
|
| 639 |
+
"1:1 Square (1024x1024)",
|
| 640 |
+
"16:9 Landscape (1344x768)",
|
| 641 |
+
"9:16 Portrait (768x1344)",
|
| 642 |
+
"4:3 Standard (1152x896)"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 643 |
]
|
| 644 |
|
| 645 |
+
# CSS for enhanced UI
|
| 646 |
+
CSS = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 647 |
:root {
|
| 648 |
+
--primary: #3b82f6;
|
| 649 |
+
--primary-dark: #2563eb;
|
| 650 |
+
--secondary: #10b981;
|
| 651 |
+
--background: #f8fafc;
|
| 652 |
+
--surface: #ffffff;
|
| 653 |
+
--error: #ef4444;
|
| 654 |
+
--warning: #f59e0b;
|
| 655 |
+
--success: #22c55e;
|
|
|
|
|
|
|
| 656 |
--border-radius: 12px;
|
| 657 |
+
--shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 658 |
}
|
| 659 |
|
| 660 |
+
/* Main container */
|
| 661 |
.gradio-container {
|
| 662 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
| 663 |
+
background: var(--background);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 664 |
}
|
| 665 |
|
| 666 |
+
/* Headers */
|
| 667 |
+
h1 {
|
| 668 |
+
color: #1e293b;
|
| 669 |
+
font-weight: 700;
|
| 670 |
+
font-size: 2.5rem;
|
| 671 |
+
margin-bottom: 0.5rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
}
|
| 673 |
|
| 674 |
+
h2 {
|
| 675 |
+
color: #334155;
|
| 676 |
+
font-weight: 600;
|
| 677 |
+
font-size: 1.5rem;
|
| 678 |
+
margin-top: 1.5rem;
|
| 679 |
}
|
| 680 |
|
| 681 |
+
/* Buttons */
|
| 682 |
+
.gradio-button {
|
| 683 |
border-radius: var(--border-radius);
|
| 684 |
+
font-weight: 600;
|
| 685 |
+
transition: all 0.2s ease;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 686 |
}
|
| 687 |
|
| 688 |
+
.gradio-button.primary {
|
| 689 |
+
background: var(--primary);
|
| 690 |
+
border: none;
|
| 691 |
}
|
| 692 |
|
| 693 |
+
.gradio-button.primary:hover {
|
| 694 |
+
background: var(--primary-dark);
|
| 695 |
+
transform: translateY(-1px);
|
| 696 |
+
box-shadow: var(--shadow);
|
|
|
|
| 697 |
}
|
| 698 |
|
| 699 |
+
/* Cards */
|
| 700 |
+
.border {
|
| 701 |
+
border: 1px solid #e2e8f0 !important;
|
| 702 |
+
border-radius: var(--border-radius) !important;
|
| 703 |
+
background: var(--surface);
|
|
|
|
|
|
|
| 704 |
}
|
| 705 |
|
| 706 |
+
/* Status indicators */
|
| 707 |
+
.status-success {
|
| 708 |
+
color: var(--success);
|
| 709 |
+
font-weight: 600;
|
| 710 |
}
|
| 711 |
|
| 712 |
+
.status-error {
|
| 713 |
+
color: var(--error);
|
| 714 |
+
font-weight: 600;
|
|
|
|
|
|
|
|
|
|
| 715 |
}
|
| 716 |
|
| 717 |
+
.status-warning {
|
| 718 |
+
color: var(--warning);
|
| 719 |
+
font-weight: 600;
|
| 720 |
}
|
| 721 |
|
| 722 |
+
/* Performance metrics */
|
| 723 |
+
.metric-card {
|
| 724 |
+
background: var(--surface);
|
| 725 |
+
padding: 1rem;
|
| 726 |
border-radius: var(--border-radius);
|
| 727 |
+
box-shadow: var(--shadow);
|
|
|
|
| 728 |
}
|
| 729 |
|
| 730 |
+
.metric-value {
|
| 731 |
+
font-size: 2rem;
|
|
|
|
|
|
|
|
|
|
| 732 |
font-weight: 700;
|
| 733 |
+
color: var(--primary);
|
|
|
|
| 734 |
}
|
| 735 |
|
| 736 |
+
.metric-label {
|
| 737 |
+
color: #64748b;
|
| 738 |
+
font-size: 0.875rem;
|
| 739 |
+
margin-top: 0.25rem;
|
|
|
|
|
|
|
|
|
|
| 740 |
}
|
| 741 |
|
| 742 |
+
/* Animations */
|
| 743 |
+
@keyframes pulse {
|
| 744 |
+
0%, 100% { opacity: 1; }
|
| 745 |
+
50% { opacity: 0.5; }
|
| 746 |
}
|
| 747 |
|
| 748 |
+
.loading {
|
| 749 |
+
animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 750 |
}
|
| 751 |
|
| 752 |
/* Responsive design */
|
| 753 |
@media (max-width: 768px) {
|
| 754 |
+
.gradio-row {
|
| 755 |
+
flex-direction: column !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 756 |
}
|
| 757 |
}
|
| 758 |
"""
|
| 759 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 760 |
|
| 761 |
+
async def handle_generation(
|
| 762 |
+
prompt: str,
|
| 763 |
+
style: str,
|
| 764 |
+
ratio: str,
|
| 765 |
+
steps: int,
|
| 766 |
+
seed: int,
|
| 767 |
+
randomize: bool,
|
| 768 |
+
guidance_scale: float
|
| 769 |
+
) -> Tuple[Optional[Image.Image], int, str]:
|
| 770 |
+
"""Handle image generation with caching"""
|
| 771 |
+
try:
|
| 772 |
+
# Check cache first
|
| 773 |
+
cache_key = cache_manager.get_cache_key(prompt, style, ratio, steps, seed)
|
| 774 |
+
cached_image = cache_manager.get_cached_image(cache_key)
|
| 775 |
+
|
| 776 |
+
if cached_image:
|
| 777 |
+
system_monitor.record_cache_hit()
|
| 778 |
+
logger.info("Returning cached generation")
|
| 779 |
+
return cached_image, seed, "✅ Retrieved from cache"
|
| 780 |
+
|
| 781 |
+
system_monitor.record_cache_miss()
|
| 782 |
+
|
| 783 |
+
# Generate new image
|
| 784 |
+
result = await image_processor.generate_image(
|
| 785 |
+
prompt=prompt,
|
| 786 |
+
style=style,
|
| 787 |
+
ratio=ratio,
|
| 788 |
+
steps=steps,
|
| 789 |
+
seed=seed,
|
| 790 |
+
randomize=randomize,
|
| 791 |
+
guidance_scale=guidance_scale
|
| 792 |
+
)
|
| 793 |
+
|
| 794 |
+
if result.success:
|
| 795 |
+
# Cache the result
|
| 796 |
+
cache_manager.cache_image(cache_key, result.image)
|
| 797 |
+
system_monitor.record_generation(True)
|
| 798 |
+
return result.image, result.seed, f"✅ {result.message} ({result.generation_time:.1f}s)"
|
| 799 |
+
else:
|
| 800 |
+
system_monitor.record_generation(False)
|
| 801 |
+
return None, seed, f"❌ {result.message}"
|
| 802 |
+
|
| 803 |
+
except Exception as e:
|
| 804 |
+
system_monitor.record_generation(False)
|
| 805 |
+
logger.error(f"Generation handler error: {e}")
|
| 806 |
+
return None, seed, f"❌ Unexpected error: {str(e)}"
|
| 807 |
+
|
| 808 |
+
|
| 809 |
+
async def handle_transform(
|
| 810 |
+
input_image: Image.Image,
|
| 811 |
+
prompt: str,
|
| 812 |
+
style: str,
|
| 813 |
+
strength: float,
|
| 814 |
+
steps: int,
|
| 815 |
+
seed: int,
|
| 816 |
+
randomize: bool,
|
| 817 |
+
guidance_scale: float
|
| 818 |
+
) -> Tuple[Optional[Image.Image], int, str]:
|
| 819 |
+
"""Handle image transformation"""
|
| 820 |
+
try:
|
| 821 |
+
result = await image_processor.transform_image(
|
| 822 |
+
input_image=input_image,
|
| 823 |
+
prompt=prompt,
|
| 824 |
+
style=style,
|
| 825 |
+
strength=strength,
|
| 826 |
+
steps=steps,
|
| 827 |
+
seed=seed,
|
| 828 |
+
randomize=randomize,
|
| 829 |
+
guidance_scale=guidance_scale
|
| 830 |
+
)
|
| 831 |
+
|
| 832 |
+
if result.success:
|
| 833 |
+
system_monitor.record_generation(True)
|
| 834 |
+
return result.image, result.seed, f"✅ {result.message} ({result.generation_time:.1f}s)"
|
| 835 |
+
else:
|
| 836 |
+
system_monitor.record_generation(False)
|
| 837 |
+
return None, seed, f"❌ {result.message}"
|
| 838 |
+
|
| 839 |
+
except Exception as e:
|
| 840 |
+
system_monitor.record_generation(False)
|
| 841 |
+
logger.error(f"Transform handler error: {e}")
|
| 842 |
+
return None, seed, f"❌ Unexpected error: {str(e)}"
|
| 843 |
+
|
| 844 |
+
|
| 845 |
+
def create_interface() -> gr.Blocks:
|
| 846 |
+
"""Create the Gradio interface"""
|
| 847 |
+
|
| 848 |
+
with gr.Blocks(
|
| 849 |
+
title="Z Image Turbo - Production",
|
| 850 |
+
theme=gr.themes.Soft(),
|
| 851 |
+
css=CSS
|
| 852 |
+
) as demo:
|
| 853 |
+
|
| 854 |
+
# Header
|
| 855 |
+
gr.HTML("""
|
| 856 |
+
<div style="text-align: center; padding: 2rem 0;">
|
| 857 |
+
<h1 style="margin: 0;">⚡ Z Image Turbo</h1>
|
| 858 |
+
<p style="color: #64748b; font-size: 1.1rem; margin-top: 0.5rem;">
|
| 859 |
+
Production-Ready Image Generation with Advanced Optimizations
|
| 860 |
+
</p>
|
| 861 |
+
</div>
|
| 862 |
+
""")
|
| 863 |
+
|
| 864 |
+
with gr.Tabs():
|
| 865 |
+
# Generation Tab
|
| 866 |
+
with gr.Tab("🎨 Generate", elem_id="generate-tab"):
|
| 867 |
+
with gr.Row():
|
| 868 |
+
with gr.Column(scale=3):
|
| 869 |
+
gen_prompt = gr.Textbox(
|
| 870 |
+
label="Prompt",
|
| 871 |
+
placeholder="Describe the image you want to generate...",
|
| 872 |
+
lines=3,
|
| 873 |
+
max_lines=5
|
| 874 |
)
|
| 875 |
+
|
| 876 |
gen_style = gr.Dropdown(
|
| 877 |
choices=STYLES,
|
| 878 |
value="None",
|
| 879 |
+
label="Style",
|
| 880 |
+
info="Apply a style to your generation"
|
| 881 |
)
|
| 882 |
|
| 883 |
+
gen_ratio = gr.Dropdown(
|
| 884 |
+
choices=RATIOS,
|
| 885 |
+
value="1:1 Square (1024x1024)",
|
| 886 |
+
label="Aspect Ratio"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 887 |
)
|
| 888 |
|
| 889 |
+
with gr.Row():
|
| 890 |
+
gen_steps = gr.Slider(
|
| 891 |
+
minimum=4,
|
| 892 |
+
maximum=16,
|
| 893 |
+
value=9,
|
| 894 |
+
step=1,
|
| 895 |
+
label="Inference Steps",
|
| 896 |
+
info="More steps = better quality but slower"
|
| 897 |
+
)
|
| 898 |
+
|
| 899 |
+
gen_guidance = gr.Slider(
|
| 900 |
+
minimum=0.0,
|
| 901 |
+
maximum=20.0,
|
| 902 |
+
value=0.0,
|
| 903 |
+
step=0.5,
|
| 904 |
+
label="Guidance Scale",
|
| 905 |
+
info="Higher = more prompt adherence"
|
| 906 |
+
)
|
| 907 |
+
|
| 908 |
+
with gr.Row():
|
| 909 |
+
gen_seed = gr.Number(
|
| 910 |
+
label="Seed",
|
| 911 |
+
value=42,
|
| 912 |
+
precision=0,
|
| 913 |
+
info="Fixed seed for reproducible results"
|
| 914 |
+
)
|
| 915 |
+
gen_randomize = gr.Checkbox(
|
| 916 |
+
label="Randomize Seed",
|
| 917 |
+
value=True,
|
| 918 |
+
info="Generate with random seed"
|
| 919 |
+
)
|
| 920 |
+
|
| 921 |
+
gen_btn = gr.Button(
|
| 922 |
+
"🚀 Generate",
|
| 923 |
+
variant="primary",
|
| 924 |
+
size="lg",
|
| 925 |
+
elem_classes=["generate-button"]
|
| 926 |
+
)
|
| 927 |
|
| 928 |
+
with gr.Column(scale=2):
|
| 929 |
+
gen_output = gr.Image(
|
| 930 |
+
label="Generated Image",
|
| 931 |
+
type="pil",
|
| 932 |
+
format="png",
|
| 933 |
+
interactive=False,
|
| 934 |
+
show_share_button=True,
|
| 935 |
+
show_download_button=True,
|
| 936 |
+
elem_classes=["output-image"]
|
| 937 |
+
)
|
|
|
|
| 938 |
|
| 939 |
+
gen_status = gr.Textbox(
|
| 940 |
+
label="Status",
|
| 941 |
+
interactive=False,
|
| 942 |
+
max_lines=2,
|
| 943 |
+
elem_classes=["status-text"]
|
| 944 |
+
)
|
| 945 |
|
| 946 |
+
gen_seed_out = gr.Number(
|
| 947 |
+
label="Seed Used",
|
| 948 |
+
interactive=False,
|
| 949 |
+
precision=0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 950 |
)
|
| 951 |
|
| 952 |
+
# Event handler
|
| 953 |
+
gen_btn.click(
|
| 954 |
+
fn=lambda *args: asyncio.run(handle_generation(*args)),
|
| 955 |
+
inputs=[
|
| 956 |
+
gen_prompt, gen_style, gen_ratio,
|
| 957 |
+
gen_steps, gen_seed, gen_randomize, gen_guidance
|
| 958 |
+
],
|
| 959 |
+
outputs=[gen_output, gen_seed_out, gen_status]
|
| 960 |
+
)
|
| 961 |
+
|
| 962 |
+
# Transform Tab
|
| 963 |
+
with gr.Tab("🖼️ Transform", elem_id="transform-tab"):
|
| 964 |
+
gr.Markdown("""
|
| 965 |
+
### Transform an existing image with AI
|
| 966 |
+
Upload an image and provide a prompt to guide the transformation.
|
| 967 |
+
""")
|
| 968 |
+
|
| 969 |
+
with gr.Row():
|
| 970 |
+
with gr.Column(scale=3):
|
| 971 |
+
trans_input = gr.Image(
|
| 972 |
+
label="Input Image",
|
| 973 |
+
type="pil",
|
| 974 |
+
sources=["upload", "webcam"]
|
| 975 |
+
)
|
| 976 |
|
| 977 |
+
trans_prompt = gr.Textbox(
|
| 978 |
+
label="Transform Prompt",
|
| 979 |
+
placeholder="Describe how to transform the image...",
|
| 980 |
+
lines=2
|
| 981 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 982 |
|
| 983 |
+
trans_style = gr.Dropdown(
|
| 984 |
+
choices=STYLES,
|
| 985 |
+
value="None",
|
| 986 |
+
label="Style"
|
| 987 |
+
)
|
|
|
|
|
|
|
| 988 |
|
| 989 |
+
with gr.Row():
|
| 990 |
+
trans_strength = gr.Slider(
|
| 991 |
+
minimum=0.0,
|
| 992 |
+
maximum=1.0,
|
| 993 |
+
value=0.8,
|
| 994 |
+
step=0.1,
|
| 995 |
+
label="Transformation Strength",
|
| 996 |
+
info="Higher = more changes"
|
| 997 |
+
)
|
| 998 |
+
|
| 999 |
+
trans_steps = gr.Slider(
|
| 1000 |
+
minimum=4,
|
| 1001 |
+
maximum=16,
|
| 1002 |
+
value=9,
|
| 1003 |
+
step=1,
|
| 1004 |
+
label="Inference Steps"
|
| 1005 |
+
)
|
| 1006 |
+
|
| 1007 |
+
with gr.Row():
|
| 1008 |
+
trans_seed = gr.Number(
|
| 1009 |
+
label="Seed",
|
| 1010 |
+
value=42,
|
| 1011 |
+
precision=0
|
| 1012 |
+
)
|
| 1013 |
+
trans_randomize = gr.Checkbox(
|
| 1014 |
+
label="Randomize Seed",
|
| 1015 |
+
value=True
|
| 1016 |
+
)
|
| 1017 |
+
|
| 1018 |
+
trans_btn = gr.Button(
|
| 1019 |
+
"✨ Transform",
|
| 1020 |
+
variant="primary",
|
| 1021 |
+
size="lg"
|
| 1022 |
+
)
|
| 1023 |
|
| 1024 |
+
with gr.Column(scale=2):
|
| 1025 |
+
trans_output = gr.Image(
|
| 1026 |
+
label="Transformed Image",
|
| 1027 |
+
type="pil",
|
| 1028 |
+
format="png",
|
| 1029 |
+
interactive=False,
|
| 1030 |
+
show_share_button=True
|
| 1031 |
+
)
|
| 1032 |
|
| 1033 |
+
trans_status = gr.Textbox(
|
| 1034 |
+
label="Status",
|
| 1035 |
+
interactive=False,
|
| 1036 |
+
max_lines=2
|
| 1037 |
+
)
|
|
|
|
|
|
|
|
|
|
| 1038 |
|
| 1039 |
+
# Event handler
|
| 1040 |
+
trans_btn.click(
|
| 1041 |
+
fn=lambda *args: asyncio.run(handle_transform(*args)),
|
| 1042 |
+
inputs=[
|
| 1043 |
+
trans_input, trans_prompt, trans_style,
|
| 1044 |
+
trans_strength, trans_steps, trans_seed,
|
| 1045 |
+
trans_randomize, gen_guidance
|
| 1046 |
+
],
|
| 1047 |
+
outputs=[trans_output, trans_seed_out, trans_status]
|
| 1048 |
+
)
|
| 1049 |
+
|
| 1050 |
+
# System Monitor Tab
|
| 1051 |
+
with gr.Tab("📊 System Monitor", elem_id="monitor-tab"):
|
| 1052 |
+
gr.Markdown("""
|
| 1053 |
+
### System Performance Metrics
|
| 1054 |
+
Real-time monitoring of system resources and application performance.
|
| 1055 |
+
""")
|
| 1056 |
+
|
| 1057 |
+
# Resource metrics
|
| 1058 |
+
with gr.Row():
|
| 1059 |
+
with gr.Column():
|
| 1060 |
+
gr.Markdown("#### 🖥️ System Resources")
|
| 1061 |
+
|
| 1062 |
+
with gr.Row():
|
| 1063 |
+
mem_usage = gr.Number(
|
| 1064 |
+
label="Memory Usage (GB)",
|
| 1065 |
+
precision=2,
|
| 1066 |
+
elem_classes=["metric-value"]
|
| 1067 |
+
)
|
| 1068 |
+
cpu_usage = gr.Number(
|
| 1069 |
+
label="CPU Usage (%)",
|
| 1070 |
+
precision=1,
|
| 1071 |
+
elem_classes=["metric-value"]
|
| 1072 |
+
)
|
| 1073 |
+
|
| 1074 |
+
gpu_mem = gr.Number(
|
| 1075 |
+
label="GPU Memory (GB)",
|
| 1076 |
+
precision=2,
|
| 1077 |
+
elem_classes=["metric-value"]
|
| 1078 |
)
|
| 1079 |
+
|
| 1080 |
+
with gr.Column():
|
| 1081 |
+
gr.Markdown("#### 📈 Application Metrics")
|
| 1082 |
+
|
| 1083 |
+
with gr.Row():
|
| 1084 |
+
uptime = gr.Number(
|
| 1085 |
+
label="Uptime (seconds)",
|
| 1086 |
+
precision=0,
|
| 1087 |
+
elem_classes=["metric-value"]
|
| 1088 |
+
)
|
| 1089 |
+
generations = gr.Number(
|
| 1090 |
+
label="Total Generations",
|
| 1091 |
+
precision=0,
|
| 1092 |
+
elem_classes=["metric-value"]
|
| 1093 |
+
)
|
| 1094 |
+
|
| 1095 |
+
cache_rate = gr.Number(
|
| 1096 |
+
label="Cache Hit Rate (%)",
|
| 1097 |
+
precision=1,
|
| 1098 |
+
elem_classes=["metric-value"]
|
| 1099 |
)
|
| 1100 |
|
| 1101 |
+
# Optimization status
|
| 1102 |
+
with gr.Row():
|
| 1103 |
+
opt_status = gr.JSON(
|
| 1104 |
+
label="Optimization Status",
|
| 1105 |
+
value={
|
| 1106 |
+
"model_loaded": False,
|
| 1107 |
+
"optimizations": [],
|
| 1108 |
+
"last_update": datetime.now().isoformat()
|
| 1109 |
+
}
|
| 1110 |
)
|
| 1111 |
|
| 1112 |
+
# Refresh button
|
| 1113 |
+
refresh_btn = gr.Button("🔄 Refresh", size="sm")
|
| 1114 |
+
|
| 1115 |
+
# Refresh handler
|
| 1116 |
+
def refresh_metrics():
|
| 1117 |
+
"""Refresh all metrics"""
|
| 1118 |
+
info = system_monitor.get_system_info()
|
| 1119 |
+
|
| 1120 |
+
return (
|
| 1121 |
+
info.get("memory_used_gb", 0),
|
| 1122 |
+
info.get("cpu_percent", 0),
|
| 1123 |
+
info.get("gpu_memory_used_gb", 0),
|
| 1124 |
+
info.get("uptime_seconds", 0),
|
| 1125 |
+
info.get("active_generations", 0),
|
| 1126 |
+
info.get("cache_hit_rate", 0),
|
| 1127 |
+
{
|
| 1128 |
+
"model_loaded": model_manager.is_loaded,
|
| 1129 |
+
"optimizations": model_manager.optimizations_applied,
|
| 1130 |
+
"last_update": datetime.now().isoformat()
|
| 1131 |
+
}
|
| 1132 |
)
|
| 1133 |
|
| 1134 |
+
refresh_btn.click(
|
| 1135 |
+
fn=refresh_metrics,
|
| 1136 |
+
outputs=[
|
| 1137 |
+
mem_usage, cpu_usage, gpu_mem,
|
| 1138 |
+
uptime, generations, cache_rate, opt_status
|
| 1139 |
+
]
|
| 1140 |
+
)
|
| 1141 |
+
|
| 1142 |
+
# Auto-refresh every 5 seconds
|
| 1143 |
+
demo.load(
|
| 1144 |
+
fn=refresh_metrics,
|
| 1145 |
+
outputs=[
|
| 1146 |
+
mem_usage, cpu_usage, gpu_mem,
|
| 1147 |
+
uptime, generations, cache_rate, opt_status
|
| 1148 |
+
],
|
| 1149 |
+
every=5
|
| 1150 |
+
)
|
| 1151 |
+
|
| 1152 |
+
# About Tab
|
| 1153 |
+
with gr.Tab("ℹ️ About", elem_id="about-tab"):
|
| 1154 |
+
gr.Markdown("""
|
| 1155 |
+
# Z Image Turbo - Production Edition
|
| 1156 |
+
|
| 1157 |
+
## Features
|
| 1158 |
+
- ✅ **PyTorch 2.0+ Compilation** with graceful fallback
|
| 1159 |
+
- ✅ **xformers Optimization** with CPU fallback
|
| 1160 |
+
- ✅ **Memory Management** with CPU offloading
|
| 1161 |
+
- ✅ **Caching System** for improved performance
|
| 1162 |
+
- ✅ **Comprehensive Error Handling**
|
| 1163 |
+
- ✅ **Real-time Monitoring**
|
| 1164 |
+
- ✅ **Production-Ready Architecture**
|
| 1165 |
+
|
| 1166 |
+
## Model
|
| 1167 |
+
- **Base Model**: [Tongyi-MAI/Z-Image-Turbo](https://huggingface.co/Tongyi-MAI/Z-Image-Turbo)
|
| 1168 |
+
- **Architecture**: DiT-based diffusion model
|
| 1169 |
+
- **Optimized for**: Fast generation with high quality
|
| 1170 |
+
|
| 1171 |
+
## System Requirements
|
| 1172 |
+
- GPU with at least 6GB VRAM recommended
|
| 1173 |
+
- PyTorch 2.0+ for optimal performance
|
| 1174 |
+
- Optional: xformers for memory efficiency
|
| 1175 |
+
|
| 1176 |
+
## Changelog
|
| 1177 |
+
### v2.0.0 Production
|
| 1178 |
+
- Added comprehensive error handling
|
| 1179 |
+
- Implemented PyTorch compilation with fallback
|
| 1180 |
+
- Added xformers optimization with CPU fallback
|
| 1181 |
+
- Integrated caching system
|
| 1182 |
+
- Added real-time monitoring
|
| 1183 |
+
- Improved resource management
|
| 1184 |
+
|
| 1185 |
+
---
|
| 1186 |
+
Created with ❤️ by AI Agent Framework Specialist
|
| 1187 |
+
""")
|
| 1188 |
+
|
| 1189 |
+
return demo
|
| 1190 |
+
|
| 1191 |
+
|
| 1192 |
+
# Health check endpoint
|
| 1193 |
+
async def health_check() -> Dict[str, Any]:
|
| 1194 |
+
"""Application health check"""
|
| 1195 |
+
return {
|
| 1196 |
+
"status": "healthy" if model_manager.is_loaded else "loading",
|
| 1197 |
+
"model_loaded": model_manager.is_loaded,
|
| 1198 |
+
"optimizations": model_manager.optimizations_applied,
|
| 1199 |
+
"uptime": time.time() - system_monitor.start_time,
|
| 1200 |
+
"generation_count": system_monitor.generation_count,
|
| 1201 |
+
"error_count": system_monitor.error_count,
|
| 1202 |
+
"cache_hit_rate": system_monitor.get_cache_hit_rate()
|
| 1203 |
+
}
|
| 1204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1205 |
|
| 1206 |
+
# Main application entry
|
| 1207 |
+
if __name__ == "__main__":
|
| 1208 |
+
logger.info("Starting Z Image Turbo - Production Edition")
|
|
|
|
|
|
|
| 1209 |
|
| 1210 |
+
# Create interface
|
| 1211 |
+
demo = create_interface()
|
| 1212 |
+
|
| 1213 |
+
# Configure for Hugging Face Spaces
|
| 1214 |
+
demo.queue(
|
| 1215 |
+
api_open=False,
|
| 1216 |
+
max_size=20,
|
| 1217 |
+
default_concurrency_limit=1
|
| 1218 |
+
)
|
| 1219 |
+
|
| 1220 |
+
# Launch with optimizations
|
| 1221 |
+
demo.launch(
|
| 1222 |
+
share=False,
|
| 1223 |
+
show_error=True,
|
| 1224 |
+
show_tips=True,
|
| 1225 |
+
max_threads=40,
|
| 1226 |
+
prevent_thread_lock=False
|
| 1227 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1228 |
|
| 1229 |
+
logger.info("Application launched successfully")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|