Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- lab3/README.md +41 -0
- lab3/app.py +83 -0
- lab3/requirements.txt +4 -0
lab3/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Vision Language AI Web App (HW3)
|
| 2 |
+
|
| 3 |
+
* **學號:** 314831001
|
| 4 |
+
* **姓名:** 詹沐恩
|
| 5 |
+
|
| 6 |
+
## 專案介紹
|
| 7 |
+
|
| 8 |
+
這份作業實作了一個 Web UI 應用程式,用於展示 Vision Language AI 的功能。
|
| 9 |
+
|
| 10 |
+
我選擇實作的核心功能是 **圖片描述 (Image Captioning)**。
|
| 11 |
+
|
| 12 |
+
* **AI 模型:** `nlpconnect/vit-gpt2-image-captioning`
|
| 13 |
+
* **UI 框架:** `Gradio`
|
| 14 |
+
|
| 15 |
+
## 功能
|
| 16 |
+
|
| 17 |
+
使用者可以上傳任何圖片,應用程式將會返回一段由 AI 自動生成的英文描述文字。
|
| 18 |
+
|
| 19 |
+
## 如何在本機運行 (Self-Hosted)
|
| 20 |
+
|
| 21 |
+
1. 確認已安裝 Python 3.8+
|
| 22 |
+
2. (建議) 建立並啟動虛擬環境:
|
| 23 |
+
```bash
|
| 24 |
+
python -m venv venv
|
| 25 |
+
source venv/bin/activate # on Windows: venv\Scripts\activate
|
| 26 |
+
```
|
| 27 |
+
3. 安裝所有必要的套件:
|
| 28 |
+
```bash
|
| 29 |
+
pip install -r requirements.txt
|
| 30 |
+
```
|
| 31 |
+
4. 啟動應用程式:
|
| 32 |
+
```bash
|
| 33 |
+
python app.py
|
| 34 |
+
```
|
| 35 |
+
5. 在瀏覽器中開啟 `http://127.0.0.1:7860` 即可使用。
|
| 36 |
+
|
| 37 |
+
## Hugging Face Spaces
|
| 38 |
+
|
| 39 |
+
您也可以直接透過以下的 Hugging Face Spaces 連結來體驗這個 App:
|
| 40 |
+
|
| 41 |
+
(如果你上傳到 HF Spaces,請把公開網址貼在這裡)
|
lab3/app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# --- 1. 載入 AI 模型 ---
|
| 6 |
+
# 使用 Hugging Face pipeline 快速載入預訓練模型
|
| 7 |
+
# 任務: "image-to-text" (圖片轉文字)
|
| 8 |
+
# 模型: "nlpconnect/vit-gpt2-image-captioning"
|
| 9 |
+
#
|
| 10 |
+
# 第一次執行時,會自動下載模型 (可能需要幾分鐘)
|
| 11 |
+
print("正在載入 AI 模型...")
|
| 12 |
+
# 確保模型在 CPU 上運行 (符合作業的 Zero GPU/CPU 要求)
|
| 13 |
+
captioner = pipeline("image-to-text",
|
| 14 |
+
model="nlpconnect/vit-gpt2-image-captioning",
|
| 15 |
+
device="cpu")
|
| 16 |
+
print("模型載入完成。")
|
| 17 |
+
|
| 18 |
+
# --- 2. 定義核心 AI 功能 ---
|
| 19 |
+
def generate_caption(input_image: Image.Image) -> str:
|
| 20 |
+
"""
|
| 21 |
+
接收一個 PIL Image 物件,返回 AI 生成的描述文字。
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
input_image: Gradio 傳入的 PIL 圖片物件。
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
一個描述圖片的字串。
|
| 28 |
+
"""
|
| 29 |
+
if input_image is None:
|
| 30 |
+
return "錯誤:請先上傳圖片。"
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# 使用 pipeline 進行預測
|
| 34 |
+
# pipeline 會返回一個 list,其中包含一個 dict
|
| 35 |
+
# result 格式: [{'generated_text': 'a cat sitting on a couch'}]
|
| 36 |
+
result = captioner(input_image)
|
| 37 |
+
caption = result[0]['generated_text']
|
| 38 |
+
|
| 39 |
+
# 對輸出做一點清理,讓它更易讀
|
| 40 |
+
return caption.capitalize()
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"處理圖片時發生錯誤: {e}")
|
| 44 |
+
return f"發生錯誤: {e}"
|
| 45 |
+
|
| 46 |
+
# --- 3. 建立 Gradio Web UI 介面 ---
|
| 47 |
+
|
| 48 |
+
# 定義輸入介面:
|
| 49 |
+
# type="pil" 確保傳入 Python 函式的是 PIL Image 物件
|
| 50 |
+
input_component = gr.Image(type="pil", label="上傳圖片 (Upload Image)")
|
| 51 |
+
|
| 52 |
+
# 定義輸出介面:
|
| 53 |
+
output_component = gr.Textbox(label="AI 產生的圖片描述 (AI-Generated Caption)")
|
| 54 |
+
|
| 55 |
+
# 讀取範例圖片 (請確保你已在 sample_images/ 資料夾放入對應檔案)
|
| 56 |
+
# 這是為了讓使用者一點就知道怎麼用
|
| 57 |
+
example_list = [
|
| 58 |
+
["sample_images/cat.jpg"],
|
| 59 |
+
["sample_images/dog_park.jpg"],
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
# 組合介面
|
| 63 |
+
iface = gr.Interface(
|
| 64 |
+
fn=generate_caption, # 要呼叫的函式
|
| 65 |
+
inputs=input_component, # 輸入元件
|
| 66 |
+
outputs=output_component, # 輸出元件
|
| 67 |
+
|
| 68 |
+
# 介面標題與說明
|
| 69 |
+
title="Vision Language AI:智慧圖片描述",
|
| 70 |
+
description="這是一個使用 Gradio 和 Hugging Face Transformers 打造的 Web App。\n上傳一張圖片,AI 將會自動為您產生一段描述文字。",
|
| 71 |
+
|
| 72 |
+
# 提供範例
|
| 73 |
+
examples=example_list,
|
| 74 |
+
|
| 75 |
+
# 介面主題 (可選)
|
| 76 |
+
theme=gr.themes.Soft()
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# --- 4. 啟動 Web App ---
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
# share=True 會產生一個公開的臨時網址,方便分享
|
| 82 |
+
# (但在 HF Spaces 上部署時不需要 share=True)
|
| 83 |
+
iface.launch()
|
lab3/requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
Pillow
|