Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,37 @@
|
|
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",
|
9 |
-
model="openai/clip-vit-base-patch32"
|
10 |
-
)
|
11 |
-
|
12 |
-
SCENE_LABELS = ["outdoor", "indoor", "beach", "office", "street",
|
13 |
-
"restaurant", "park", "sports", "kitchen", "mountain"]
|
14 |
-
TEMPLATES = {
|
15 |
-
"outdoor": "In this picture, {caption}. It looks like a pleasant outdoor setting, and the subject seems relaxed.",
|
16 |
-
"indoor": "In this picture, {caption}. It appears to be indoors, perhaps at home or in an office environment.",
|
17 |
-
"beach": "In this picture, {caption}. It seems to be on a beach, and the atmosphere looks warm and sunny.",
|
18 |
-
"office": "In this picture, {caption}. It looks like an office scene, with people engaged in work or discussion.",
|
19 |
-
"street": "In this picture, {caption}. The scene appears to be on a busy street, with vehicles and pedestrians.",
|
20 |
-
"restaurant": "In this picture, {caption}. It looks like a restaurant setting, where people are dining together.",
|
21 |
-
"park": "In this picture, {caption}. The location seems to be a park, with trees and open space.",
|
22 |
-
"sports": "In this picture, {caption}. It appears to be a sports activity, showing movement and action.",
|
23 |
-
"kitchen": "In this picture, {caption}. It seems to be in a kitchen, with cooking utensils visible.",
|
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 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
btn = gr.Button("Describe")
|
62 |
-
output = gr.Textbox(label="TOEIC Part 1 Response", lines=4)
|
63 |
-
btn.click(fn=generate_caption, inputs=[img_inputs, choice], outputs=output)
|
64 |
-
|
65 |
-
if __name__ == "__main__":
|
66 |
-
demo.launch()
|
|
|
|
|
|
|
1 |
from PIL import Image
|
2 |
+
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
def generate_caption(filepaths, choice_index):
|
5 |
+
try:
|
6 |
+
idx = int(choice_index)
|
7 |
+
img_path = filepaths[idx]
|
8 |
+
print(f"[DEBUG] Selected image path: {img_path}")
|
9 |
+
|
10 |
+
# 1) 이미지 로드
|
11 |
+
img = Image.open(img_path).convert("RGB")
|
12 |
+
print("[DEBUG] Image loaded")
|
13 |
+
|
14 |
+
# 2) 캡션 생성
|
15 |
+
out = captioner(img)
|
16 |
+
print(f"[DEBUG] captioner output: {out!r}")
|
17 |
+
first = out[0] if isinstance(out, list) else out
|
18 |
+
raw = first.get("generated_text") or first.get("text") or str(first)
|
19 |
+
raw = raw.strip()
|
20 |
+
print(f"[DEBUG] raw caption: {raw!r}")
|
21 |
+
|
22 |
+
# 3) 장면 분류
|
23 |
+
cls = scene_classifier(img, candidate_labels=SCENE_LABELS)
|
24 |
+
print(f"[DEBUG] scene_classifier output: {cls!r}")
|
25 |
+
scene = cls["labels"][0]
|
26 |
+
|
27 |
+
# 4) 템플릿 매핑
|
28 |
+
template = TEMPLATES.get(scene, "In this picture, {caption}.")
|
29 |
+
result = template.format(caption=raw)
|
30 |
+
print(f"[DEBUG] Final result: {result}")
|
31 |
+
return result
|
32 |
+
|
33 |
+
except Exception as e:
|
34 |
+
# 에러 메시지와 스택 트레이스 리턴
|
35 |
+
error_msg = f"🔴 Error:\n{e}\n\n{traceback.format_exc()}"
|
36 |
+
print(error_msg)
|
37 |
+
return error_msg
|
|
|
|
|
|
|
|
|
|
|
|