Spaces:
Sleeping
Sleeping
Create app.py
Browse files
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()
|