jackwang220 commited on
Commit
b7a3fd8
·
verified ·
1 Parent(s): 5df94bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -95
app.py CHANGED
@@ -1,70 +1,61 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
 
4
 
5
- print("--- 程式開始執行 ---")
 
 
6
 
7
- # 1. 載入模型
8
- # ---------------------------------------------------
9
- print("開始載入 Image Captioning 模型...")
10
- captioner = pipeline(
11
- "image-to-text",
12
- model="nlpconnect/vit-gpt2-image-captioning"
13
- )
14
- print("Image Captioning 模型載入完成。")
 
15
 
16
- print("開始載入 VQA 模型...")
17
- vqa = pipeline(
18
- "visual-question-answering",
19
- model="dandelin/vilt-b32-finetuned-vqa"
20
- )
21
- print("VQA 模型載入完成。")
 
 
 
22
 
23
-
24
- # 2. 定義 AI 功能的函式
25
- # ---------------------------------------------------
26
- def image_captioning(image):
27
- """
28
- 看圖說故事
29
- """
30
  if image is None:
31
  return "請先上傳圖片"
32
- print("正在執行 image_captioning...")
33
- result = captioner(image)
34
- print("Captioning 結果:", result[0]['generated_text'])
35
- return result[0]['generated_text']
36
 
37
- def visual_question_answer(image, question):
38
- """
39
- 視覺問答
40
- """
41
  if image is None:
42
  return "請先上傳圖片"
43
  if not question:
44
  return "請輸入問題"
45
-
46
- print(f"正在執行 VQA... 問題: {question}")
47
- result = vqa(image, question)
48
- print("VQA 結果:", result[0]['answer'])
49
- return result[0]['answer']
50
 
51
- # 3. 建立 Gradio 介面
52
- # ---------------------------------------------------
53
- print("開始建立 Gradio 介面...")
54
  with gr.Blocks(title="Vision Language AI 應用") as demo:
55
  gr.Markdown("# 🎨 Vision Language AI 應用展示")
56
  gr.Markdown("這是一個展示 Vision-Language (VL) 模型的 Web UI,由 Gradio 和 Hugging Face Transformers 驅動。")
57
 
58
  with gr.Tabs():
59
-
60
- # --- 分頁 1: Image Captioning ---
61
  with gr.TabItem("🖼️ 看圖說故事 (Image Captioning)"):
62
  with gr.Row():
63
  image_input_caption = gr.Image(type="pil", label="上傳圖片")
64
  text_output_caption = gr.Textbox(label="AI 產生的描述")
65
  caption_button = gr.Button("開始產生描述")
66
 
67
- # --- 分頁 2: Visual Question Answering ---
68
  with gr.TabItem("❓ 視覺問答 (VQA)"):
69
  with gr.Row():
70
  image_input_vqa = gr.Image(type="pil", label="上傳圖片")
@@ -73,59 +64,12 @@ with gr.Blocks(title="Vision Language AI 應用") as demo:
73
  text_output_vqa = gr.Textbox(label="AI 的回答")
74
  vqa_button = gr.Button("開始問答")
75
 
76
- # 4. 綁定按鈕和函式 (***解決 No API found 的關鍵***)
77
- # ---------------------------------------------------
78
-
79
- print("開始綁定 '看圖說故事' 按鈕...")
80
- caption_button.click(
81
- fn=image_captioning,
82
- inputs=[image_input_caption],
83
- outputs=[text_output_caption]
84
- )
85
-
86
- print("開始綁定 '視覺問答' 按鈕...")
87
- vqa_button.click(
88
- fn=visual_question_answer,
89
- inputs=[image_input_vqa, question_input_vqa],
90
- outputs=[text_output_vqa]
91
- )
92
-
93
- print("按鈕綁定完成。")
94
-
95
- # ---------------------------------------------------
96
- # 5. 範例區 (***暫時停用來避開 TypeError Bug***)
97
- # ---------------------------------------------------
98
- """
99
- print("正在處理範例區...") # 這行不會被印出
100
- gr.Markdown("--- \n ### 範例圖片")
101
- gr.Examples(
102
- examples=[
103
- ["sample_images/cat.jpeg"],
104
- ["sample_images/human.jpg"],
105
- ["sample_images/clown.png"],
106
- ],
107
- inputs=[image_input_caption],
108
- outputs=[text_output_caption],
109
- fn=image_captioning,
110
- cache_examples=True
111
- )
112
- gr.Examples(
113
- examples=[
114
- ["sample_images/cat.jpeg", "What color is the cat?"],
115
- ["sample_images/human.jpg", "What is the gender of the human?"]
116
- ],
117
- inputs=[image_input_vqa, question_input_vqa],
118
- outputs=[text_output_vqa],
119
- fn=visual_question_answer,
120
- cache_examples=True
121
- )
122
- """
123
 
