File size: 1,201 Bytes
f73e37d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624e35e
 
 
f73e37d
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
import torch
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
import gradio as gr

# Check if CUDA is available
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")

if device == "cuda":
    torch.cuda.empty_cache()

model_id = "stabilityai/stable-diffusion-2-1"

# Use appropriate dtype based on device
if device == "cuda":
    pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
else:
    pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)

pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to(device)

def generate_image(prompt, width, height):
    image = pipe(prompt, width=int(width), height=int(height)).images[0]
    return image

iface = gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(label="Prompt", value="a house in front of the ocean and a dog is running in the field"),
        gr.Number(label="Width", value=1000),
        gr.Number(label="Height", value=1000)
    ],
    outputs=gr.Image(type="pil"),
    title="Stable Diffusion Image Generator"
)

if __name__ == "__main__":
    iface.launch()