memecaptioning / app.py
englissi's picture
Create app.py
846cddc verified
raw
history blame
3.05 kB
# app.py
import os
from PIL import Image, ImageDraw, ImageFont
import gradio as gr
from transformers import pipeline
# 1) ์ด๋ฏธ์ง€ ์บก์…”๋‹ ํŒŒ์ดํ”„๋ผ์ธ
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
# 2) ๋ฐˆ + ์ž๋ง‰ ํ•ฉ์„ฑ ํ•จ์ˆ˜
def make_meme_with_subtitle(image_path, top_text, bottom_text):
# ์›๋ณธ ์ด๋ฏธ์ง€ ๋กœ๋“œ
img = Image.open(image_path).convert("RGB")
w, h = img.size
# โ‘  ์ž๋™ ์บก์…˜ ์ƒ์„ฑ
out = captioner(img, max_length=40, num_beams=5)
raw = out[0].get("generated_text") or out[0].get("text") or ""
subtitle = raw.strip().capitalize()
# โ‘ก ์ž๋ง‰์šฉ ๊ฒ€์€ ๋ 
bar_h = int(h * 0.15)
bar = Image.new("RGB", (w, bar_h), color="black")
combined_h = h + bar_h
combined = Image.new("RGB", (w, combined_h))
combined.paste(img, (0, 0))
combined.paste(bar, (0, h))
draw = ImageDraw.Draw(combined)
# ์‚ฌ์šฉํ•  ํฐํŠธ (์‹œ์Šคํ…œ์— ์—†์œผ๋ฉด ๊ธฐ๋ณธ ํฐํŠธ)
try:
font_path = "arialbd.ttf"
base_font_size = int(bar_h * 0.5)
font = ImageFont.truetype(font_path, size=base_font_size)
except:
font = ImageFont.load_default()
# โ‘ข ์ž๋ง‰ ๊ทธ๋ฆฌ๊ธฐ (์ด๋ฏธ์ง€ ํ•˜๋‹จ ์ค‘์•™)
tw, th = draw.textsize(subtitle, font=font)
tx = (w - tw) // 2
ty = h + (bar_h - th) // 2
draw.text((tx, ty), subtitle, font=font, fill="white")
# โ‘ฃ ๋ฐˆ ํ…์ŠคํŠธ (์ƒ๋‹จยทํ•˜๋‹จ)
meme_font_size = int(h * 0.07)
try:
meme_font = ImageFont.truetype(font_path, size=meme_font_size)
except:
meme_font = ImageFont.load_default()
# ์ƒ๋‹จ ํ…์ŠคํŠธ
if top_text:
text = top_text.upper()
tw, th = draw.textsize(text, font=meme_font)
draw.text(((w - tw)//2, 10), text, font=meme_font, fill="white", stroke_width=2, stroke_fill="black")
# ํ•˜๋‹จ ํ…์ŠคํŠธ (์ด๋ฏธ์ง€ ์˜์—ญ ์•ˆ์ชฝ)
if bottom_text:
text = bottom_text.upper()
tw, th = draw.textsize(text, font=meme_font)
draw.text(((w - tw)//2, h - th - 10), text, font=meme_font, fill="white", stroke_width=2, stroke_fill="black")
return combined
# 3) Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## ๐Ÿ“ธ ๋ฐˆ ์ƒ์„ฑ + ์ž๋™ ์˜์–ด ์ž๋ง‰ ํ•ฉ์„ฑ")
gr.Markdown("์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๊ณ , ์ƒ๋‹จยทํ•˜๋‹จ ๋ฐˆ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด\n์ž๋™ ์ƒ์„ฑ๋œ ์˜์–ด ์ž๋ง‰๊ณผ ํ•จ๊ป˜ ๋ฐˆ ์Šคํƒ€์ผ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.")
img_in = gr.Image(type="filepath", label="Upload Image")
top_txt = gr.Textbox(label="Top Text (optional)", placeholder="e.g. WHEN YOU REALIZE...", lines=1)
bottom_txt = gr.Textbox(label="Bottom Text (optional)", placeholder="e.g. ...IT'S MONDAY AGAIN", lines=1)
btn = gr.Button("Generate Meme")
out_img = gr.Image(label="Meme with Subtitle")
btn.click(fn=make_meme_with_subtitle, inputs=[img_in, top_txt, bottom_txt], outputs=out_img)
# 4) ์•ฑ ์‹คํ–‰
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=int(os.environ.get("PORT", 7860))
)