Create app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,36 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
import torch
|
| 4 |
-
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 5 |
from diffusers import StableDiffusionPipeline
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
# ๋ชจ๋ธ ๋ก๋ฉ (์ต๋ํ ๋น ๋ฅธ ์ฒ๋ฆฌ: BLIP + SD-Turbo)
|
| 9 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
|
| 11 |
-
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 12 |
-
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
|
| 13 |
|
|
|
|
| 14 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 15 |
-
"
|
| 16 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 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 |
-
except Exception as e:
|
| 46 |
-
err = f"โ๏ธ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ด์!\n\n{e}\n\n{traceback.format_exc()}"
|
| 47 |
-
empty = Image.new("RGB", (512,512), "#FFFDE7")
|
| 48 |
-
return empty, err
|
| 49 |
-
|
| 50 |
-
custom_css = """
|
| 51 |
-
body { background: linear-gradient(135deg, #a7ffeb 0%, #fce4ec 100%);}
|
| 52 |
-
.gradio-container { background-color: #fafcff !important;}
|
| 53 |
-
.gr-button { background: #ffd600; color: #252525; border-radius:18px; font-size:1.2em;}
|
| 54 |
-
.gr-button:hover { background:#ffd54f;}
|
| 55 |
-
#caricature-preview img {
|
| 56 |
-
border-radius: 30px; border: 4px solid #ffff8d;
|
| 57 |
-
box-shadow: 0px 8px 56px #ffd60044;
|
| 58 |
-
margin-top: 1em;
|
| 59 |
-
}
|
| 60 |
-
#main-view .block {
|
| 61 |
-
background: #ffffffb8; border-radius: 22px;
|
| 62 |
-
box-shadow: 0 4px 32px #b3afec33; margin:18px;
|
| 63 |
-
}
|
| 64 |
-
"""
|
| 65 |
-
|
| 66 |
-
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 67 |
-
gr.Markdown("""
|
| 68 |
-
<div style="text-align:center; padding-top:.5em;">
|
| 69 |
-
<span style="font-size:2.3em;">๐จ <b style="color:#1ec69d">์บ๋ฆฌ์ปค์ณ ๋งค์ง</b> <span style="font-size:1em;">์ฑ</span> โจ</span>
|
| 70 |
-
<p style="font-size:1.11em; color:#575d76;">
|
| 71 |
-
์ด๋ค ์ฌ์ง์ด๋ <b>1์ด ๋ง์ ์บ๋ฆฌ์ปค์ณ</b>๋ก!<br>
|
| 72 |
-
<span style="color:#2196f3">์นด๋ฉ๋ผ ๐ท</span> ๋๋ <span style="color:#ff1696">์จ๋ฒ</span>์์ ์ด๋ฏธ์ง๋ฅผ ์ฌ๋ ค๋ณด์ธ์!
|
| 73 |
-
</p>
|
| 74 |
-
</div>
|
| 75 |
-
""")
|
| 76 |
-
with gr.Row(elem_id="main-view"):
|
| 77 |
-
with gr.Column():
|
| 78 |
-
photo = gr.Image(
|
| 79 |
-
label="๐ท ์ฌ์ง ์ฐ๊ธฐ ๋๋ ์ ํ(์จ๋ฒ)",
|
| 80 |
-
type="pil",
|
| 81 |
-
height=300,
|
| 82 |
-
elem_id="user-photo",
|
| 83 |
-
)
|
| 84 |
-
btn = gr.Button("๐ ์บ๋ฆฌ์ปค์ณ ๋ง๋ค๊ธฐ!", elem_id="do_caricature")
|
| 85 |
-
with gr.Column():
|
| 86 |
-
caricature_img = gr.Image(
|
| 87 |
-
label="๐ผ๏ธ ์บ๋ฆฌ์ปค์ณ ๋ฏธ๋ฆฌ๋ณด๊ธฐ",
|
| 88 |
-
elem_id="caricature-preview",
|
| 89 |
-
height=350,
|
| 90 |
-
show_label=True
|
| 91 |
-
)
|
| 92 |
-
info = gr.Markdown(label="์์ฑ ๋ด์ญ ๋ฐ ์ค๋ช
", value="", elem_id="summary-field")
|
| 93 |
-
btn.click(caricature_from_photo, inputs=photo, outputs=[caricature_img, info])
|
| 94 |
-
|
| 95 |
-
# ์๋ด ๋ฉ์์ง
|
| 96 |
-
gr.Markdown("""
|
| 97 |
-
<div style="background:#fffde7; border-radius:18px; border:1.5px dashed #ffe082; padding:1em; text-align:center; margin-top:18px;">
|
| 98 |
-
<b>๐ก ์ฌ์ฉ๋ฒ & ์ธ์ ์๋ด</b><br/>
|
| 99 |
-
<ul style="text-align:left; margin-left:1.8em;">
|
| 100 |
-
<li>ํด๋ํฐ์ [์ฌ์ง์ฐ๊ธฐ] ์ ํ ์, ์นด๋ฉ๋ผ์ ์จ๋ฒ ๋ ๋ค ์ฌ์ฉ ๊ฐ๋ฅ</li>
|
| 101 |
-
<li>[๐ ์บ๋ฆฌ์ปค์ณ ๋ง๋ค๊ธฐ!] ๋ฒํผ์ ๋๋ฅด๋ฉด ์บ๋ฆฌ์ปค์ณ๊ฐ ๋น ๋ฅด๊ฒ ์์ฑ๋ฉ๋๋ค.</li>
|
| 102 |
-
<li>๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๊ธธ๊ฒ ๋๋ฅด๊ฑฐ๋ ์ฐํด๋ฆญํ์ฌ ์ ์ฅ/๊ณต์ /์ธ์ ํ ์ ์์ต๋๋ค.</li>
|
| 103 |
-
</ul>
|
| 104 |
-
</div>
|
| 105 |
-
""")
|
| 106 |
-
|
| 107 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
|
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# ๋ชจ๋ธ ๋ก๋ (๋ก์ปฌ์ ์์ ๊ฒฝ์ฐ ํ๊น
ํ์ด์ค์์ ์๋ ๋ค์ด๋ก๋๋จ)
|
| 6 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 7 |
+
"runwayml/stable-diffusion-v1-5",
|
| 8 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 9 |
+
revision="fp16" if torch.cuda.is_available() else None,
|
| 10 |
+
use_safetensors=True
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# GPU ์ฌ์ฉ ๊ฐ๋ฅํ๋ฉด ์ด๋
|
| 14 |
+
if torch.cuda.is_available():
|
| 15 |
+
pipe = pipe.to("cuda")
|
| 16 |
+
else:
|
| 17 |
+
pipe = pipe.to("cpu")
|
| 18 |
+
|
| 19 |
+
# ์์ฑ ํจ์
|
| 20 |
+
def generate_image(prompt):
|
| 21 |
+
result = pipe(prompt, guidance_scale=7.5)
|
| 22 |
+
image = result.images[0]
|
| 23 |
+
return image
|
| 24 |
+
|
| 25 |
+
# Gradio ์ธํฐํ์ด์ค
|
| 26 |
+
demo = gr.Interface(
|
| 27 |
+
fn=generate_image,
|
| 28 |
+
inputs=gr.Textbox(label="Input Text", placeholder="์: A fantasy landscape with flying whales"),
|
| 29 |
+
outputs=gr.Image(label="Generated Image"),
|
| 30 |
+
title="๐ผ๏ธ Text to Image Generator (Stable Diffusion)",
|
| 31 |
+
description="AI creates a Image if you give the prompt. ๋ชจ๋ธ: runwayml/stable-diffusion-v1-5"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# ์คํ
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|