File size: 1,428 Bytes
70d39e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
import gradio as gr
from PIL import Image
import torch
from torch import autocast
from diffusers import StableDiffusionImg2ImgPipeline
from torch import autocast
from tqdm.auto import tqdm
import requests
from io import BytesIO
from PIL import Image
from typing import List, Optional, Union
import inspect
import warnings

# Load the Stable Diffusion model
modelid = "CompVis/stable-diffusion-v1-4"
device = "cuda" if torch.cuda.is_available() else "cpu"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(modelid, revision="fp16", torch_dtype=torch.float16)
pipe.to(device)

def generate_image(prompt):
    response = requests.get(url)
    init_img = Image.open(BytesIO(response.content)).convert("RGB")
    init_img = init_img.resize((768, 512))

    generator = torch.Generator(device=device).manual_seed(1024)
    with autocast("cuda"):
        image = pipe(prompt=prompt, init_image=init_img, strength=0.75, guidance_scale=7.5, generator=generator).images[0]

    return image

# Define the input and output components
input_text = gr.inputs.Textbox(lines=10, label="Enter a prompt")
output_image = gr.outputs.Image(label="Generated Image")

# Create the Gradio interface
iface = gr.Interface(
    fn=generate_image,
    inputs=input_text,
    outputs=output_image,
    title="Stable Bud",
    description="Generate images using Stable Diffusion",
    layout="vertical",
)

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