englissi commited on
Commit
846cddc
ยท
verified ยท
1 Parent(s): 41d103d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import os
3
+ from PIL import Image, ImageDraw, ImageFont
4
+ import gradio as gr
5
+ from transformers import pipeline
6
+
7
+ # 1) ์ด๋ฏธ์ง€ ์บก์…”๋‹ ํŒŒ์ดํ”„๋ผ์ธ
8
+ captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
9
+
10
+ # 2) ๋ฐˆ + ์ž๋ง‰ ํ•ฉ์„ฑ ํ•จ์ˆ˜
11
+ def make_meme_with_subtitle(image_path, top_text, bottom_text):
12
+ # ์›๋ณธ ์ด๋ฏธ์ง€ ๋กœ๋“œ
13
+ img = Image.open(image_path).convert("RGB")
14
+ w, h = img.size
15
+
16
+ # โ‘  ์ž๋™ ์บก์…˜ ์ƒ์„ฑ
17
+ out = captioner(img, max_length=40, num_beams=5)
18
+ raw = out[0].get("generated_text") or out[0].get("text") or ""
19
+ subtitle = raw.strip().capitalize()
20
+
21
+ # โ‘ก ์ž๋ง‰์šฉ ๊ฒ€์€ ๋ 
22
+ bar_h = int(h * 0.15)
23
+ bar = Image.new("RGB", (w, bar_h), color="black")
24
+ combined_h = h + bar_h
25
+ combined = Image.new("RGB", (w, combined_h))
26
+ combined.paste(img, (0, 0))
27
+ combined.paste(bar, (0, h))
28
+
29
+ draw = ImageDraw.Draw(combined)
30
+ # ์‚ฌ์šฉํ•  ํฐํŠธ (์‹œ์Šคํ…œ์— ์—†์œผ๋ฉด ๊ธฐ๋ณธ ํฐํŠธ)
31
+ try:
32
+ font_path = "arialbd.ttf"
33
+ base_font_size = int(bar_h * 0.5)
34
+ font = ImageFont.truetype(font_path, size=base_font_size)
35
+ except:
36
+ font = ImageFont.load_default()
37
+
38
+ # โ‘ข ์ž๋ง‰ ๊ทธ๋ฆฌ๊ธฐ (์ด๋ฏธ์ง€ ํ•˜๋‹จ ์ค‘์•™)
39
+ tw, th = draw.textsize(subtitle, font=font)
40
+ tx = (w - tw) // 2
41
+ ty = h + (bar_h - th) // 2
42
+ draw.text((tx, ty), subtitle, font=font, fill="white")
43
+
44
+ # โ‘ฃ ๋ฐˆ ํ…์ŠคํŠธ (์ƒ๋‹จยทํ•˜๋‹จ)
45
+ meme_font_size = int(h * 0.07)
46
+ try:
47
+ meme_font = ImageFont.truetype(font_path, size=meme_font_size)
48
+ except:
49
+ meme_font = ImageFont.load_default()
50
+
51
+ # ์ƒ๋‹จ ํ…์ŠคํŠธ
52
+ if top_text:
53
+ text = top_text.upper()
54
+ tw, th = draw.textsize(text, font=meme_font)
55
+ draw.text(((w - tw)//2, 10), text, font=meme_font, fill="white", stroke_width=2, stroke_fill="black")
56
+
57
+ # ํ•˜๋‹จ ํ…์ŠคํŠธ (์ด๋ฏธ์ง€ ์˜์—ญ ์•ˆ์ชฝ)
58
+ if bottom_text:
59
+ text = bottom_text.upper()
60
+ tw, th = draw.textsize(text, font=meme_font)
61
+ draw.text(((w - tw)//2, h - th - 10), text, font=meme_font, fill="white", stroke_width=2, stroke_fill="black")
62
+
63
+ return combined
64
+
65
+ # 3) Gradio UI
66
+ with gr.Blocks() as demo:
67
+ gr.Markdown("## ๐Ÿ“ธ ๋ฐˆ ์ƒ์„ฑ + ์ž๋™ ์˜์–ด ์ž๋ง‰ ํ•ฉ์„ฑ")
68
+ gr.Markdown("์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๊ณ , ์ƒ๋‹จยทํ•˜๋‹จ ๋ฐˆ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด\n์ž๋™ ์ƒ์„ฑ๋œ ์˜์–ด ์ž๋ง‰๊ณผ ํ•จ๊ป˜ ๋ฐˆ ์Šคํƒ€์ผ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.")
69
+ img_in = gr.Image(type="filepath", label="Upload Image")
70
+ top_txt = gr.Textbox(label="Top Text (optional)", placeholder="e.g. WHEN YOU REALIZE...", lines=1)
71
+ bottom_txt = gr.Textbox(label="Bottom Text (optional)", placeholder="e.g. ...IT'S MONDAY AGAIN", lines=1)
72
+ btn = gr.Button("Generate Meme")
73
+ out_img = gr.Image(label="Meme with Subtitle")
74
+ btn.click(fn=make_meme_with_subtitle, inputs=[img_in, top_txt, bottom_txt], outputs=out_img)
75
+
76
+ # 4) ์•ฑ ์‹คํ–‰
77
+ if __name__ == "__main__":
78
+ demo.launch(
79
+ server_name="0.0.0.0",
80
+ server_port=int(os.environ.get("PORT", 7860))
81
+ )