Spaces:
Sleeping
Sleeping
Update app.py
Browse files3 unmodified lines4567897891011131412131415161718202122192021222324252627282627282930313233343532333435363738394041424344454647484950513637385455565758394060414243443 unmodified linesfrom diffusers import DiffusionPipelineimport gradio as gr
HF_TOKEN = os.getenv("HF_TOKEN")
model_id = "black-forest-labs/FLUX.1-dev"# 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, token=HF_TOKEN low_cpu_mem_usage=True)
# Load your UI Icon LoRApipe.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):# 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=steps, guidance_scale=guidance ).images[0] num_inference_steps=4, guidance_scale=0.0 # Schnell performs best with 0.0 guidance ).images 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 allocation return _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 out pipe.to("cpu") image = pipe( prompt=full_prompt, num_inference_steps=steps, guidance_scale=guidance ).images[0] 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')", 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") ], 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" title="Flux UI/UX Icon Kit Generator (Fast Schnell Edition)")
if __name__ == "__main__":app.py+18-37