MathObsession commited on
Commit
7b53522
·
verified ·
1 Parent(s): f0e2bfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -5
app.py CHANGED
@@ -5,14 +5,14 @@ import torch
5
  from diffusers import DiffusionPipeline
6
  import gradio as gr
7
 
8
- # 2. Retrieve token from space secrets for the gated model access
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
 
11
  # 3. Optimize the pipeline instantiation for RAM limits
12
  model_id = "black-forest-labs/FLUX.1-dev"
13
  pipe = DiffusionPipeline.from_pretrained(
14
  model_id,
15
- torch_dtype=torch.bfloat16, # Half-precision vectors save footprint
16
  low_cpu_mem_usage=True, # Prevent loading full weights into CPU system RAM at once
17
  token=HF_TOKEN
18
  )
@@ -29,24 +29,25 @@ def generate_icon(prompt, num_inference_steps=28, guidance_scale=3.5):
29
 
30
  full_prompt = f"Icon Kit, {prompt}, minimalist UI UX design element, flat line icon, uniform stroke, solid background"
31
 
32
- # Run prediction execution loop
33
  output = pipe(
34
  prompt=full_prompt,
35
  num_inference_steps=int(num_inference_steps),
36
  guidance_scale=float(guidance_scale)
37
  )
38
 
 
39
  image = output.images[0]
40
 
41
  # CRUCIAL: Immediately dump weights back to CPU to cleanly yield the Zero GPU slot
42
  pipe.to("cpu")
43
  return image
44
 
45
- # 6. Expose API Endpoints using a standard structured Gradio Interface
46
  demo = gr.Interface(
47
  fn=generate_icon,
48
  inputs=[
49
- gr.Textbox(label="Icon Subject", placeholder="Enter icon concept...", value="a settings gear wheel"),
50
  gr.Slider(minimum=15, maximum=40, value=28, step=1, label="Inference Steps"),
51
  gr.Slider(minimum=1.0, maximum=10.0, value=3.5, step=0.5, label="Guidance Scale")
52
  ],
 
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 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, # Prevent loading full weights into CPU system RAM at once
17
  token=HF_TOKEN
18
  )
 
29
 
30
  full_prompt = f"Icon Kit, {prompt}, minimalist UI UX design element, flat line icon, uniform stroke, solid background"
31
 
32
+ # Run prediction
33
  output = pipe(
34
  prompt=full_prompt,
35
  num_inference_steps=int(num_inference_steps),
36
  guidance_scale=float(guidance_scale)
37
  )
38
 
39
+ # Extract the exact first PIL Image item from the output list safely
40
  image = output.images[0]
41
 
42
  # CRUCIAL: Immediately dump weights back to CPU to cleanly yield the Zero GPU slot
43
  pipe.to("cpu")
44
  return image
45
 
46
+ # 6. Expose API Endpoints using Gradio Interface
47
  demo = gr.Interface(
48
  fn=generate_icon,
49
  inputs=[
50
+ gr.Textbox(label="Icon Subject (e.g., 'a settings gear wheel')", placeholder="Enter icon concept..."),
51
  gr.Slider(minimum=15, maximum=40, value=28, step=1, label="Inference Steps"),
52
  gr.Slider(minimum=1.0, maximum=10.0, value=3.5, step=0.5, label="Guidance Scale")
53
  ],