Yeonu1108 commited on
Commit
01ecc50
ยท
verified ยท
1 Parent(s): 5ca65c0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 traceback
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
+ "stabilityai/stable-diffusion-turbo",
16
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
17
+ ).to(device)
18
+ pipe.safety_checker = None # ์•ˆ์ „ํ•„ํ„ฐ off (์‹ค์Šต/ํ•™์Šต ๋ชฉ์ )
19
+
20
+ def caricature_from_photo(image):
21
+ try:
22
+ raw_image = image.convert('RGB')
23
+ inputs = blip_processor(raw_image, return_tensors="pt").to(device)
24
+ out = blip_model.generate(**inputs, max_new_tokens=30)
25
+ description = blip_processor.decode(out[0], skip_special_tokens=True)
26
+
27
+ # ์บ๋ฆฌ์ปค์ณ ์Šคํƒ€์ผ ํ”„๋กฌํ”„ํŠธ (์ด๋ชจํ‹ฐ์ฝ˜๊ณผ ์ง๊ด€์  ํ‚ค์›Œ๋“œ๋กœ ๊พธ๋ฐˆ)
28
+ caricature_prompt = (
29
+ f"{description}, cute cartoon, gentle lines, charming and lovely, sweet expression, lovely mood, digital illustration, "
30
+ "cheerful, simple background, joy, cartoon-style, ๐Ÿ˜„โœจ"
31
+ )
32
+
33
+ # turbo SD: ๋งค์šฐ ๋น ๋ฅธ ์ƒ์„ฑ!
34
+ result = pipe(
35
+ caricature_prompt,
36
+ num_inference_steps=4, # turbo ๊ถŒ์žฅ๊ฐ’: 4
37
+ guidance_scale=0.0 # turbo ๊ถŒ์žฅ๊ฐ’: 0.0 (ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ ๋ฐ˜์˜)
38
+ ).images[0]
39
+
40
+ info_text = (
41
+ f"**์ด๋ฏธ์ง€ ์„ค๋ช…**: {description}\n\n"
42
+ f"**์บ๋ฆฌ์ปค์ณ ํ”„๋กฌํ”„ํŠธ**: `{caricature_prompt}`"
43
+ )
44
+ return result, info_text
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()