File size: 4,612 Bytes
01ecc50 |
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
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}, 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()
|