englissi commited on
Commit
6e28cb9
ยท
verified ยท
1 Parent(s): cef7916

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # 1) ์ด๋ฏธ์ง€ ์บก์…”๋‹ ํŒŒ์ดํ”„๋ผ์ธ ์ดˆ๊ธฐํ™”
5
+ captioner = pipeline("image-captioning", model="Salesforce/blip-image-captioning-base")
6
+
7
+ # 2) (์˜ต์…˜) ์Šคํƒ€์ผ ๋ณ€ํ™˜ ํ•จ์ˆ˜
8
+ def style_convert(raw_caption, style):
9
+ if style == "TOEIC Speaking Part 1":
10
+ # Partโ€ฏ1: โ€œWhat do you see in the picture?โ€
11
+ # ๋‹ต๋ณ€: โ€œI see ~.โ€ ํ•œ ๋ฌธ์žฅ
12
+ return f"Q: What do you see in the picture?\nA: {raw_caption.capitalize()}."
13
+ elif style == "IELTS Describe a Photo":
14
+ return f"Describe the photo in two sentences:\n1. {raw_caption.capitalize()}.\n2. It also shows the context of daily life."
15
+ else:
16
+ return raw_caption
17
+
18
+ # 3) Gradio ์ธํ„ฐํŽ˜์ด์Šค ํ•จ์ˆ˜
19
+ def generate_caption(image, style):
20
+ # 3.1 ์ด๋ฏธ์ง€ ์บก์…”๋‹
21
+ result = captioner(image, max_length=30, num_beams=3)[0]["caption"]
22
+ # 3.2 ์Šคํƒ€์ผ ๋ณ€ํ™˜
23
+ return style_convert(result, style)
24
+
25
+ # 4) Gradio Blocks ์ •์˜
26
+ with gr.Blocks() as demo:
27
+ gr.Markdown("## ๐Ÿ“ธ ์ด๋ฏธ์ง€ ์บก์…”๋‹ โ†’ English Test ์Šคํƒ€์ผ ๋ฌธ์žฅ ์ƒ์„ฑ")
28
+ with gr.Row():
29
+ img_in = gr.Image(type="pil", label="Upload Image")
30
+ style_sel = gr.Dropdown(
31
+ choices=["Raw Caption", "TOEIC Speaking Part 1", "IELTS Describe a Photo"],
32
+ value="TOEIC Speaking Part 1",
33
+ label="์‹œํ—˜ ํ˜•์‹ ์„ ํƒ"
34
+ )
35
+ output = gr.Textbox(label="Generated Caption", lines=4)
36
+ btn = gr.Button("Generate")
37
+ btn.click(fn=generate_caption, inputs=[img_in, style_sel], outputs=output)
38
+
39
+ # 5) ์•ฑ ์‹คํ–‰
40
+ if __name__ == "__main__":
41
+ demo.launch()