Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
# 1) μ΄λ―Έμ§ μΊ‘μ λ νμ΄νλΌμΈ μ΄κΈ°ν | |
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") | |
def generate_caption(image, style): | |
# 1) μλ³Έ μΊ‘μ μ»κΈ° | |
output = captioner(image) | |
# output μμ: [{"generated_text": "..."}] λλ [{"text": "..."}] νΉμ ["..."] | |
# μμ νκ² μΆμΆ | |
if isinstance(output, list) and output: | |
first = output[0] | |
if isinstance(first, dict): | |
raw_caption = first.get("generated_text") or first.get("text") or "" | |
else: | |
raw_caption = str(first) | |
else: | |
raw_caption = str(output) | |
raw_caption = raw_caption.strip() | |
# 2) μ€νμΌ λ³ν | |
if style == "TOEIC Speaking Part 1": | |
return f"Q: What do you see in the picture?\nA: {raw_caption.capitalize()}." | |
elif style == "IELTS Describe a Photo": | |
return ( | |
"Describe the photo in two sentences:\n" | |
f"1. {raw_caption.capitalize()}.\n" | |
"2. It also shows the context of daily life." | |
) | |
else: | |
return raw_caption | |
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) | |
if __name__ == "__main__": | |
demo.launch() | |