englissi commited on
Commit
16ac547
ยท
verified ยท
1 Parent(s): 410aa7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -9
app.py CHANGED
@@ -4,21 +4,33 @@ from transformers import pipeline
4
  # 1) ์ด๋ฏธ์ง€ ์บก์…”๋‹ ํŒŒ์ดํ”„๋ผ์ธ ์ดˆ๊ธฐํ™”
5
  captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
6
 
7
- # 2) ์Šคํƒ€์ผ ๋ณ€ํ™˜ ํ•จ์ˆ˜
8
- def style_convert(raw_caption, style):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  if style == "TOEIC Speaking Part 1":
10
  return f"Q: What do you see in the picture?\nA: {raw_caption.capitalize()}."
11
  elif style == "IELTS Describe a Photo":
12
- return f"Describe the photo in two sentences:\n1. {raw_caption.capitalize()}.\n2. It also shows the context of daily life."
 
 
 
 
13
  else:
14
  return raw_caption
15
 
16
- # 3) Gradio ์ธํ„ฐํŽ˜์ด์Šค ํ•จ์ˆ˜
17
- def generate_caption(image, style):
18
- result = captioner(image, max_length=30, num_beams=3)[0]["generated_text"]
19
- return style_convert(result, style)
20
-
21
- # 4) Gradio Blocks ์ •์˜
22
  with gr.Blocks() as demo:
23
  gr.Markdown("## ๐Ÿ“ธ ์ด๋ฏธ์ง€ ์บก์…”๋‹ โ†’ English Test ์Šคํƒ€์ผ ๋ฌธ์žฅ ์ƒ์„ฑ")
24
  with gr.Row():
 
4
  # 1) ์ด๋ฏธ์ง€ ์บก์…”๋‹ ํŒŒ์ดํ”„๋ผ์ธ ์ดˆ๊ธฐํ™”
5
  captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
6
 
7
+ def generate_caption(image, style):
8
+ # 1) ์›๋ณธ ์บก์…˜ ์–ป๊ธฐ
9
+ output = captioner(image)
10
+ # output ์˜ˆ์‹œ: [{"generated_text": "..."}] ๋˜๋Š” [{"text": "..."}] ํ˜น์€ ["..."]
11
+ # ์•ˆ์ „ํ•˜๊ฒŒ ์ถ”์ถœ
12
+ if isinstance(output, list) and output:
13
+ first = output[0]
14
+ if isinstance(first, dict):
15
+ raw_caption = first.get("generated_text") or first.get("text") or ""
16
+ else:
17
+ raw_caption = str(first)
18
+ else:
19
+ raw_caption = str(output)
20
+
21
+ raw_caption = raw_caption.strip()
22
+ # 2) ์Šคํƒ€์ผ ๋ณ€ํ™˜
23
  if style == "TOEIC Speaking Part 1":
24
  return f"Q: What do you see in the picture?\nA: {raw_caption.capitalize()}."
25
  elif style == "IELTS Describe a Photo":
26
+ return (
27
+ "Describe the photo in two sentences:\n"
28
+ f"1. {raw_caption.capitalize()}.\n"
29
+ "2. It also shows the context of daily life."
30
+ )
31
  else:
32
  return raw_caption
33
 
 
 
 
 
 
 
34
  with gr.Blocks() as demo:
35
  gr.Markdown("## ๐Ÿ“ธ ์ด๋ฏธ์ง€ ์บก์…”๋‹ โ†’ English Test ์Šคํƒ€์ผ ๋ฌธ์žฅ ์ƒ์„ฑ")
36
  with gr.Row():