Spaces:
Sleeping
Sleeping
Update app.py
Browse files3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162633 unmodified linesfrom diffusers import DiffusionPipelineimport gradio as grHF_TOKEN = os.getenv("HF_TOKEN")model_id = "black-forest-labs/FLUX.1-dev"pipe = DiffusionPipeline.from_pretrained(model_id,torch_dtype=torch.bfloat16,low_cpu_mem_usage=True,token=HF_TOKEN)pipe.load_lora_weights("strangerzonehf/Flux-Icon-Kit-LoRA")pipe.fuse_lora()# GPU processing block@spaces.GPU(duration=60)def _run_gpu_inference(full_prompt, steps, guidance):pipe.to("cuda")image = pipe(prompt=full_prompt,num_inference_steps=steps,guidance_scale=guidance).images[0]pipe.to("cpu")return image# Main execution loop with safe fallback handlingdef generate_icon(prompt, num_inference_steps=28, guidance_scale=3.5):full_prompt = f"Icon Kit, {prompt}, minimalist UI UX design element, flat line icon, uniform stroke, solid background"steps = int(num_inference_steps)guidance = float(guidance_scale)try:# Attempt to run on dynamic ZeroGPU hardware allocationreturn _run_gpu_inference(full_prompt, steps, guidance)except Exception as gpu_error:print(f"ZeroGPU queue full, routing to CPU fallback node: {gpu_error}")# Run safely on the free CPU instance container if queue times outpipe.to("cpu")image = pipe(prompt=full_prompt,num_inference_steps=steps,guidance_scale=guidance).images[0]return imagedemo = gr.Interface(fn=generate_icon,inputs=[gr.Textbox(label="Icon Subject (e.g., 'a settings gear wheel')", placeholder="Enter icon concept..."),gr.Slider(minimum=15, maximum=40, value=28, step=1, label="Inference Steps"),gr.Slider(minimum=1.0, maximum=10.0, value=3.5, step=0.5, label="Guidance Scale")],outputs=gr.Image(type="pil", label="Generated UI Icon Component"),title="Flux UI/UX Icon Kit Generator")if __name__ == "__main__":3 unmodified lines45678910111213141516171819202122232425262728293031323334353637383940414243443 unmodified linesfrom diffusers import DiffusionPipelineimport gradio as gr# Schnell does NOT require a gated license token, making it faster to loadmodel_id = "black-forest-labs/FLUX.1-schnell"pipe = DiffusionPipeline.from_pretrained(model_id,torch_dtype=torch.bfloat16,low_cpu_mem_usage=True)# Load your UI Icon LoRApipe.load_lora_weights("strangerzonehf/Flux-Icon-Kit-LoRA")pipe.fuse_lora()# REDUCED FOOTPRINT: Duration cut from 60 seconds down to only 15 seconds!@spaces.GPU(duration=15)def generate_icon(prompt):pipe.to("cuda")full_prompt = f"Icon Kit, {prompt}, minimalist UI UX design element, flat line icon, uniform stroke, solid background"# Schnell completes clean layouts in only 4 steps!image = pipe(prompt=full_prompt,num_inference_steps=4,guidance_scale=0.0 # Schnell performs best with 0.0 guidance).imagespipe.to("cpu")return image# Streamlined interface matching the fast execution loopdemo = gr.Interface(fn=generate_icon,inputs=[gr.Textbox(label="Icon Subject (e.g., 'a settings gear wheel')")],outputs=gr.Image(type="pil", label="Generated UI Icon Component"),title="Flux UI/UX Icon Kit Generator (Fast Schnell Edition)")if __name__ == "__main__":
app.py+18-37