Futuretop's picture
Update app.py
86c607c verified
raw
history blame
4.64 kB
import gradio as gr
from PIL import Image
import torch
from transformers import BlipProcessor, BlipForConditionalGeneration
from diffusers import StableDiffusionPipeline
import traceback
# ๋ชจ๋ธ ๋กœ๋”ฉ (์ตœ๋Œ€ํ•œ ๋น ๋ฅธ ์ฒ˜๋ฆฌ: BLIP + SD-Turbo)
device = "cuda" if torch.cuda.is_available() else "cpu"
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
pipe = StableDiffusionPipeline.from_pretrained(
"stabilityai/stable-diffusion-turbo",
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
).to(device)
pipe.safety_checker = None # ์•ˆ์ „ํ•„ํ„ฐ off (์‹ค์Šต/ํ•™์Šต ๋ชฉ์ )
def caricature_from_photo(image):
try:
raw_image = image.convert('RGB')
inputs = blip_processor(raw_image, return_tensors="pt").to(device)
out = blip_model.generate(**inputs, max_new_tokens=30)
description = blip_processor.decode(out[0], skip_special_tokens=True)
# ์บ๋ฆฌ์ปค์ณ ์Šคํƒ€์ผ ํ”„๋กฌํ”„ํŠธ (์ด๋ชจํ‹ฐ์ฝ˜๊ณผ ์ง๊ด€์  ํ‚ค์›Œ๋“œ๋กœ ๊พธ๋ฐˆ)
caricature_prompt = (
f"{description}, normal hands, five fingers, cute cartoon, gentle lines, charming and lovely, sweet expression, lovely mood, digital illustration, "
"cheerful, simple background, joy, cartoon-style, ๐Ÿ˜„โœจ"
)
# turbo SD: ๋งค์šฐ ๋น ๋ฅธ ์ƒ์„ฑ!
result = pipe(
caricature_prompt,
num_inference_steps=4, # turbo ๊ถŒ์žฅ๊ฐ’: 4
guidance_scale=0.0 # turbo ๊ถŒ์žฅ๊ฐ’: 0.0 (ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ ๋ฐ˜์˜)
).images[0]
info_text = (
f"**์ด๋ฏธ์ง€ ์„ค๋ช…**: {description}\n\n"
f"**์บ๋ฆฌ์ปค์ณ ํ”„๋กฌํ”„ํŠธ**: `{caricature_prompt}`"
)
return result, info_text
except Exception as e:
err = f"โ—๏ธ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์–ด์š”!\n\n{e}\n\n{traceback.format_exc()}"
empty = Image.new("RGB", (512,512), "#FFFDE7")
return empty, err
custom_css = """
body { background: linear-gradient(135deg, #a7ffeb 0%, #fce4ec 100%);}
.gradio-container { background-color: #fafcff !important;}
.gr-button { background: #ffd600; color: #252525; border-radius:18px; font-size:1.2em;}
.gr-button:hover { background:#ffd54f;}
#caricature-preview img {
border-radius: 30px; border: 4px solid #ffff8d;
box-shadow: 0px 8px 56px #ffd60044;
margin-top: 1em;
}
#main-view .block {
background: #ffffffb8; border-radius: 22px;
box-shadow: 0 4px 32px #b3afec33; margin:18px;
}
"""
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
gr.Markdown("""
<div style="text-align:center; padding-top:.5em;">
<span style="font-size:2.3em;">๐ŸŽจ <b style="color:#1ec69d">์บ๋ฆฌ์ปค์ณ ๋งค์ง</b> <span style="font-size:1em;">์•ฑ</span> โœจ</span>
<p style="font-size:1.11em; color:#575d76;">
์–ด๋–ค ์‚ฌ์ง„์ด๋“  <b>1์ดˆ ๋งŒ์— ์บ๋ฆฌ์ปค์ณ</b>๋กœ!<br>
<span style="color:#2196f3">์นด๋ฉ”๋ผ ๐Ÿ“ท</span> ๋˜๋Š” <span style="color:#ff1696">์•จ๋ฒ”</span>์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์˜ฌ๋ ค๋ณด์„ธ์š”!
</p>
</div>
""")
with gr.Row(elem_id="main-view"):
with gr.Column():
photo = gr.Image(
label="๐Ÿ“ท ์‚ฌ์ง„ ์ฐ๊ธฐ ๋˜๋Š” ์„ ํƒ(์•จ๋ฒ”)",
type="pil",
height=300,
elem_id="user-photo",
)
btn = gr.Button("๐Ÿ‘‰ ์บ๋ฆฌ์ปค์ณ ๋งŒ๋“ค๊ธฐ!", elem_id="do_caricature")
with gr.Column():
caricature_img = gr.Image(
label="๐Ÿ–ผ๏ธ ์บ๋ฆฌ์ปค์ณ ๋ฏธ๋ฆฌ๋ณด๊ธฐ",
elem_id="caricature-preview",
height=350,
show_label=True
)
info = gr.Markdown(label="์ƒ์„ฑ ๋‚ด์—ญ ๋ฐ ์„ค๋ช…", value="", elem_id="summary-field")
btn.click(caricature_from_photo, inputs=photo, outputs=[caricature_img, info])
# ์•ˆ๋‚ด ๋ฉ”์‹œ์ง€
gr.Markdown("""
<div style="background:#fffde7; border-radius:18px; border:1.5px dashed #ffe082; padding:1em; text-align:center; margin-top:18px;">
<b>๐Ÿ’ก ์‚ฌ์šฉ๋ฒ• & ์ธ์‡„ ์•ˆ๋‚ด</b><br/>
<ul style="text-align:left; margin-left:1.8em;">
<li>ํœด๋Œ€ํฐ์€ [์‚ฌ์ง„์ฐ๊ธฐ] ์„ ํƒ ์‹œ, ์นด๋ฉ”๋ผ์™€ ์•จ๋ฒ” ๋‘˜ ๋‹ค ์‚ฌ์šฉ ๊ฐ€๋Šฅ</li>
<li>[๐Ÿ‘‰ ์บ๋ฆฌ์ปค์ณ ๋งŒ๋“ค๊ธฐ!] ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์บ๋ฆฌ์ปค์ณ๊ฐ€ ๋น ๋ฅด๊ฒŒ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.</li>
<li>๊ฒฐ๊ณผ ์ด๋ฏธ์ง€๋ฅผ ๊ธธ๊ฒŒ ๋ˆ„๋ฅด๊ฑฐ๋‚˜ ์šฐํด๋ฆญํ•˜์—ฌ ์ €์žฅ/๊ณต์œ /์ธ์‡„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.</li>
</ul>
</div>
""")
demo.launch()