Spaces:
Sleeping
Sleeping
Update app.py
Browse files4 unmodified lines567898910121112131416151617181920202122232425242526272728293030313332333435383637403839404144424344454 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 Face# 2. Retrieve token from space secretsHF_TOKEN = os.getenv("HF_TOKEN")
# 3. Initialize the base pipeline on CPU to save Zero GPU resources initially# 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, torch_dtype=torch.bfloat16, # Use half-precision for memory savings low_cpu_mem_usage=True, # DO NOT load full weights into CPU system RAM at once token=HF_TOKEN)
# 4. Fuse the Icon Kit LoRA into the base model weights# 4. Mount and fuse the LoRA weights cleanly pipe.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 seconds# 5. Zero GPU dynamic invocation container wrapper@spaces.GPU(duration=60) def generate_icon(prompt, num_inference_steps=28, guidance_scale=3.5): # Move the pipeline to the allocated GPU device safely # Dynamically move tensors onto the dynamically provisioned Zero GPU pipe.to("cuda") # Enforce standard UI icon syntax constraints to the user prompt full_prompt = f"Icon Kit, {prompt}, minimalist UI UX design element, flat line icon, uniform stroke, solid background" # Run the image prediction pipeline image = 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 ).images[0] # Corrected syntax to safely extract the PIL object index # Offload back to CPU to cleanly yield the Zero GPU slot back to the public pool # CRUCIAL: Immediately dump back to CPU so the container doesn't hang/crash pipe.to("cpu") return image
# 6. Expose the generation function as a clean Gradio API interface# 6. Expose API Endpoints using Gradio Interfacedemo = gr.Interface( fn=generate_icon, inputs=[app.py+11-13