Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
# 1) ์ด๋ฏธ์ง ์บก์ ๋ ํ์ดํ๋ผ์ธ ์ด๊ธฐํ | |
captioner = pipeline("image-captioning", model="Salesforce/blip-image-captioning-base") | |
# 2) (์ต์ ) ์คํ์ผ ๋ณํ ํจ์ | |
def style_convert(raw_caption, style): | |
if style == "TOEIC Speaking Part 1": | |
# Partโฏ1: โWhat do you see in the picture?โ | |
# ๋ต๋ณ: โI see ~.โ ํ ๋ฌธ์ฅ | |
return f"Q: What do you see in the picture?\nA: {raw_caption.capitalize()}." | |
elif style == "IELTS Describe a Photo": | |
return f"Describe the photo in two sentences:\n1. {raw_caption.capitalize()}.\n2. It also shows the context of daily life." | |
else: | |
return raw_caption | |
# 3) Gradio ์ธํฐํ์ด์ค ํจ์ | |
def generate_caption(image, style): | |
# 3.1 ์ด๋ฏธ์ง ์บก์ ๋ | |
result = captioner(image, max_length=30, num_beams=3)[0]["caption"] | |
# 3.2 ์คํ์ผ ๋ณํ | |
return style_convert(result, style) | |
# 4) Gradio Blocks ์ ์ | |
with gr.Blocks() as demo: | |
gr.Markdown("## ๐ธ ์ด๋ฏธ์ง ์บก์ ๋ โ English Test ์คํ์ผ ๋ฌธ์ฅ ์์ฑ") | |
with gr.Row(): | |
img_in = gr.Image(type="pil", label="Upload Image") | |
style_sel = gr.Dropdown( | |
choices=["Raw Caption", "TOEIC Speaking Part 1", "IELTS Describe a Photo"], | |
value="TOEIC Speaking Part 1", | |
label="์ํ ํ์ ์ ํ" | |
) | |
output = gr.Textbox(label="Generated Caption", lines=4) | |
btn = gr.Button("Generate") | |
btn.click(fn=generate_caption, inputs=[img_in, style_sel], outputs=output) | |
# 5) ์ฑ ์คํ | |
if __name__ == "__main__": | |
demo.launch() | |