Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# app.py
|
2 |
import os
|
3 |
from PIL import Image, ImageDraw, ImageFont
|
4 |
import gradio as gr
|
@@ -8,17 +7,23 @@ from transformers import pipeline
|
|
8 |
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
9 |
|
10 |
# 2) λ° + μλ§ ν©μ± ν¨μ
|
11 |
-
def make_meme_with_subtitle(
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
14 |
w, h = img.size
|
15 |
|
16 |
# β μλ μΊ‘μ
μμ±
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
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
|
@@ -27,21 +32,22 @@ def make_meme_with_subtitle(image_path, top_text, bottom_text):
|
|
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)
|
@@ -52,30 +58,29 @@ def make_meme_with_subtitle(image_path, top_text, bottom_text):
|
|
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("μ΄λ―Έμ§λ₯Ό μ
λ‘λνκ³ , μλ¨Β·νλ¨ λ° ν
μ€νΈλ₯Ό
|
69 |
-
|
|
|
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 |
-
)
|
|
|
|
|
1 |
import os
|
2 |
from PIL import Image, ImageDraw, ImageFont
|
3 |
import gradio as gr
|
|
|
7 |
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
8 |
|
9 |
# 2) λ° + μλ§ ν©μ± ν¨μ
|
10 |
+
def make_meme_with_subtitle(image, top_text, bottom_text):
|
11 |
+
if image is None:
|
12 |
+
raise ValueError("μ΄λ―Έμ§κ° μ
λ‘λλμ§ μμμ΅λλ€.")
|
13 |
+
|
14 |
+
# μ΄λ―Έμ§ λ³ν
|
15 |
+
img = image.convert("RGB")
|
16 |
w, h = img.size
|
17 |
|
18 |
# β μλ μΊ‘μ
μμ±
|
19 |
+
try:
|
20 |
+
out = captioner(img, max_length=40, num_beams=5)
|
21 |
+
raw = out[0].get("generated_text") or out[0].get("text") or ""
|
22 |
+
subtitle = raw.strip().capitalize() if raw else "No caption found"
|
23 |
+
except Exception as e:
|
24 |
+
subtitle = "No caption (error)"
|
25 |
|
26 |
+
# β‘ μλ§μ© κ²μ λ μμ±
|
27 |
bar_h = int(h * 0.15)
|
28 |
bar = Image.new("RGB", (w, bar_h), color="black")
|
29 |
combined_h = h + bar_h
|
|
|
32 |
combined.paste(bar, (0, h))
|
33 |
|
34 |
draw = ImageDraw.Draw(combined)
|
35 |
+
|
36 |
+
# β’ μλ§ ν°νΈ μ€μ
|
37 |
try:
|
38 |
+
font_path = "arialbd.ttf" # μμ€ν
μ ν΄λΉ ν°νΈκ° μμ κ²½μ° μ€λ₯
|
39 |
base_font_size = int(bar_h * 0.5)
|
40 |
font = ImageFont.truetype(font_path, size=base_font_size)
|
41 |
except:
|
42 |
font = ImageFont.load_default()
|
43 |
|
44 |
+
# β£ μλ§ κ·Έλ¦¬κΈ° (νλ¨ μ€μ)
|
45 |
tw, th = draw.textsize(subtitle, font=font)
|
46 |
tx = (w - tw) // 2
|
47 |
ty = h + (bar_h - th) // 2
|
48 |
draw.text((tx, ty), subtitle, font=font, fill="white")
|
49 |
|
50 |
+
# β€ λ° ν
μ€νΈ ν°νΈ μ€μ
|
51 |
meme_font_size = int(h * 0.07)
|
52 |
try:
|
53 |
meme_font = ImageFont.truetype(font_path, size=meme_font_size)
|
|
|
58 |
if top_text:
|
59 |
text = top_text.upper()
|
60 |
tw, th = draw.textsize(text, font=meme_font)
|
61 |
+
draw.text(((w - tw) // 2, 10), text, font=meme_font, fill="white", stroke_width=2, stroke_fill="black")
|
62 |
|
63 |
+
# νλ¨ ν
μ€νΈ (μ΄λ―Έμ§ λ΄λΆ)
|
64 |
if bottom_text:
|
65 |
text = bottom_text.upper()
|
66 |
tw, th = draw.textsize(text, font=meme_font)
|
67 |
+
draw.text(((w - tw) // 2, h - th - 10), text, font=meme_font, fill="white", stroke_width=2, stroke_fill="black")
|
68 |
|
69 |
return combined
|
70 |
|
71 |
# 3) Gradio UI
|
72 |
with gr.Blocks() as demo:
|
73 |
gr.Markdown("## πΈ λ° μμ± + μλ μμ΄ μλ§ ν©μ±")
|
74 |
+
gr.Markdown("μ΄λ―Έμ§λ₯Ό μ
λ‘λνκ³ , μλ¨Β·νλ¨ λ° ν
μ€νΈλ₯Ό μ
λ ₯νλ©΄ μλ μμ±λ μμ΄ μλ§κ³Ό ν¨κ» λ° μ€νμΌ μ΄λ―Έμ§λ₯Ό λ°νν©λλ€.")
|
75 |
+
|
76 |
+
img_in = gr.Image(type="pil", label="Upload Image") # filepath β pil
|
77 |
top_txt = gr.Textbox(label="Top Text (optional)", placeholder="e.g. WHEN YOU REALIZE...", lines=1)
|
78 |
bottom_txt = gr.Textbox(label="Bottom Text (optional)", placeholder="e.g. ...IT'S MONDAY AGAIN", lines=1)
|
79 |
btn = gr.Button("Generate Meme")
|
80 |
out_img = gr.Image(label="Meme with Subtitle")
|
81 |
+
|
82 |
btn.click(fn=make_meme_with_subtitle, inputs=[img_in, top_txt, bottom_txt], outputs=out_img)
|
83 |
|
84 |
# 4) μ± μ€ν
|
85 |
if __name__ == "__main__":
|
86 |
+
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
|
|
|
|
|