Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,47 @@
|
|
| 1 |
import torch
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
MODEL_ID,
|
| 9 |
-
torch_dtype=
|
|
|
|
|
|
|
| 10 |
)
|
| 11 |
-
pipe.to(
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|