Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,8 @@
|
|
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",
|
@@ -22,28 +24,35 @@ TEMPLATES = {
|
|
22 |
"mountain": "In this picture, {caption}. The background looks like mountains, suggesting a hiking scene."
|
23 |
}
|
24 |
|
25 |
-
def generate_caption(
|
26 |
idx = int(choice_index)
|
27 |
-
|
28 |
|
29 |
-
#
|
|
|
|
|
|
|
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 |
-
#
|
36 |
cls = scene_classifier(img, candidate_labels=SCENE_LABELS)
|
37 |
scene = cls["labels"][0]
|
38 |
|
39 |
-
#
|
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 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
47 |
choice = gr.Dropdown(
|
48 |
choices=[str(i) for i in range(4)],
|
49 |
value="0",
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
|
5 |
+
# ① 파이프라인 초기화
|
6 |
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
7 |
scene_classifier = pipeline(
|
8 |
"zero-shot-image-classification",
|
|
|
24 |
"mountain": "In this picture, {caption}. The background looks like mountains, suggesting a hiking scene."
|
25 |
}
|
26 |
|
27 |
+
def generate_caption(filepaths, choice_index):
|
28 |
idx = int(choice_index)
|
29 |
+
img_path = filepaths[idx]
|
30 |
|
31 |
+
# PIL로 이미지 로드
|
32 |
+
img = Image.open(img_path).convert("RGB")
|
33 |
+
|
34 |
+
# 1) 원본 캡션
|
35 |
out = captioner(img)
|
36 |
first = out[0] if isinstance(out, list) else out
|
37 |
raw = first.get("generated_text") or first.get("text") or str(first)
|
38 |
raw = raw.strip()
|
39 |
|
40 |
+
# 2) 장면 분류
|
41 |
cls = scene_classifier(img, candidate_labels=SCENE_LABELS)
|
42 |
scene = cls["labels"][0]
|
43 |
|
44 |
+
# 3) 템플릿 매핑
|
45 |
template = TEMPLATES.get(scene, "In this picture, {caption}.")
|
46 |
return template.format(caption=raw)
|
47 |
|
48 |
with gr.Blocks() as demo:
|
49 |
gr.Markdown("## 📸 TOEIC Part 1: 상황별 사진 묘사")
|
50 |
+
# Multiple files 업로드, filepath로 받기
|
51 |
+
img_inputs = gr.Files(
|
52 |
+
file_count="multiple",
|
53 |
+
type="filepath",
|
54 |
+
label="Upload up to 4 images"
|
55 |
+
)
|
56 |
choice = gr.Dropdown(
|
57 |
choices=[str(i) for i in range(4)],
|
58 |
value="0",
|