File size: 685 Bytes
e98a644
 
 
 
3c332da
 
 
 
e98a644
 
3c332da
 
e98a644
3c332da
e98a644
 
 
 
3c332da
e98a644
 
3c332da
e98a644
3c332da
e98a644
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import gradio as gr
from diffusers import StableDiffusionPipeline
import torch

# Automatically detect device
device = "cuda" if torch.cuda.is_available() else "cpu"

# Load Stable Diffusion model
pipe = StableDiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-2",
    torch_dtype=torch.float16 if device == "cuda" else torch.float32
).to(device)

# Define image generation function
def generate_image(prompt):
    image = pipe(prompt).images[0]
    return image

# Gradio Interface
gr.Interface(
    fn=generate_image,
    inputs=gr.Textbox(label="Enter your prompt here"),
    outputs="image",
    title="Text to Image Generator with Stable Diffusion 2"
).launch()