import os os.environ.setdefault("HF_HOME", "/data/.cache/huggingface") os.environ.setdefault("HF_MODULES_CACHE", "/tmp/hf_modules") os.environ.setdefault("MPLCONFIGDIR", "/tmp/matplotlib") os.environ.setdefault("TOKENIZERS_PARALLELISM", "false") os.environ.setdefault("GRADIO_SSR_MODE", "false") os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True") import spaces import torch import gradio as gr from diffusers import ZImagePipeline import random import warnings import logging warnings.filterwarnings("ignore") logging.getLogger("transformers").setLevel(logging.ERROR) MODEL_ID = "Tongyi-MAI/Z-Image-Turbo" _pipeline = None def _load_pipeline(): print(f"Loading ZImagePipeline from {MODEL_ID}...") try: pipe = ZImagePipeline.from_pretrained( MODEL_ID, torch_dtype=torch.bfloat16, low_cpu_mem_usage=False, ) pipe.to("cuda") print("Model loaded successfully.") return pipe except Exception as e: print(f"Error loading model: {e}") import traceback traceback.print_exc() return None # Load at module level for ZeroGPU module-level CUDA emulation _pipeline = _load_pipeline() def health(): """Cheap health endpoint. Must not load weights, run GPU work, or download large files.""" return { "status": "healthy" if _pipeline is not None else "unhealthy", "model_loaded": _pipeline is not None, "model_id": MODEL_ID, } @spaces.GPU(duration=120) def generate( prompt: str, resolution: str, seed: int, random_seed: bool, num_inference_steps: int, progress=gr.Progress(track_tqdm=True), ): if _pipeline is None: raise gr.Error("Model not loaded. Please check Space logs.") if not prompt or not prompt.strip(): raise gr.Error("Please enter a prompt.") if random_seed: seed = random.randint(1, 1000000) try: width, height = map(int, resolution.split("x")) except Exception: width, height = 1024, 1024 generator = torch.Generator("cuda").manual_seed(int(seed)) with torch.no_grad(): image = _pipeline( prompt=prompt, height=height, width=width, num_inference_steps=int(num_inference_steps), guidance_scale=0.0, generator=generator, ).images[0] return image, str(seed) RESOLUTIONS = [ "1024x1024", "1152x896", "896x1152", "1280x720", "720x1280", "1536x1024", "1024x1536", ] EXAMPLES = [ ["A serene mountain landscape at sunset", "1024x1024", 42, True, 9], ["A futuristic city with neon lights and flying cars", "1024x1024", 42, True, 9], ["Young Chinese woman in red Hanfu, intricate embroidery", "1024x1024", 42, True, 9], ] with gr.Blocks(title="Z-Image-Turbo") as demo: gr.Markdown( """
Efficient 6B text-to-image generation with 8-step diffusion