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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -40
app.py CHANGED
@@ -1,56 +1,38 @@
 
1
  import torch
2
  from diffusers import StableDiffusionPipeline
3
  import gradio as gr
4
 
5
- # Load Stable Diffusion model (CPU)
 
 
6
  pipe = StableDiffusionPipeline.from_pretrained(
7
- "stabilityai/stable-diffusion-2-1-base",
8
  torch_dtype=torch.float32
9
  ).to("cpu")
 
 
10
  pipe.enable_attention_slicing()
 
11
 
12
- # Generate function
13
- def generate(prompt, steps, guidance, width, height):
14
  image = pipe(
15
  prompt,
16
- num_inference_steps=steps,
17
- guidance_scale=guidance,
18
- width=width,
19
- height=height
20
  ).images[0]
21
  return image
22
 
23
- # Responsive Layout
24
- with gr.Blocks(css="""
25
- .gradio-container {max-width: 900px; margin: auto;}
26
- @media (max-width: 768px) {
27
- .gradio-container {padding: 10px;}
28
- }
29
- """) as demo:
30
- gr.Markdown(
31
- """
32
- # 🎨 AI Image Generator
33
- Enter your prompt below and generate images with Stable Diffusion (CPU).
34
- Works on desktop & mobile 📱
35
- """
36
- )
37
-
38
- with gr.Row():
39
- with gr.Column(scale=2):
40
- prompt = gr.Textbox(
41
- label="Prompt",
42
- placeholder="Describe your image here...",
43
- lines=2
44
- )
45
- steps = gr.Slider(5, 30, value=15, step=1, label="Steps")
46
- guidance = gr.Slider(1, 15, value=7.5, step=0.5, label="Guidance Scale")
47
- width = gr.Slider(256, 512, value=384, step=64, label="Width (px)")
48
- height = gr.Slider(256, 512, value=384, step=64, label="Height (px)")
49
- btn = gr.Button("🚀 Generate", elem_classes="generate-btn")
50
-
51
- with gr.Column(scale=3):
52
- output = gr.Image(type="pil", label="Generated Image")
53
-
54
- btn.click(fn=generate, inputs=[prompt, steps, guidance, width, height], outputs=output)
55
 
56
  demo.launch()
 
1
+
2
  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(
10
+ model_id,
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
+
36
+ btn.click(fn=generate, inputs=prompt, outputs=output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  demo.launch()