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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -7
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(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",
 
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",