Spaces:
Sleeping
Sleeping
Commit
·
33f1e89
0
Parent(s):
初始化食物卡路里檢測器專案 - 已針對HuggingFace Spaces優化
Browse files🍎 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
- .env.example +7 -0
- .gitignore +71 -0
- Dockerfile +11 -0
- README.md +187 -0
- app.py +184 -0
- packages.txt +1 -0
- requirements.txt +3 -0
- requirements_full.txt +75 -0
- run.sh +20 -0
.env.example
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Google AI API Key
|
| 2 |
+
# 請到 https://makersuite.google.com/app/apikey 取得你的API金鑰
|
| 3 |
+
GOOGLE_API_KEY=your_google_api_key_here
|
| 4 |
+
|
| 5 |
+
# HuggingFace Token (用於部署)
|
| 6 |
+
# 請到 https://huggingface.co/settings/tokens 取得你的token
|
| 7 |
+
HF_TOKEN=your_huggingface_token_here
|
.gitignore
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
*.so
|
| 6 |
+
.Python
|
| 7 |
+
build/
|
| 8 |
+
develop-eggs/
|
| 9 |
+
dist/
|
| 10 |
+
downloads/
|
| 11 |
+
eggs/
|
| 12 |
+
.eggs/
|
| 13 |
+
lib/
|
| 14 |
+
lib64/
|
| 15 |
+
parts/
|
| 16 |
+
sdist/
|
| 17 |
+
var/
|
| 18 |
+
wheels/
|
| 19 |
+
*.egg-info/
|
| 20 |
+
.installed.cfg
|
| 21 |
+
*.egg
|
| 22 |
+
MANIFEST
|
| 23 |
+
|
| 24 |
+
# Virtual Environment
|
| 25 |
+
.venv/
|
| 26 |
+
venv/
|
| 27 |
+
ENV/
|
| 28 |
+
env/
|
| 29 |
+
.ENV/
|
| 30 |
+
|
| 31 |
+
# Environment Variables
|
| 32 |
+
.env
|
| 33 |
+
.env.local
|
| 34 |
+
.env.production
|
| 35 |
+
.env.staging
|
| 36 |
+
|
| 37 |
+
# IDE
|
| 38 |
+
.vscode/
|
| 39 |
+
.idea/
|
| 40 |
+
*.swp
|
| 41 |
+
*.swo
|
| 42 |
+
*~
|
| 43 |
+
|
| 44 |
+
# OS
|
| 45 |
+
.DS_Store
|
| 46 |
+
.DS_Store?
|
| 47 |
+
._*
|
| 48 |
+
.Spotlight-V100
|
| 49 |
+
.Trashes
|
| 50 |
+
ehthumbs.db
|
| 51 |
+
Thumbs.db
|
| 52 |
+
|
| 53 |
+
# Logs
|
| 54 |
+
*.log
|
| 55 |
+
logs/
|
| 56 |
+
|
| 57 |
+
# Gradio
|
| 58 |
+
gradio_cached_examples/
|
| 59 |
+
flagged/
|
| 60 |
+
|
| 61 |
+
# Temporary files
|
| 62 |
+
*.tmp
|
| 63 |
+
*.temp
|
| 64 |
+
|
| 65 |
+
# HuggingFace
|
| 66 |
+
.huggingface/
|
| 67 |
+
|
| 68 |
+
# API Keys (extra safety)
|
| 69 |
+
*api_key*
|
| 70 |
+
*API_KEY*
|
| 71 |
+
secrets.txt
|
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
CMD ["python", "app.py"]
|
README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: 食物卡路里檢測器
|
| 3 |
+
emoji: 🍎
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.42.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# 🍎 食物卡路里檢測器
|
| 14 |
+
|
| 15 |
+
這是一個基於Gradio和Google AI的智慧食物卡路里檢測應用程式。使用者可以上傳食物照片,AI會自動識別食物種類並估算卡路里含量。
|
| 16 |
+
|
| 17 |
+
## ✨ 功能特色
|
| 18 |
+
|
| 19 |
+
- 📸 **智慧食物識別**:使用Google Gemini AI識別照片中的食物
|
| 20 |
+
- 🔢 **卡路里估算**:自動計算食物的大概卡路里含量
|
| 21 |
+
- 📊 **營養資訊**:提供基本的營養價值說明
|
| 22 |
+
- 🚫 **非食物檢測**:能夠識別並提醒非食物圖片
|
| 23 |
+
- 🌐 **繁體中文介面**:完全繁體中文使用者介面
|
| 24 |
+
- 🚀 **簡易部署**:支援HuggingFace Spaces一鍵部署
|
| 25 |
+
|
| 26 |
+
## 🛠️ 技術棧
|
| 27 |
+
|
| 28 |
+
- **前端框架**:Gradio
|
| 29 |
+
- **AI模型**:Google Gemini 1.5 Flash
|
| 30 |
+
- **圖片處理**:Pillow (PIL)
|
| 31 |
+
- **環境管理**:uv + venv
|
| 32 |
+
- **部署平台**:HuggingFace Spaces
|
| 33 |
+
|
| 34 |
+
## 📋 環境需求
|
| 35 |
+
|
| 36 |
+
- Python 3.8+
|
| 37 |
+
- Google AI API Key
|
| 38 |
+
- HuggingFace Token(用於部署)
|
| 39 |
+
|
| 40 |
+
## 🚀 快速開始
|
| 41 |
+
|
| 42 |
+
### 1. 克隆專案
|
| 43 |
+
|
| 44 |
+
```bash
|
| 45 |
+
git clone <your-repo-url>
|
| 46 |
+
cd CalorieApp
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
### 2. 設定Python環境
|
| 50 |
+
|
| 51 |
+
```bash
|
| 52 |
+
# 使用uv建立虛擬環境
|
| 53 |
+
uv venv .venv
|
| 54 |
+
|
| 55 |
+
# 啟動虛擬環境
|
| 56 |
+
source .venv/bin/activate # Linux/Mac
|
| 57 |
+
# 或
|
| 58 |
+
.venv\\Scripts\\activate # Windows
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
### 3. 安裝依賴
|
| 62 |
+
|
| 63 |
+
```bash
|
| 64 |
+
uv pip install -r requirements.txt
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
### 4. 設定環境變數
|
| 68 |
+
|
| 69 |
+
```bash
|
| 70 |
+
# 複製環境變數範例檔案
|
| 71 |
+
cp .env.example .env
|
| 72 |
+
|
| 73 |
+
# 編輯.env檔案,填入你的API金鑰
|
| 74 |
+
nano .env
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
在`.env`檔案中填入:
|
| 78 |
+
```env
|
| 79 |
+
GOOGLE_API_KEY=your_google_api_key_here
|
| 80 |
+
HF_TOKEN=your_huggingface_token_here
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
### 5. 執行應用程式
|
| 84 |
+
|
| 85 |
+
```bash
|
| 86 |
+
python app.py
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
應用程式將在 `http://localhost:7860` 啟動。
|
| 90 |
+
|
| 91 |
+
## 🔑 取得API金鑰
|
| 92 |
+
|
| 93 |
+
### Google AI API Key
|
| 94 |
+
1. 前往 [Google AI Studio](https://makersuite.google.com/app/apikey)
|
| 95 |
+
2. 登入你的Google帳號
|
| 96 |
+
3. 建立新的API金鑰
|
| 97 |
+
4. **本機開發**:將金鑰加入`.env`檔案
|
| 98 |
+
5. **HuggingFace部署**:在Space設定中加入環境變數 `GOOGLE_API_KEY`
|
| 99 |
+
|
| 100 |
+
### HuggingFace Token(僅用於程式化部署)
|
| 101 |
+
1. 前往 [HuggingFace設定](https://huggingface.co/settings/tokens)
|
| 102 |
+
2. 建立新的Access Token
|
| 103 |
+
3. 選擇適當的權限範圍
|
| 104 |
+
|
| 105 |
+
## 🌐 HuggingFace Spaces 部署說明
|
| 106 |
+
|
| 107 |
+
此應用程式已針對HuggingFace Spaces進行優化:
|
| 108 |
+
|
| 109 |
+
### 環境變數設定
|
| 110 |
+
在你的Space設定中加入以下環境變數:
|
| 111 |
+
- `GOOGLE_API_KEY`: 你的Google AI API金鑰
|
| 112 |
+
|
| 113 |
+
### 自動部署
|
| 114 |
+
1. Fork或克隆此repository到你的HuggingFace Space
|
| 115 |
+
2. 設定環境變數
|
| 116 |
+
3. 應用程式將自動建置和部署
|
| 117 |
+
|
| 118 |
+
## 🌐 部署到HuggingFace Spaces
|
| 119 |
+
|
| 120 |
+
### 方法一:直接上傳
|
| 121 |
+
|
| 122 |
+
1. 在HuggingFace建立新的Space
|
| 123 |
+
2. 選擇Gradio作為SDK
|
| 124 |
+
3. 上傳所有專案檔案
|
| 125 |
+
4. 在Space設定中加入環境變數:
|
| 126 |
+
- `GOOGLE_API_KEY`: 你的Google AI API金鑰
|
| 127 |
+
|
| 128 |
+
### 方法二:Git Repository
|
| 129 |
+
|
| 130 |
+
1. 在HuggingFace建立新的Space
|
| 131 |
+
2. 選擇「Git repository」選項
|
| 132 |
+
3. 將此專案推送到HuggingFace的Git repository
|
| 133 |
+
4. 設定環境變數(同上)
|
| 134 |
+
|
| 135 |
+
## 📁 專案結構
|
| 136 |
+
|
| 137 |
+
```
|
| 138 |
+
CalorieApp/
|
| 139 |
+
├── app.py # 主要應用程式
|
| 140 |
+
├── requirements.txt # Python依賴套件
|
| 141 |
+
├── .env.example # 環境變數範例
|
| 142 |
+
├── .gitignore # Git忽略檔案
|
| 143 |
+
├── README.md # 專案說明文件
|
| 144 |
+
└── .venv/ # Python虛擬環境
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
## 🎯 使用方法
|
| 148 |
+
|
| 149 |
+
1. **上傳圖片**:點擊上傳區域選擇食物照片
|
| 150 |
+
2. **自動分析**:上傳後AI會自動開始分析
|
| 151 |
+
3. **查看結果**:右側會顯示詳細的分析結果,包括:
|
| 152 |
+
- 食物名稱
|
| 153 |
+
- 估算份量
|
| 154 |
+
- 預估卡路里
|
| 155 |
+
- 營養說明
|
| 156 |
+
|
| 157 |
+
## ⚠️ 注意事項
|
| 158 |
+
|
| 159 |
+
- 卡路里估算僅供參考,實際數值可能因烹飪方式和份量而有所差異
|
| 160 |
+
- 建議上傳清晰、光線充足的食物照片以獲得更準確的結果
|
| 161 |
+
- AI可能對某些特殊或混合食物的識別有限制
|
| 162 |
+
- 請確保遵守Google AI的使用條款和限制
|
| 163 |
+
|
| 164 |
+
## 🔧 故障排除
|
| 165 |
+
|
| 166 |
+
### 常見問題
|
| 167 |
+
|
| 168 |
+
**Q: 顯示「Google AI模型未正確初始化」錯誤**
|
| 169 |
+
A: 請檢查`.env`檔案中的`GOOGLE_API_KEY`是否正確設定
|
| 170 |
+
|
| 171 |
+
**Q: 上傳圖片後沒有反應**
|
| 172 |
+
A: 請確認圖片格式支援(JPG、PNG等)且檔案大小合理
|
| 173 |
+
|
| 174 |
+
**Q: 分析結果不準確**
|
| 175 |
+
A: AI模型的準確度會因圖片品質、食物種類等因素而變化,建議使用清晰的照片
|
| 176 |
+
|
| 177 |
+
## 🤝 貢獻
|
| 178 |
+
|
| 179 |
+
歡迎提交Issue和Pull Request來改善這個專案!
|
| 180 |
+
|
| 181 |
+
## 📄 授權
|
| 182 |
+
|
| 183 |
+
本專案採用MIT授權條款。
|
| 184 |
+
|
| 185 |
+
## 📞 聯絡資訊
|
| 186 |
+
|
| 187 |
+
如有任何問題或建議,請透過GitHub Issues聯絡我們。
|
app.py
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
+
|
| 8 |
+
# HuggingFace Spaces 環境下不需要 dotenv
|
| 9 |
+
# 環境變數會直接由平台提供
|
| 10 |
+
|
| 11 |
+
# 配置Google AI
|
| 12 |
+
def configure_google_ai():
|
| 13 |
+
"""配置Google AI API"""
|
| 14 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
| 15 |
+
if not api_key:
|
| 16 |
+
raise ValueError("請設定GOOGLE_API_KEY環境變數")
|
| 17 |
+
genai.configure(api_key=api_key)
|
| 18 |
+
return genai.GenerativeModel('gemini-1.5-flash')
|
| 19 |
+
|
| 20 |
+
# 初始化模型
|
| 21 |
+
try:
|
| 22 |
+
model = configure_google_ai()
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"警告:無法初始化Google AI模型 - {e}")
|
| 25 |
+
model = None
|
| 26 |
+
|
| 27 |
+
def analyze_food_calories(image):
|
| 28 |
+
"""
|
| 29 |
+
分析圖片中的食物並估算卡路里
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
image: PIL Image 物件
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
str: 分析結果
|
| 36 |
+
"""
|
| 37 |
+
if model is None:
|
| 38 |
+
return "錯誤:Google AI模型未正確初始化,請檢查API KEY設定"
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
# 準備提示詞
|
| 42 |
+
prompt = """
|
| 43 |
+
請分析這張圖片中的內容:
|
| 44 |
+
|
| 45 |
+
1. 首先判斷圖片中是否包含食物
|
| 46 |
+
2. 如果不是食物,請回答:「這個不是食物」
|
| 47 |
+
3. 如果是食物,請:
|
| 48 |
+
- 識別食物種類
|
| 49 |
+
- 估算大概的份量
|
| 50 |
+
- 計算大概的卡路里含量
|
| 51 |
+
- 以繁體中文回答
|
| 52 |
+
|
| 53 |
+
請以以下格式回答:
|
| 54 |
+
- 食物名稱:[食物名稱]
|
| 55 |
+
- 估算份量:[份量描述]
|
| 56 |
+
- 預估卡路里:[卡路里數值] 大卡
|
| 57 |
+
- 營養說明:[簡短的營養價值說明]
|
| 58 |
+
|
| 59 |
+
如果不是食物,就直接回答「這個不是食物」。
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
# 生成回應
|
| 63 |
+
response = model.generate_content([prompt, image])
|
| 64 |
+
return response.text
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"分析時發生錯誤:{str(e)}"
|
| 68 |
+
|
| 69 |
+
def process_image(image):
|
| 70 |
+
"""
|
| 71 |
+
處理上傳的圖片
|
| 72 |
+
|
| 73 |
+
Args:
|
| 74 |
+
image: Gradio上傳的圖片
|
| 75 |
+
|
| 76 |
+
Returns:
|
| 77 |
+
str: 分析結果
|
| 78 |
+
"""
|
| 79 |
+
if image is None:
|
| 80 |
+
return "請上傳一張圖片"
|
| 81 |
+
|
| 82 |
+
try:
|
| 83 |
+
# 確保圖片是PIL Image格式
|
| 84 |
+
if isinstance(image, str):
|
| 85 |
+
# 如果是路徑字串,載入圖片
|
| 86 |
+
pil_image = Image.open(image)
|
| 87 |
+
else:
|
| 88 |
+
pil_image = image
|
| 89 |
+
|
| 90 |
+
# 分析圖片
|
| 91 |
+
result = analyze_food_calories(pil_image)
|
| 92 |
+
return result
|
| 93 |
+
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return f"處理圖片時發生錯誤:{str(e)}"
|
| 96 |
+
|
| 97 |
+
# 建立Gradio介面
|
| 98 |
+
def create_interface():
|
| 99 |
+
"""建立Gradio使用者介面"""
|
| 100 |
+
|
| 101 |
+
# 自訂CSS樣式
|
| 102 |
+
css = """
|
| 103 |
+
.gradio-container {
|
| 104 |
+
font-family: 'Microsoft JhengHei', sans-serif;
|
| 105 |
+
}
|
| 106 |
+
.title {
|
| 107 |
+
text-align: center;
|
| 108 |
+
color: #2E7D32;
|
| 109 |
+
margin-bottom: 20px;
|
| 110 |
+
}
|
| 111 |
+
.description {
|
| 112 |
+
text-align: center;
|
| 113 |
+
color: #666;
|
| 114 |
+
margin-bottom: 30px;
|
| 115 |
+
}
|
| 116 |
+
"""
|
| 117 |
+
|
| 118 |
+
with gr.Blocks(css=css, title="食物卡路里檢測器") as demo:
|
| 119 |
+
gr.HTML("<h1 class='title'>🍎 食物卡路里檢測器</h1>")
|
| 120 |
+
gr.HTML("<p class='description'>上傳食物照片,AI會幫你識別食物種類並估算卡路里含量</p>")
|
| 121 |
+
|
| 122 |
+
with gr.Row():
|
| 123 |
+
with gr.Column():
|
| 124 |
+
# 圖片上傳元件
|
| 125 |
+
image_input = gr.Image(
|
| 126 |
+
label="上傳食物照片",
|
| 127 |
+
type="pil",
|
| 128 |
+
height=400
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# 分析按鈕
|
| 132 |
+
analyze_btn = gr.Button(
|
| 133 |
+
"🔍 分析卡路里",
|
| 134 |
+
variant="primary",
|
| 135 |
+
size="lg"
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
with gr.Column():
|
| 139 |
+
# 結果顯示
|
| 140 |
+
result_output = gr.Textbox(
|
| 141 |
+
label="分析結果",
|
| 142 |
+
lines=10,
|
| 143 |
+
placeholder="上傳圖片後點擊分析按鈕...",
|
| 144 |
+
interactive=False
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# 使用說明
|
| 148 |
+
gr.HTML("""
|
| 149 |
+
<div style="margin-top: 30px; padding: 20px; background-color: #f5f5f5; border-radius: 10px;">
|
| 150 |
+
<h3>📋 使用說明:</h3>
|
| 151 |
+
<ul>
|
| 152 |
+
<li>📸 上傳一張包含食物的清晰照片</li>
|
| 153 |
+
<li>🤖 AI會自動識別食物種類</li>
|
| 154 |
+
<li>📊 獲得卡路里估算和營養資訊</li>
|
| 155 |
+
<li>⚠️ 如果不是食物,系統會提醒您</li>
|
| 156 |
+
</ul>
|
| 157 |
+
<p><strong>注意:</strong>卡路里估算僅供參考,實際數值可能因烹飪方式和份量而有所差異。</p>
|
| 158 |
+
</div>
|
| 159 |
+
""")
|
| 160 |
+
|
| 161 |
+
# 設定事件處理
|
| 162 |
+
analyze_btn.click(
|
| 163 |
+
fn=process_image,
|
| 164 |
+
inputs=[image_input],
|
| 165 |
+
outputs=[result_output]
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
# 也可以在上傳圖片後自動分析
|
| 169 |
+
image_input.change(
|
| 170 |
+
fn=process_image,
|
| 171 |
+
inputs=[image_input],
|
| 172 |
+
outputs=[result_output]
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
return demo
|
| 176 |
+
|
| 177 |
+
# 主程式
|
| 178 |
+
if __name__ == "__main__":
|
| 179 |
+
# 建立介面
|
| 180 |
+
demo = create_interface()
|
| 181 |
+
|
| 182 |
+
# 啟動應用
|
| 183 |
+
# HuggingFace Spaces 使用預設設定
|
| 184 |
+
demo.launch()
|
packages.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
ffmpeg
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.42.0
|
| 2 |
+
google-generativeai==0.8.5
|
| 3 |
+
pillow==11.3.0
|
requirements_full.txt
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==24.1.0
|
| 2 |
+
annotated-types==0.7.0
|
| 3 |
+
anyio==4.10.0
|
| 4 |
+
audioop-lts==0.2.2
|
| 5 |
+
brotli==1.1.0
|
| 6 |
+
cachetools==5.5.2
|
| 7 |
+
certifi==2025.8.3
|
| 8 |
+
charset-normalizer==3.4.3
|
| 9 |
+
click==8.2.1
|
| 10 |
+
fastapi==0.116.1
|
| 11 |
+
ffmpy==0.6.1
|
| 12 |
+
filelock==3.19.1
|
| 13 |
+
fsspec==2025.7.0
|
| 14 |
+
google-ai-generativelanguage==0.6.15
|
| 15 |
+
google-api-core==2.25.1
|
| 16 |
+
google-api-python-client==2.179.0
|
| 17 |
+
google-auth==2.40.3
|
| 18 |
+
google-auth-httplib2==0.2.0
|
| 19 |
+
google-generativeai==0.8.5
|
| 20 |
+
googleapis-common-protos==1.70.0
|
| 21 |
+
gradio==5.42.0
|
| 22 |
+
gradio-client==1.11.1
|
| 23 |
+
groovy==0.1.2
|
| 24 |
+
grpcio==1.74.0
|
| 25 |
+
grpcio-status==1.71.2
|
| 26 |
+
h11==0.16.0
|
| 27 |
+
hf-xet==1.1.7
|
| 28 |
+
httpcore==1.0.9
|
| 29 |
+
httplib2==0.22.0
|
| 30 |
+
httpx==0.28.1
|
| 31 |
+
huggingface-hub==0.34.4
|
| 32 |
+
idna==3.10
|
| 33 |
+
jinja2==3.1.6
|
| 34 |
+
markdown-it-py==4.0.0
|
| 35 |
+
markupsafe==3.0.2
|
| 36 |
+
mdurl==0.1.2
|
| 37 |
+
numpy==2.3.2
|
| 38 |
+
orjson==3.11.2
|
| 39 |
+
packaging==25.0
|
| 40 |
+
pandas==2.3.1
|
| 41 |
+
pillow==11.3.0
|
| 42 |
+
proto-plus==1.26.1
|
| 43 |
+
protobuf==5.29.5
|
| 44 |
+
pyasn1==0.6.1
|
| 45 |
+
pyasn1-modules==0.4.2
|
| 46 |
+
pydantic==2.11.7
|
| 47 |
+
pydantic-core==2.33.2
|
| 48 |
+
pydub==0.25.1
|
| 49 |
+
pygments==2.19.2
|
| 50 |
+
pyparsing==3.2.3
|
| 51 |
+
python-dateutil==2.9.0.post0
|
| 52 |
+
python-dotenv==1.1.1
|
| 53 |
+
python-multipart==0.0.20
|
| 54 |
+
pytz==2025.2
|
| 55 |
+
pyyaml==6.0.2
|
| 56 |
+
requests==2.32.4
|
| 57 |
+
rich==14.1.0
|
| 58 |
+
rsa==4.9.1
|
| 59 |
+
ruff==0.12.9
|
| 60 |
+
safehttpx==0.1.6
|
| 61 |
+
semantic-version==2.10.0
|
| 62 |
+
shellingham==1.5.4
|
| 63 |
+
six==1.17.0
|
| 64 |
+
sniffio==1.3.1
|
| 65 |
+
starlette==0.47.2
|
| 66 |
+
tomlkit==0.13.3
|
| 67 |
+
tqdm==4.67.1
|
| 68 |
+
typer==0.16.0
|
| 69 |
+
typing-extensions==4.14.1
|
| 70 |
+
typing-inspection==0.4.1
|
| 71 |
+
tzdata==2025.2
|
| 72 |
+
uritemplate==4.2.0
|
| 73 |
+
urllib3==2.5.0
|
| 74 |
+
uvicorn==0.35.0
|
| 75 |
+
websockets==15.0.1
|
run.sh
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
echo "🍎 食物卡路里檢測器 - 啟動中..."
|
| 4 |
+
echo ""
|
| 5 |
+
|
| 6 |
+
# 檢查虛擬環境是否存在
|
| 7 |
+
if [ ! -d ".venv" ]; then
|
| 8 |
+
echo "❌ 找不到虛擬環境,請先執行 uv venv .venv"
|
| 9 |
+
exit 1
|
| 10 |
+
fi
|
| 11 |
+
|
| 12 |
+
# 檢查.env檔案是否存在
|
| 13 |
+
if [ ! -f ".env" ]; then
|
| 14 |
+
echo "❌ 找不到.env檔案,請複製.env.example並設定API金鑰"
|
| 15 |
+
exit 1
|
| 16 |
+
fi
|
| 17 |
+
|
| 18 |
+
# 啟動虛擬環境並執行應用程式
|
| 19 |
+
echo "🚀 啟動Gradio應用程式..."
|
| 20 |
+
source .venv/bin/activate && python app.py
|