Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,43 +5,41 @@ import torch
|
|
| 5 |
from diffusers import DiffusionPipeline
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
# 2.
|
| 9 |
-
# Ensure your Space has access to FLUX.1-dev on Hugging Face
|
| 10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
|
| 12 |
-
# 3.
|
| 13 |
model_id = "black-forest-labs/FLUX.1-dev"
|
| 14 |
pipe = DiffusionPipeline.from_pretrained(
|
| 15 |
model_id,
|
| 16 |
-
torch_dtype=torch.bfloat16,
|
|
|
|
| 17 |
token=HF_TOKEN
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# 4.
|
| 21 |
pipe.load_lora_weights("strangerzonehf/Flux-Icon-Kit-LoRA")
|
| 22 |
pipe.fuse_lora()
|
| 23 |
|
| 24 |
-
# 5.
|
| 25 |
-
@spaces.GPU(duration=60)
|
| 26 |
def generate_icon(prompt, num_inference_steps=28, guidance_scale=3.5):
|
| 27 |
-
#
|
| 28 |
pipe.to("cuda")
|
| 29 |
|
| 30 |
-
# Enforce standard UI icon syntax constraints to the user prompt
|
| 31 |
full_prompt = f"Icon Kit, {prompt}, minimalist UI UX design element, flat line icon, uniform stroke, solid background"
|
| 32 |
|
| 33 |
-
# Run the image prediction pipeline
|
| 34 |
image = pipe(
|
| 35 |
prompt=full_prompt,
|
| 36 |
num_inference_steps=int(num_inference_steps),
|
| 37 |
guidance_scale=float(guidance_scale)
|
| 38 |
-
).images[0] #
|
| 39 |
|
| 40 |
-
#
|
| 41 |
pipe.to("cpu")
|
| 42 |
return image
|
| 43 |
|
| 44 |
-
# 6. Expose
|
| 45 |
demo = gr.Interface(
|
| 46 |
fn=generate_icon,
|
| 47 |
inputs=[
|
|
|
|
| 5 |
from diffusers import DiffusionPipeline
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
# 2. Retrieve token from space secrets
|
|
|
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
|
| 11 |
+
# 3. Optimize the pipeline instantiation for severe CPU RAM limits
|
| 12 |
model_id = "black-forest-labs/FLUX.1-dev"
|
| 13 |
pipe = DiffusionPipeline.from_pretrained(
|
| 14 |
model_id,
|
| 15 |
+
torch_dtype=torch.bfloat16, # Use half-precision for memory savings
|
| 16 |
+
low_cpu_mem_usage=True, # DO NOT load full weights into CPU system RAM at once
|
| 17 |
token=HF_TOKEN
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# 4. Mount and fuse the LoRA weights cleanly
|
| 21 |
pipe.load_lora_weights("strangerzonehf/Flux-Icon-Kit-LoRA")
|
| 22 |
pipe.fuse_lora()
|
| 23 |
|
| 24 |
+
# 5. Zero GPU dynamic invocation container wrapper
|
| 25 |
+
@spaces.GPU(duration=60)
|
| 26 |
def generate_icon(prompt, num_inference_steps=28, guidance_scale=3.5):
|
| 27 |
+
# Dynamically move tensors onto the dynamically provisioned Zero GPU
|
| 28 |
pipe.to("cuda")
|
| 29 |
|
|
|
|
| 30 |
full_prompt = f"Icon Kit, {prompt}, minimalist UI UX design element, flat line icon, uniform stroke, solid background"
|
| 31 |
|
|
|
|
| 32 |
image = pipe(
|
| 33 |
prompt=full_prompt,
|
| 34 |
num_inference_steps=int(num_inference_steps),
|
| 35 |
guidance_scale=float(guidance_scale)
|
| 36 |
+
).images[0] # Corrected syntax to safely extract the PIL object index
|
| 37 |
|
| 38 |
+
# CRUCIAL: Immediately dump back to CPU so the container doesn't hang/crash
|
| 39 |
pipe.to("cpu")
|
| 40 |
return image
|
| 41 |
|
| 42 |
+
# 6. Expose API Endpoints using Gradio Interface
|
| 43 |
demo = gr.Interface(
|
| 44 |
fn=generate_icon,
|
| 45 |
inputs=[
|