124
- print("Gradio 介面建立完成。")
 
 
125
 
126
- # 6. 啟動 Web App
127
- # ---------------------------------------------------
128
- if __name__ == "__main__":
129
- print("準備啟動 App...")
130
- demo.launch()
131
- print("App 應該已經啟動。")
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
4
+ import torch
5
 
6
+ # --- 延後載入:先設為 None,第一次呼叫功能時才載入 ---
7
+ _captioner = None
8
+ _vqa = None
9
 
10
+ def get_captioner():
11
+ global _captioner
12
+ if _captioner is None:
13
+ _captioner = pipeline(
14
+ "image-to-text",
15
+ model="nlpconnect/vit-gpt2-image-captioning",
16
+ device=0 if torch.cuda.is_available() else -1,
17
+ )
18
+ return _captioner
19
 
20
+ def get_vqa():
21
+ global _vqa
22
+ if _vqa is None:
23
+ _vqa = pipeline(
24
+ "visual-question-answering",
25
+ model="dandelin/vilt-b32-finetuned-vqa",
26
+ device=0 if torch.cuda.is_available() else -1,
27
+ )
28
+ return _vqa
29
 
30
+ # --- 功能 ---
31
+ def image_captioning(image: Image.Image):
 
 
 
 
 
32
  if image is None:
33
  return "請先上傳圖片"
34
+ pipe = get_captioner()
35
+ out = pipe(image)
36
+ return out[0]["generated_text"]
 
37
 
38
+ def visual_question_answer(image: Image.Image, question: str):
 
 
 
39
  if image is None:
40
  return "請先上傳圖片"
41
  if not question:
42
  return "請輸入問題"
43
+ pipe = get_vqa()
44
+ out = pipe(image, question)
45
+ return out[0]["answer"]
 
 
46
 
47
+ # --- UI ---
 
 
48
  with gr.Blocks(title="Vision Language AI 應用") as demo:
49
  gr.Markdown("# 🎨 Vision Language AI 應用展示")
50
  gr.Markdown("這是一個展示 Vision-Language (VL) 模型的 Web UI,由 Gradio 和 Hugging Face Transformers 驅動。")
51
 
52
  with gr.Tabs():
 
 
53
  with gr.TabItem("🖼️ 看圖說故事 (Image Captioning)"):
54
  with gr.Row():
55
  image_input_caption = gr.Image(type="pil", label="上傳圖片")
56
  text_output_caption = gr.Textbox(label="AI 產生的描述")
57
  caption_button = gr.Button("開始產生描述")
58
 
 
59
  with gr.TabItem("❓ 視覺問答 (VQA)"):
60
  with gr.Row():
61
  image_input_vqa = gr.Image(type="pil", label="上傳圖片")
 
64
  text_output_vqa = gr.Textbox(label="AI 的回答")
65
  vqa_button = gr.Button("開始問答")
66
 
67
+ caption_button.click(image_captioning, inputs=[image_input_caption], outputs=[text_output_caption])
68
+ vqa_button.click(visual_question_answer, inputs=[image_input_vqa, question_input_vqa], outputs=[text_output_vqa])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ # ★ 在 Spaces 讓後端自動啟動;本地端才需要 launch()
71
+ # demo.queue(concurrency_count=1, max_size=10) 放在全域即可
72
+ demo.queue(concurrency_count=1, max_size=10)
73
 
74
+ if __name__ == "__main__": # 只在本地 run 時才啟動
75
+ demo.launch()