File size: 1,170 Bytes
9e02238
bc300ed
 
9e02238
bc300ed
 
 
 
 
 
 
9e02238
bc300ed
9e02238
bc300ed
9e02238
bc300ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e02238
 
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
import torch
from diffusers import StableDiffusionPipeline
import gradio as gr

# ๋ชจ๋ธ ๋กœ๋“œ (๋กœ์ปฌ์— ์—†์„ ๊ฒฝ์šฐ ํ—ˆ๊น…ํŽ˜์ด์Šค์—์„œ ์ž๋™ ๋‹ค์šด๋กœ๋“œ๋จ)
pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
    revision="fp16" if torch.cuda.is_available() else None,
    use_safetensors=True
)

# GPU ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉด ์ด๋™
if torch.cuda.is_available():
    pipe = pipe.to("cuda")
else:
    pipe = pipe.to("cpu")

# ์ƒ์„ฑ ํ•จ์ˆ˜
def generate_image(prompt):
    result = pipe(prompt, guidance_scale=7.5)
    image = result.images[0]
    return image

# Gradio ์ธํ„ฐํŽ˜์ด์Šค
demo = gr.Interface(
    fn=generate_image,
    inputs=gr.Textbox(label="ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”", placeholder="์˜ˆ: A fantasy landscape with flying whales"),
    outputs=gr.Image(label="์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€"),
    title="๐Ÿ–ผ๏ธ Text to Image Generator (Stable Diffusion)",
    description="ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ธ๊ณต์ง€๋Šฅ์ด ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋ธ: runwayml/stable-diffusion-v1-5"
)

# ์‹คํ–‰
if __name__ == "__main__":
    demo.launch()