englissi commited on
Commit
31447f5
·
verified ·
1 Parent(s): cd00a55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -63
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
- 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",
59
- label="Which image to describe? (0–3)"
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