Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
from torch import autocast
|
5 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
6 |
+
from torch import autocast
|
7 |
+
from tqdm.auto import tqdm
|
8 |
+
import requests
|
9 |
+
from io import BytesIO
|
10 |
+
from PIL import Image
|
11 |
+
from typing import List, Optional, Union
|
12 |
+
import inspect
|
13 |
+
import warnings
|
14 |
+
|
15 |
+
# Load the Stable Diffusion model
|
16 |
+
modelid = "CompVis/stable-diffusion-v1-4"
|
17 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
18 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(modelid, revision="fp16", torch_dtype=torch.float16)
|
19 |
+
pipe.to(device)
|
20 |
+
|
21 |
+
def generate_image(prompt):
|
22 |
+
response = requests.get(url)
|
23 |
+
init_img = Image.open(BytesIO(response.content)).convert("RGB")
|
24 |
+
init_img = init_img.resize((768, 512))
|
25 |
+
|
26 |
+
generator = torch.Generator(device=device).manual_seed(1024)
|
27 |
+
with autocast("cuda"):
|
28 |
+
image = pipe(prompt=prompt, init_image=init_img, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
|
29 |
+
|
30 |
+
return image
|
31 |
+
|
32 |
+
# Define the input and output components
|
33 |
+
input_text = gr.inputs.Textbox(lines=10, label="Enter a prompt")
|
34 |
+
output_image = gr.outputs.Image(label="Generated Image")
|
35 |
+
|
36 |
+
# Create the Gradio interface
|
37 |
+
iface = gr.Interface(
|
38 |
+
fn=generate_image,
|
39 |
+
inputs=input_text,
|
40 |
+
outputs=output_image,
|
41 |
+
title="Stable Bud",
|
42 |
+
description="Generate images using Stable Diffusion",
|
43 |
+
layout="vertical",
|
44 |
+
)
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
iface.launch()
|