Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,20 +2,24 @@ import gradio as gr
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
| 6 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 7 |
"stabilityai/stable-diffusion-2",
|
| 8 |
-
torch_dtype=torch.float16
|
| 9 |
-
).to(
|
| 10 |
|
|
|
|
| 11 |
def generate_image(prompt):
|
| 12 |
image = pipe(prompt).images[0]
|
| 13 |
return image
|
| 14 |
|
| 15 |
-
# Gradio
|
| 16 |
gr.Interface(
|
| 17 |
fn=generate_image,
|
| 18 |
-
inputs=gr.Textbox(label="Enter your prompt"),
|
| 19 |
outputs="image",
|
| 20 |
-
title="
|
| 21 |
).launch()
|
|
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Automatically detect device
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
# Load Stable Diffusion model
|
| 9 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 10 |
"stabilityai/stable-diffusion-2",
|
| 11 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 12 |
+
).to(device)
|
| 13 |
|
| 14 |
+
# Define image generation function
|
| 15 |
def generate_image(prompt):
|
| 16 |
image = pipe(prompt).images[0]
|
| 17 |
return image
|
| 18 |
|
| 19 |
+
# Gradio Interface
|
| 20 |
gr.Interface(
|
| 21 |
fn=generate_image,
|
| 22 |
+
inputs=gr.Textbox(label="Enter your prompt here"),
|
| 23 |
outputs="image",
|
| 24 |
+
title="Text to Image Generator with Stable Diffusion 2"
|
| 25 |
).launch()
|