import gradio as gr import spaces import torch from diffusers import HiDreamImagePipeline from transformers import PreTrainedTokenizerFast, LlamaForCausalLM import random import numpy as np # Set data type dtype = torch.bfloat16 device = "cpu" # Use CPU for model loading to avoid CUDA initialization # Load tokenizer and text encoder for Llama try: tokenizer_4 = PreTrainedTokenizerFast.from_pretrained("meta-llama/Meta-Llama-3.1-8B-Instruct") text_encoder_4 = LlamaForCausalLM.from_pretrained( "meta-llama/Meta-Llama-3.1-8B-Instruct", output_hidden_states=True, output_attentions=True, attn_implementation="eager", torch_dtype=dtype, ).to(device) except Exception as e: raise Exception(f"Failed to load Llama model: {e}. Ensure you have access to 'meta-llama/Meta-Llama-3.1-8B-Instruct' and are logged in via `huggingface-cli login`.") # Load the HiDreamImagePipeline try: pipe = HiDreamImagePipeline.from_pretrained( "HiDream-ai/HiDream-I1-Fast", tokenizer_4=tokenizer_4, text_encoder_4=text_encoder_4, torch_dtype=dtype, ).to(device) pipe.enable_model_cpu_offload() # Offload to CPU, automatically manages GPU placement except Exception as e: raise Exception(f"Failed to load HiDreamImagePipeline: {e}. Ensure you have access to 'HiDream-ai/HiDream-I1-Full'.") # Define maximum values MAX_SEED = np.iinfo(np.int32).max MAX_IMAGE_SIZE = 2048 # Inference function with GPU access @spaces.GPU() def infer(prompt, negative_prompt="", seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=16, guidance_scale=3.5, progress=gr.Progress(track_tqdm=True)): pipe.to("cuda") try: if randomize_seed: seed = random.randint(0, MAX_SEED) generator = torch.Generator("cuda").manual_seed(seed) # Generate the image; offloading handles device placement image = pipe( prompt=prompt, negative_prompt=negative_prompt, height=height, width=width, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale, generator=generator, output_type="pil", ).images[0] return image, seed finally: # Clear GPU memory torch.cuda.empty_cache() # Define examples examples = [ ["A cat holding a sign that says \"Hi-Dreams.ai\".", ""], ["A futuristic cityscape with flying cars.", "blurry, low quality"], ["A serene landscape with mountains and a lake.", ""], ] # CSS styling css = """ #col-container { margin: 0 auto; max-width: 960px; } .generate-btn { background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%) !important; border: none !important; color: white !important; } .generate-btn:hover { transform: translateY2px); box-shadow: 0 5px 15px rgba(0,0,0,0.2); } """ # Create Gradio interface with gr.Blocks(css=css) as app: gr.HTML("