File size: 1,769 Bytes
6e28cb9
 
 
 
410aa7d
6e28cb9
16ac547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e28cb9
 
 
16ac547
 
 
 
 
6e28cb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()