image / app.py
openvision4's picture
Update app.py
bc300ed verified
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()