englissi commited on
Commit
1ca8456
Β·
verified Β·
1 Parent(s): f9dbeb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
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(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
@@ -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("이미지λ₯Ό μ—…λ‘œλ“œν•˜κ³ , μƒλ‹¨Β·ν•˜λ‹¨ 밈 ν…μŠ€νŠΈλ₯Ό μž…λ ₯ν•˜λ©΄\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
- )
 
 
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)))