MathObsession commited on
Commit
7bf6e65
·
verified ·
1 Parent(s): e813640

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -13
app.py CHANGED
@@ -5,43 +5,41 @@ import torch
5
  from diffusers import DiffusionPipeline
6
  import gradio as gr
7
 
8
- # 2. Authenticate using the built-in Hugging Face Space Token
9
- # Ensure your Space has access to FLUX.1-dev on Hugging Face
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
 
12
- # 3. Initialize the base pipeline on CPU to save Zero GPU resources initially
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. Fuse the Icon Kit LoRA into the base model weights
21
  pipe.load_lora_weights("strangerzonehf/Flux-Icon-Kit-LoRA")
22
  pipe.fuse_lora()
23
 
24
- # 5. Create the execution wrapper explicitly decorated for Zero GPU allocation
25
- @spaces.GPU(duration=60) # Allocates dynamic GPU ceiling for up to 60 seconds
26
  def generate_icon(prompt, num_inference_steps=28, guidance_scale=3.5):
27
- # Move the pipeline to the allocated GPU device safely
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] # Extracted the exact PIL image from the list
39
 
40
- # Offload back to CPU to cleanly yield the Zero GPU slot back to the public pool
41
  pipe.to("cpu")
42
  return image
43
 
44
- # 6. Expose the generation function as a clean Gradio API interface
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=[