Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,57 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
# 1) ์ด๋ฏธ์ง ์บก์
๋ ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
5 |
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
|
|
|
|
|
|
|
|
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 |
with gr.Blocks() as demo:
|
35 |
-
gr.Markdown("## ๐ธ
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
btn.click(fn=generate_caption, inputs=[
|
46 |
|
47 |
if __name__ == "__main__":
|
48 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
|
|
4 |
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
5 |
+
scene_classifier = pipeline(
|
6 |
+
"zero-shot-image-classification",
|
7 |
+
model="openai/clip-vit-base-patch32"
|
8 |
+
)
|
9 |
|
10 |
+
SCENE_LABELS = ["outdoor", "indoor", "beach", "office", "street",
|
11 |
+
"restaurant", "park", "sports", "kitchen", "mountain"]
|
12 |
+
TEMPLATES = {
|
13 |
+
"outdoor": "In this picture, {caption}. It looks like a pleasant outdoor setting, and the subject seems relaxed.",
|
14 |
+
"indoor": "In this picture, {caption}. It appears to be indoors, perhaps at home or in an office environment.",
|
15 |
+
"beach": "In this picture, {caption}. It seems to be on a beach, and the atmosphere looks warm and sunny.",
|
16 |
+
"office": "In this picture, {caption}. It looks like an office scene, with people engaged in work or discussion.",
|
17 |
+
"street": "In this picture, {caption}. The scene appears to be on a busy street, with vehicles and pedestrians.",
|
18 |
+
"restaurant": "In this picture, {caption}. It looks like a restaurant setting, where people are dining together.",
|
19 |
+
"park": "In this picture, {caption}. The location seems to be a park, with trees and open space.",
|
20 |
+
"sports": "In this picture, {caption}. It appears to be a sports activity, showing movement and action.",
|
21 |
+
"kitchen": "In this picture, {caption}. It seems to be in a kitchen, with cooking utensils visible.",
|
22 |
+
"mountain": "In this picture, {caption}. The background looks like mountains, suggesting a hiking scene."
|
23 |
+
}
|
24 |
+
|
25 |
+
def generate_caption(images, choice_index):
|
26 |
+
idx = int(choice_index)
|
27 |
+
img = images[idx]
|
28 |
+
|
29 |
+
# raw caption
|
30 |
+
out = captioner(img)
|
31 |
+
first = out[0] if isinstance(out, list) else out
|
32 |
+
raw = first.get("generated_text") or first.get("text") or str(first)
|
33 |
+
raw = raw.strip()
|
34 |
+
|
35 |
+
# scene classification
|
36 |
+
cls = scene_classifier(img, candidate_labels=SCENE_LABELS)
|
37 |
+
scene = cls["labels"][0]
|
38 |
+
|
39 |
+
# template mapping
|
40 |
+
template = TEMPLATES.get(scene, "In this picture, {caption}.")
|
41 |
+
return template.format(caption=raw)
|
42 |
|
43 |
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown("## ๐ธ TOEIC Partโฏ1: ์ํฉ๋ณ ์ฌ์ง ๋ฌ์ฌ")
|
45 |
+
img_inputs = gr.Files(file_count="multiple", type="pil",
|
46 |
+
label="Upload up to 4 images")
|
47 |
+
choice = gr.Dropdown(
|
48 |
+
choices=[str(i) for i in range(4)],
|
49 |
+
value="0",
|
50 |
+
label="Which image to describe? (0โ3)"
|
51 |
+
)
|
52 |
+
btn = gr.Button("Describe")
|
53 |
+
output = gr.Textbox(label="TOEIC Partโฏ1 Response", lines=4)
|
54 |
+
btn.click(fn=generate_caption, inputs=[img_inputs, choice], outputs=output)
|
55 |
|
56 |
if __name__ == "__main__":
|
57 |
demo.launch()
|