adminuser742150 commited on
Commit
29a75bb
·
verified ·
1 Parent(s): e1edf19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -8
app.py CHANGED
@@ -3,7 +3,7 @@ import torch
3
  from diffusers import StableDiffusionPipeline
4
  import gradio as gr
5
 
6
- # Use a smaller Stable Diffusion model for CPU
7
  model_id = "stabilityai/stable-diffusion-2-1-base"
8
 
9
  pipe = StableDiffusionPipeline.from_pretrained(
@@ -11,25 +11,23 @@ pipe = StableDiffusionPipeline.from_pretrained(
11
  torch_dtype=torch.float32
12
  ).to("cpu")
13
 
14
- # Optimize for CPU
15
  pipe.enable_attention_slicing()
16
- pipe.enable_sequential_cpu_offload()
17
 
18
  def generate(prompt):
19
- # Keep steps and resolution low for faster generation
20
  image = pipe(
21
  prompt,
22
- num_inference_steps=12, # lower steps = faster
23
  guidance_scale=7, # balanced CFG
24
- width=384, # lower res (CPU safe)
25
  height=384
26
  ).images[0]
27
  return image
28
 
29
  with gr.Blocks(css=".gradio-container {max-width: 800px; margin: auto;}") as demo:
30
- gr.Markdown("## 🎨 AI Image Generator (CPU Friendly)\nType your prompt and get results in ~20s (CPU).")
31
 
32
- prompt = gr.Textbox(label="Prompt", placeholder="A castle on a hill at sunset")
33
  output = gr.Image(type="pil", label="Generated Image")
34
  btn = gr.Button("🚀 Generate")
35
 
 
3
  from diffusers import StableDiffusionPipeline
4
  import gradio as gr
5
 
6
+ # Load a lighter model for CPU
7
  model_id = "stabilityai/stable-diffusion-2-1-base"
8
 
9
  pipe = StableDiffusionPipeline.from_pretrained(
 
11
  torch_dtype=torch.float32
12
  ).to("cpu")
13
 
14
+ # CPU-friendly optimization
15
  pipe.enable_attention_slicing()
 
16
 
17
  def generate(prompt):
 
18
  image = pipe(
19
  prompt,
20
+ num_inference_steps=12, # keep low for speed
21
  guidance_scale=7, # balanced CFG
22
+ width=384, # smaller resolution
23
  height=384
24
  ).images[0]
25
  return image
26
 
27
  with gr.Blocks(css=".gradio-container {max-width: 800px; margin: auto;}") as demo:
28
+ gr.Markdown("## 🎨 AI Image Generator (CPU)\nType your prompt and generate in ~20s.")
29
 
30
+ prompt = gr.Textbox(label="Prompt", placeholder="A futuristic city at night")
31
  output = gr.Image(type="pil", label="Generated Image")
32
  btn = gr.Button("🚀 Generate")
33