Spaces:
Sleeping
Sleeping
Update app.py
Browse files4 unmodified lines5678910111213141516171819202122232425262728293031323334353637383940414243444546474 unmodified linesfrom diffusers import DiffusionPipelineimport gradio as gr# 2. Authenticate using the built-in Hugging Face Space Token# Ensure your Space has access to FLUX.1-dev on Hugging FaceHF_TOKEN = os.getenv("HF_TOKEN")# 3. Initialize the base pipeline on CPU to save Zero GPU resources initiallymodel_id = "black-forest-labs/FLUX.1-dev"pipe = DiffusionPipeline.from_pretrained(model_id,torch_dtype=torch.bfloat16,token=HF_TOKEN)# 4. Fuse the Icon Kit LoRA into the base model weightspipe.load_lora_weights("strangerzonehf/Flux-Icon-Kit-LoRA")pipe.fuse_lora()# 5. Create the execution wrapper explicitly decorated for Zero GPU allocation@spaces.GPU(duration=60) # Allocates dynamic GPU ceiling for up to 60 secondsdef generate_icon(prompt, num_inference_steps=28, guidance_scale=3.5):# Move the pipeline to the allocated GPU device safelypipe.to("cuda")# Enforce standard UI icon syntax constraints to the user promptfull_prompt = f"Icon Kit, {prompt}, minimalist UI UX design element, flat line icon, uniform stroke, solid background"# Run the image prediction pipelineimage = pipe(prompt=full_prompt,num_inference_steps=int(num_inference_steps),guidance_scale=float(guidance_scale)).images[0] # Extracted the exact PIL image from the list# Offload back to CPU to cleanly yield the Zero GPU slot back to the public poolpipe.to("cpu")return image# 6. Expose the generation function as a clean Gradio API interfacedemo = gr.Interface(fn=generate_icon,inputs=[4 unmodified lines567891011121314151617181920212223242526272829303132333435363738394041424344454 unmodified linesfrom diffusers import DiffusionPipelineimport gradio as gr# 2. Retrieve token from space secretsHF_TOKEN = os.getenv("HF_TOKEN")# 3. Optimize the pipeline instantiation for severe CPU RAM limitsmodel_id = "black-forest-labs/FLUX.1-dev"pipe = DiffusionPipeline.from_pretrained(model_id,torch_dtype=torch.bfloat16, # Use half-precision for memory savingslow_cpu_mem_usage=True, # DO NOT load full weights into CPU system RAM at oncetoken=HF_TOKEN)# 4. Mount and fuse the LoRA weights cleanlypipe.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"image = pipe(prompt=full_prompt,num_inference_steps=int(num_inference_steps),guidance_scale=float(guidance_scale)).images[0] # Corrected syntax to safely extract the PIL object index# CRUCIAL: Immediately dump back to CPU so the container doesn't hang/crashpipe.to("cpu")return image# 6. Expose API Endpoints using Gradio Interfacedemo = gr.Interface(fn=generate_icon,inputs=[
app.py+11-13