Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files123445678891011121314158 unmodified lines24252626272829272829333031323733343536413738394046414243444 unmodified lines49505157585253545556575861626364656667686970717273747576777879808182838485868759606162636465666788899091929394959697989910010110210310410510610710810911011111211311411511611711811968697059 unmodified lines13013113218518618718818919019119219319419519619713313413595 unmodified lines23123223329930030130223423523639 unmodified lines27627727834834935035135235335435535627928028124 unmodified lines306307308387309310311312313314394315316317318319320321401322323324325import spacesimport gradio as grimport torchfrom diffusers import ZImagePipeline, AutoPipelineForText2Imagefrom diffusers import ZImagePipelineimport osfrom pathlib import Path
# Load the base model directly at startup# Load the model directly at startupprint("Loading Z-Image Turbo model...")print("This may take a few minutes on first run while the model downloads...")
# Load the pipeline with optimal settingspipe = ZImagePipeline.from_pretrained( "Tongyi-MAI/Z-Image-Turbo",8 unmodified lines print("Model loaded successfully!")
# Store the current model statecurrent_model = "base"lora_loaded = False
@spaces.GPU()def generate_image( prompt, model_choice, progress=gr.Progress(track_tqdm=True)): """ Generate an image using Z-Image Turbo model with optional LoRA. Generate an image using Z-Image Turbo model. Args: prompt: Text description of the desired image model_choice: Either "Base Model" or "Classic Painting LoRA" Returns: Generated PIL Image """ global pipe, current_model, lora_loaded global pipe if pipe is None: raise gr.Error("Model failed to load on startup. Please restart the application.")4 unmodified lines # Determine device device = "cuda" if torch.cuda.is_available() else "cpu" # Handle model switching progress(0.05, desc="Loading model...") # Set random seed for reproducibility generator = torch.Generator(device).manual_seed(42) # Generate the image with optimal settings progress(0.1, desc="Generating image...") try: if model_choice == "Classic Painting LoRA" and current_model != "lora": # Load LoRA weights if not lora_loaded: print("Loading Classic Painting LoRA...") pipe.load_lora_weights( "renderartist/Classic-Painting-Z-Image-Turbo-LoRA", adapter_name="classic_painting", weight_name="pytorch_lora_weights.safetensors" ) lora_loaded = True # Set LoRA adapter pipe.set_adapters(["classic_painting"], adapter_weights=[0.8]) current_model = "lora" progress(0.15, desc="LoRA loaded, generating image...") # Generate with LoRA settings generator = torch.Generator(device).manual_seed(42) result = pipe( prompt=prompt, negative_prompt=None, height=1024, width=1024, num_inference_steps=9, guidance_scale=0.0, generator=generator, ) result = pipe( prompt=prompt, negative_prompt=None, height=1024, width=1024, num_inference_steps=9, guidance_scale=0.0, generator=generator, ) elif model_choice == "Base Model" and current_model != "base": # Disable LoRA pipe.disable_lora() current_model = "base" progress(0.15, desc="Generating image...") # Generate with base model settings generator = torch.Generator(device).manual_seed(42) result = pipe( prompt=prompt, negative_prompt=None, height=1024, width=1024, num_inference_steps=9, guidance_scale=0.0, generator=generator, ) else: # Model already loaded, just generate progress(0.15, desc="Generating image...") generator = torch.Generator(device).manual_seed(42) result = pipe( prompt=prompt, negative_prompt=None, height=1024, width=1024, num_inference_steps=9, guidance_scale=0.0, generator=generator, ) image = result.images[0] progress(1.0, desc="Complete!")59 unmodified lines box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);}
/* Model Selector */.model-selector { margin-bottom: 24px;}
.model-selector label { font-size: 15px !important; font-weight: 500 !important; color: #1d1d1f !important; margin-bottom: 8px !important; display: block !important;}
/* Textbox */textarea { font-size: 17px !important;95 unmodified lines color: #86868b !important;}
.dark .model-selector label { color: #f5f5f7 !important;}
/* Responsive */@media (max-width: 734px) { .main-title {39 unmodified lines # Input Section with gr.Column(elem_classes="input-section"): # Model Selector with gr.Group(elem_classes="model-selector"): model_choice = gr.Radio( choices=["Base Model", "Classic Painting LoRA"], value="Base Model", label="Select Model", info="Choose between the base Z-Image Turbo model or the Classic Painting LoRA variant" ) prompt = gr.Textbox( placeholder="Describe the image you want to create...", lines=3,24 unmodified lines gr.HTML(""" <div class="footer-text"> <p>Powered by Z-Image Turbo from Tongyi-MAI</p> <p>Classic Painting LoRA by renderartist</p> </div> """) # Event handlers generate_btn.click( fn=generate_image, inputs=[prompt, model_choice], inputs=prompt, outputs=output_image, api_visibility="public" ) prompt.submit( fn=generate_image, inputs=[prompt, model_choice], inputs=prompt, outputs=output_image, api_visibility="public" )app.py+21-100