tester343 commited on
Commit
3ad9157
·
verified ·
1 Parent(s): bd99eab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -33
app.py CHANGED
@@ -1,36 +1,47 @@
1
  import torch
2
- import gradio as gr
3
- from diffusers import StableDiffusionXLPipeline
4
-
5
- MODEL_ID = "stabilityai/sdxl-turbo"
6
-
7
- pipe = StableDiffusionXLPipeline.from_pretrained(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  MODEL_ID,
9
- torch_dtype=torch.float32,
 
 
10
  )
11
- pipe.to("cpu")
12
-
13
- pipe.enable_attention_slicing()
14
-
15
- def generate(prompt):
16
- image = pipe(
17
- prompt=prompt,
18
- num_inference_steps=4,
19
- guidance_scale=0.0,
20
- ).images[0]
21
- return image
22
-
23
- with gr.Blocks() as demo:
24
- gr.Markdown("## 🖼️ Image Generator (CPU Safe)")
25
-
26
- prompt = gr.Textbox(
27
- label="Prompt",
28
- placeholder="A cinematic cyberpunk portrait"
29
- )
30
-
31
- btn = gr.Button("Generate")
32
- out = gr.Image()
33
-
34
- btn.click(generate, prompt, out)
35
-
36
- demo.launch()
 
1
  import torch
2
+ from diffusers import StableDiffusionPipeline
3
+ from PIL import Image
4
+
5
+ # ----------------------------
6
+ # Settings
7
+ # ----------------------------
8
+ MODEL_ID = "stabilityai/stable-diffusion-1.5-turbo" # Turbo model for CPU speed
9
+ DEVICE = "cpu" # CPU only
10
+ DTYPE = torch.float32 # CPU-friendly
11
+
12
+ PROMPT = "A cute cat wearing sunglasses, digital art"
13
+ WIDTH = 768
14
+ HEIGHT = 768
15
+ NUM_STEPS = 1 # Fastest possible
16
+ GUIDANCE_SCALE = 0 # Disable guidance for speed
17
+
18
+ # ----------------------------
19
+ # Load pipeline
20
+ # ----------------------------
21
+ pipe = StableDiffusionPipeline.from_pretrained(
22
  MODEL_ID,
23
+ torch_dtype=DTYPE,
24
+ safety_checker=None, # Optional, disable for speed
25
+ feature_extractor=None # Optional, disable for speed
26
  )
27
+ pipe.to(DEVICE)
28
+
29
+ # ----------------------------
30
+ # Generate image
31
+ # ----------------------------
32
+ generator = torch.Generator(device=DEVICE).manual_seed(42) # Optional seed
33
+
34
+ image: Image.Image = pipe(
35
+ prompt=PROMPT,
36
+ height=HEIGHT,
37
+ width=WIDTH,
38
+ num_inference_steps=NUM_STEPS,
39
+ guidance_scale=GUIDANCE_SCALE,
40
+ generator=generator
41
+ ).images[0]
42
+
43
+ # ----------------------------
44
+ # Save or show
45
+ # ----------------------------
46
+ image.save("output.png")
47
+ image.show()