| import gradio as gr |
| from PIL import Image |
| import torch |
| from transformers import BlipProcessor, BlipForConditionalGeneration |
| from diffusers import StableDiffusionPipeline |
| import traceback |
|
|
| |
| 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 |
|
|
| 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=False) |
|
|
| |
| caricature_prompt = ( |
| f"{description}, gentle lines, charming and lovely, sweet expression, cheerful, white background, joy, cartoon-style, caricature" |
| ) |
|
|
| |
| result = pipe( |
| caricature_prompt, |
| num_inference_steps=4, |
| guidance_scale=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() |