Spaces:
Sleeping
Sleeping
Create app.py
Browse files
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 |
+
)
|