Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758import spacesimport osimport torchfrom 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")],12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152# 1. ALWAYS IMPORT SPACES ABSOLUTELY FIRSTimport spacesimport osimport torchfrom diffusers import DiffusionPipelineimport gradio as gr# 2. Retrieve token from space secrets for the gated model accessHF_TOKEN = os.getenv("HF_TOKEN")# 3. Optimize the pipeline instantiation for RAM limitsmodel_id = "black-forest-labs/FLUX.1-dev"pipe = DiffusionPipeline.from_pretrained(model_id,torch_dtype=torch.bfloat16, # Half-precision vectors save footprintlow_cpu_mem_usage=True, # Prevent loading full weights into CPU system RAM at oncetoken=HF_TOKEN)# 4. Mount and fuse the LoRA weights cleanly (Requires 'peft' in requirements.txt)pipe.load_lora_weights("strangerzonehf/Flux-Icon-Kit-LoRA")pipe.fuse_lora()# 5. Zero GPU dynamic invocation container wrapper@spaces.GPU(duration=60)def generate_icon(prompt, num_inference_steps=28, guidance_scale=3.5):# Dynamically move tensors onto the dynamically provisioned Zero GPUpipe.to("cuda")full_prompt = f"Icon Kit, {prompt}, minimalist UI UX design element, flat line icon, uniform stroke, solid background"# Run prediction execution loopoutput = pipe(prompt=full_prompt,num_inference_steps=int(num_inference_steps),guidance_scale=float(guidance_scale))image = output.images[0]# CRUCIAL: Immediately dump weights back to CPU to cleanly yield the Zero GPU slotpipe.to("cpu")return image# 6. Expose API Endpoints using a standard structured Gradio Interfacedemo = gr.Interface(fn=generate_icon,inputs=[gr.Textbox(label="Icon Subject", placeholder="Enter icon concept...", value="a settings gear wheel"),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")],
app.py+24-30