Spaces:
Sleeping
Sleeping
YanBoChen
commited on
Commit
·
d603ef9
1
Parent(s):
b3ad1ee
WIP: Remove obsolete files and implement cloud data loading for customization and retrieval systems
Browse files- Deleted large JSON and embedding files from the customization processing and models directories.
- Added `cloud_config.py` and `cloud_loader.py` to handle cloud data loading from HuggingFace.
- Updated `BasicRetrievalSystem` to load data from cloud or local files.
- Created test scripts for cloud loader and core retrieval system to ensure functionality.
- Next_after_pushlargefiles_to_dataset.md +50 -0
- ToDo_huggingface_deployment.md +574 -0
- customization/processing/generate_mapping_json.py +0 -115
- customization/src/cloud_config.py +66 -0
- src/cloud_loader.py +43 -0
- src/retrieval.py +66 -7
- test_stage1_cloud_loader.py +52 -0
- test_stage2_core_retrieval.py +56 -0
Next_after_pushlargefiles_to_dataset.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## Phase 2: Planning & Analysis
|
| 2 |
+
|
| 3 |
+
### Step 2.1 - 執行策略規劃
|
| 4 |
+
|
| 5 |
+
**我建議分階段漸進式執行:**
|
| 6 |
+
|
| 7 |
+
## **🏗️ 漸進式執行計畫**
|
| 8 |
+
|
| 9 |
+
### **階段 1: 建立雲端載入基礎架構**
|
| 10 |
+
|
| 11 |
+
1. **新增** `src/cloud_loader.py`
|
| 12 |
+
2. **測試**: 雲端載入器獨立功能
|
| 13 |
+
3. **確認**: Dataset 連線正常
|
| 14 |
+
|
| 15 |
+
### **階段 2: 修改核心系統**
|
| 16 |
+
|
| 17 |
+
1. **修改** `src/retrieval.py`
|
| 18 |
+
2. **測試**: 核心檢索系統雲端載入
|
| 19 |
+
3. **確認**: General pipeline 正常運作
|
| 20 |
+
|
| 21 |
+
### **階段 3: 修改 Customization 系統**
|
| 22 |
+
|
| 23 |
+
1. **新增** `customization/src/cloud_config.py`
|
| 24 |
+
2. **修改** `customization/customization_pipeline.py`
|
| 25 |
+
3. **測試**: Customization pipeline 雲端載入
|
| 26 |
+
4. **確認**: Hospital-specific 功能正常
|
| 27 |
+
|
| 28 |
+
### **階段 4: 整合測試**
|
| 29 |
+
|
| 30 |
+
1. **整合測試**: 所有功能一起運作
|
| 31 |
+
2. **環境變數測試**: 本地/雲端模式切換
|
| 32 |
+
3. **完整功能驗證**: General + Hospital + Combined 模式
|
| 33 |
+
|
| 34 |
+
### **階段 5: 部署到 Spaces**
|
| 35 |
+
|
| 36 |
+
1. **推送程式碼** (不含大檔案)
|
| 37 |
+
2. **監控部署**
|
| 38 |
+
3. **線上功能驗證**
|
| 39 |
+
|
| 40 |
+
## **💡 每階段的測試方法:**
|
| 41 |
+
|
| 42 |
+
- **獨立測試**: 只測試該階段的功能
|
| 43 |
+
- **漸進整合**: 確保不破壞現有功能
|
| 44 |
+
- **快速回退**: 如果有問題可以立即復原
|
| 45 |
+
|
| 46 |
+
## **🎯 現在開始階段 1?**
|
| 47 |
+
|
| 48 |
+
**先執行 `src/cloud_loader.py` 的創建和獨立測試,確認雲端連線正常後再繼續下一階段?**
|
| 49 |
+
|
| 50 |
+
**你同意這個漸進式方法嗎?**
|
ToDo_huggingface_deployment.md
ADDED
|
@@ -0,0 +1,574 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# OnCall.ai 智慧分離部署方案 B - 詳細實施步驟
|
| 2 |
+
|
| 3 |
+
## 📋 相關檔案列表
|
| 4 |
+
|
| 5 |
+
### 需要修改的檔案:
|
| 6 |
+
|
| 7 |
+
- `src/retrieval.py` - 修改 models/ 路徑邏輯
|
| 8 |
+
- `app.py` - 修改 customization 載入邏輯
|
| 9 |
+
- `customization/src/indexing/storage.py` - 修改資料檔案路徑
|
| 10 |
+
- `customization/src/indexing/annoy_manager.py` - 修改索引檔案路徑
|
| 11 |
+
- `customization/customization_pipeline.py` - 修改處理資料路徑
|
| 12 |
+
|
| 13 |
+
### 新增的檔案:
|
| 14 |
+
|
| 15 |
+
- `src/cloud_loader.py` - 雲端資料載入器
|
| 16 |
+
- `customization/src/cloud_config.py` - customization 雲端配置
|
| 17 |
+
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
## 🏗️ 方案 B 架構總覽
|
| 21 |
+
|
| 22 |
+
### **HuggingFace Spaces (應用+程式碼, <1GB):**
|
| 23 |
+
|
| 24 |
+
```
|
| 25 |
+
oncall-guide-ai/
|
| 26 |
+
├── app.py ✅
|
| 27 |
+
├── src/ ✅
|
| 28 |
+
│ ├── user_prompt.py
|
| 29 |
+
│ ├── retrieval.py (修改)
|
| 30 |
+
│ ├── generation.py
|
| 31 |
+
│ ├── llm_clients.py
|
| 32 |
+
│ ├── medical_conditions.py
|
| 33 |
+
│ └── cloud_loader.py (新增)
|
| 34 |
+
├── customization/ ✅ (只保留程式碼)
|
| 35 |
+
│ ├── src/ (20個 .py 檔案,部分修改)
|
| 36 |
+
│ │ ├── cloud_config.py (新增)
|
| 37 |
+
│ │ ├── indexing/storage.py (修改)
|
| 38 |
+
│ │ └── indexing/annoy_manager.py (修改)
|
| 39 |
+
│ ├── customization_pipeline.py (修改)
|
| 40 |
+
│ ├── generate_embeddings.py
|
| 41 |
+
│ └── test/
|
| 42 |
+
├── requirements.txt ✅
|
| 43 |
+
├── README.md ✅
|
| 44 |
+
└── .gitattributes ✅
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
### **HuggingFace Dataset (純資料, >1.5GB):**
|
| 48 |
+
|
| 49 |
+
```
|
| 50 |
+
oncall-guide-ai-data/
|
| 51 |
+
├── models/ (1.5GB)
|
| 52 |
+
│ ├── embeddings/
|
| 53 |
+
│ │ ├── emergency_embeddings.npy
|
| 54 |
+
│ │ ├── emergency_chunks.json
|
| 55 |
+
│ │ ├── treatment_embeddings.npy
|
| 56 |
+
│ │ └── treatment_chunks.json
|
| 57 |
+
│ ├── indices/annoy/
|
| 58 |
+
│ │ ├── emergency.ann
|
| 59 |
+
│ │ ├── emergency_index.ann
|
| 60 |
+
│ │ ├── treatment.ann
|
| 61 |
+
│ │ └── treatment_index.ann
|
| 62 |
+
│ └── data_validation_report.json
|
| 63 |
+
└── customization_data/
|
| 64 |
+
└── processing/ (約100MB)
|
| 65 |
+
├── indices/
|
| 66 |
+
│ ├── annoy_metadata.json
|
| 67 |
+
│ ├── chunk_embeddings.ann
|
| 68 |
+
│ ├── chunk_mappings.json
|
| 69 |
+
│ ├── tag_embeddings.ann
|
| 70 |
+
│ └── tag_mappings.json
|
| 71 |
+
├── embeddings/
|
| 72 |
+
│ ├── chunk_embeddings.json
|
| 73 |
+
│ ├── document_index.json
|
| 74 |
+
│ ├── document_tag_mapping.json
|
| 75 |
+
│ └── tag_embeddings.json
|
| 76 |
+
└── mapping.json
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
---
|
| 80 |
+
|
| 81 |
+
## Phase 1: 準備 Dataset Repository
|
| 82 |
+
|
| 83 |
+
### Step 1.1: 創建 Dataset Repository
|
| 84 |
+
|
| 85 |
+
```bash
|
| 86 |
+
# 在 HuggingFace 網站創建
|
| 87 |
+
https://huggingface.co/new-dataset
|
| 88 |
+
Dataset name: oncall-guide-ai-data
|
| 89 |
+
Visibility: Public
|
| 90 |
+
License: MIT
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
### Step 1.2: Clone 和設置 Dataset Repository
|
| 94 |
+
|
| 95 |
+
```bash
|
| 96 |
+
# 在工作目錄外創建
|
| 97 |
+
cd ~/Documents
|
| 98 |
+
git clone https://huggingface.co/datasets/ybchen928/oncall-guide-ai-data
|
| 99 |
+
cd oncall-guide-ai-data
|
| 100 |
+
|
| 101 |
+
# 設置 Git LFS
|
| 102 |
+
git lfs install
|
| 103 |
+
git lfs track "*.npy"
|
| 104 |
+
git lfs track "*.ann"
|
| 105 |
+
git lfs track "*.json"
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
### Step 1.3: 複製資料到 Dataset Repository
|
| 109 |
+
|
| 110 |
+
```bash
|
| 111 |
+
# 複製 models 資料夾 (完整)
|
| 112 |
+
cp -r /Users/yanbochen/Documents/Life_in_Canada/CS_study_related/Student_Course_Guide/CS7180_GenAI/GenAI-OnCallAssistant/models ./
|
| 113 |
+
|
| 114 |
+
# 只複製 customization/processing 資料夾
|
| 115 |
+
mkdir -p customization_data
|
| 116 |
+
cp -r /Users/yanbochen/Documents/Life_in_Canada/CS_study_related/Student_Course_Guide/CS7180_GenAI/GenAI-OnCallAssistant/customization/processing ./customization_data/
|
| 117 |
+
|
| 118 |
+
# 提交到 Dataset Repository
|
| 119 |
+
git add .
|
| 120 |
+
git commit -m "Add OnCall.ai model data and customization data"
|
| 121 |
+
git push origin main
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
## Phase 2: 修改主系統檔案
|
| 127 |
+
|
| 128 |
+
### Step 2.1: 新增雲端載入器
|
| 129 |
+
|
| 130 |
+
**新增檔案:`src/cloud_loader.py`**
|
| 131 |
+
|
| 132 |
+
```python
|
| 133 |
+
"""Cloud Data Loader - Downloads model data from HuggingFace Dataset"""
|
| 134 |
+
|
| 135 |
+
import os
|
| 136 |
+
from pathlib import Path
|
| 137 |
+
from huggingface_hub import hf_hub_download
|
| 138 |
+
from typing import Optional
|
| 139 |
+
import logging
|
| 140 |
+
|
| 141 |
+
logger = logging.getLogger(__name__)
|
| 142 |
+
|
| 143 |
+
class CloudDataLoader:
|
| 144 |
+
"""HuggingFace Dataset data loader"""
|
| 145 |
+
|
| 146 |
+
def __init__(self):
|
| 147 |
+
self.dataset_repo = "ybchen928/oncall-guide-ai-models"
|
| 148 |
+
self.use_cloud = os.getenv('USE_CLOUD_DATA', 'true').lower() == 'true'
|
| 149 |
+
|
| 150 |
+
def get_model_file_path(self, filename: str) -> str:
|
| 151 |
+
"""Get model file path for General Pipeline"""
|
| 152 |
+
if self.use_cloud:
|
| 153 |
+
return hf_hub_download(
|
| 154 |
+
repo_id=self.dataset_repo,
|
| 155 |
+
filename=filename,
|
| 156 |
+
repo_type="dataset"
|
| 157 |
+
)
|
| 158 |
+
else:
|
| 159 |
+
# Local development mode
|
| 160 |
+
return str(Path(__file__).parent.parent / filename)
|
| 161 |
+
|
| 162 |
+
def get_customization_file_path(self, filename: str) -> str:
|
| 163 |
+
"""Get customization data file path for Customization Pipeline"""
|
| 164 |
+
if self.use_cloud:
|
| 165 |
+
return hf_hub_download(
|
| 166 |
+
repo_id=self.dataset_repo,
|
| 167 |
+
filename=f"customization_data/{filename}",
|
| 168 |
+
repo_type="dataset"
|
| 169 |
+
)
|
| 170 |
+
else:
|
| 171 |
+
# Local development mode - correct path to processing folder
|
| 172 |
+
return str(Path(__file__).parent.parent / "customization" / "processing" / filename)
|
| 173 |
+
|
| 174 |
+
# Global instance
|
| 175 |
+
cloud_loader = CloudDataLoader()
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
### Step 2.2: 修改 src/retrieval.py
|
| 179 |
+
|
| 180 |
+
**修改區域:第 60-65 行附近的路徑設置**
|
| 181 |
+
|
| 182 |
+
```python
|
| 183 |
+
# 原始程式碼 (修改前):
|
| 184 |
+
# current_file = Path(__file__)
|
| 185 |
+
# project_root = current_file.parent.parent # from src to root
|
| 186 |
+
# base_path = project_root / "models"
|
| 187 |
+
|
| 188 |
+
# 修改後的程式碼:
|
| 189 |
+
from .cloud_loader import cloud_loader
|
| 190 |
+
|
| 191 |
+
def _initialize_system(self) -> None:
|
| 192 |
+
"""Initialize embeddings, indices and chunks"""
|
| 193 |
+
try:
|
| 194 |
+
logger.info("Initializing retrieval system...")
|
| 195 |
+
|
| 196 |
+
# Initialize embedding model
|
| 197 |
+
self.embedding_model = SentenceTransformer("NeuML/pubmedbert-base-embeddings")
|
| 198 |
+
logger.info("Embedding model loaded successfully")
|
| 199 |
+
|
| 200 |
+
# Initialize Annoy indices
|
| 201 |
+
self.emergency_index = AnnoyIndex(self.embedding_dim, 'angular')
|
| 202 |
+
self.treatment_index = AnnoyIndex(self.embedding_dim, 'angular')
|
| 203 |
+
|
| 204 |
+
# Load data using cloud loader
|
| 205 |
+
self._load_chunks_from_cloud()
|
| 206 |
+
self._load_embeddings_from_cloud()
|
| 207 |
+
self._build_or_load_indices_from_cloud()
|
| 208 |
+
|
| 209 |
+
logger.info("Retrieval system initialized successfully")
|
| 210 |
+
|
| 211 |
+
except Exception as e:
|
| 212 |
+
logger.error(f"Failed to initialize retrieval system: {e}")
|
| 213 |
+
raise
|
| 214 |
+
|
| 215 |
+
def _load_chunks_from_cloud(self) -> None:
|
| 216 |
+
"""Load chunk data from cloud or local files"""
|
| 217 |
+
try:
|
| 218 |
+
# Load emergency chunks
|
| 219 |
+
emergency_chunks_path = cloud_loader.get_model_file_path("models/embeddings/emergency_chunks.json")
|
| 220 |
+
with open(emergency_chunks_path, 'r', encoding='utf-8') as f:
|
| 221 |
+
emergency_data = json.load(f)
|
| 222 |
+
self.emergency_chunks = {i: chunk for i, chunk in enumerate(emergency_data)}
|
| 223 |
+
|
| 224 |
+
# Load treatment chunks
|
| 225 |
+
treatment_chunks_path = cloud_loader.get_model_file_path("models/embeddings/treatment_chunks.json")
|
| 226 |
+
with open(treatment_chunks_path, 'r', encoding='utf-8') as f:
|
| 227 |
+
treatment_data = json.load(f)
|
| 228 |
+
self.treatment_chunks = {i: chunk for i, chunk in enumerate(treatment_data)}
|
| 229 |
+
|
| 230 |
+
logger.info(f"Loaded {len(self.emergency_chunks)} emergency and {len(self.treatment_chunks)} treatment chunks")
|
| 231 |
+
|
| 232 |
+
except Exception as e:
|
| 233 |
+
logger.error(f"Failed to load chunks: {e}")
|
| 234 |
+
raise
|
| 235 |
+
|
| 236 |
+
def _load_embeddings_from_cloud(self) -> None:
|
| 237 |
+
"""Load embeddings from cloud or local files"""
|
| 238 |
+
try:
|
| 239 |
+
# Load emergency embeddings
|
| 240 |
+
emergency_embeddings_path = cloud_loader.get_model_file_path("models/embeddings/emergency_embeddings.npy")
|
| 241 |
+
self.emergency_embeddings = np.load(emergency_embeddings_path)
|
| 242 |
+
|
| 243 |
+
# Load treatment embeddings
|
| 244 |
+
treatment_embeddings_path = cloud_loader.get_model_file_path("models/embeddings/treatment_embeddings.npy")
|
| 245 |
+
self.treatment_embeddings = np.load(treatment_embeddings_path)
|
| 246 |
+
|
| 247 |
+
logger.info("Embeddings loaded successfully")
|
| 248 |
+
|
| 249 |
+
except Exception as e:
|
| 250 |
+
logger.error(f"Failed to load embeddings: {e}")
|
| 251 |
+
raise
|
| 252 |
+
|
| 253 |
+
def _build_or_load_indices_from_cloud(self) -> None:
|
| 254 |
+
"""Build or load Annoy indices from cloud or local files"""
|
| 255 |
+
try:
|
| 256 |
+
# Load emergency index
|
| 257 |
+
emergency_index_path = cloud_loader.get_model_file_path("models/indices/annoy/emergency.ann")
|
| 258 |
+
self.emergency_index.load(emergency_index_path)
|
| 259 |
+
|
| 260 |
+
# Load treatment index
|
| 261 |
+
treatment_index_path = cloud_loader.get_model_file_path("models/indices/annoy/treatment.ann")
|
| 262 |
+
self.treatment_index.load(treatment_index_path)
|
| 263 |
+
|
| 264 |
+
logger.info("Annoy indices loaded successfully")
|
| 265 |
+
|
| 266 |
+
except Exception as e:
|
| 267 |
+
logger.error(f"Failed to load indices: {e}")
|
| 268 |
+
raise
|
| 269 |
+
```
|
| 270 |
+
|
| 271 |
+
---
|
| 272 |
+
|
| 273 |
+
## Phase 3: 修改 Customization 系統檔案
|
| 274 |
+
|
| 275 |
+
### Step 3.1: 新增 customization 雲端配置
|
| 276 |
+
|
| 277 |
+
**新增檔案:`customization/src/cloud_config.py`**
|
| 278 |
+
|
| 279 |
+
```python
|
| 280 |
+
"""Customization 系統雲端配置"""
|
| 281 |
+
|
| 282 |
+
import os
|
| 283 |
+
from pathlib import Path
|
| 284 |
+
from huggingface_hub import hf_hub_download
|
| 285 |
+
import logging
|
| 286 |
+
|
| 287 |
+
logger = logging.getLogger(__name__)
|
| 288 |
+
|
| 289 |
+
class CustomizationCloudLoader:
|
| 290 |
+
"""Customization 專用雲端載入器"""
|
| 291 |
+
|
| 292 |
+
def __init__(self):
|
| 293 |
+
self.dataset_repo = "ybchen928/oncall-guide-ai-data"
|
| 294 |
+
self.use_cloud = os.getenv('USE_CLOUD_DATA', 'true').lower() == 'true'
|
| 295 |
+
|
| 296 |
+
def get_processing_file_path(self, relative_path: str) -> str:
|
| 297 |
+
"""獲取 processing 檔案路徑"""
|
| 298 |
+
if self.use_cloud:
|
| 299 |
+
return hf_hub_download(
|
| 300 |
+
repo_id=self.dataset_repo,
|
| 301 |
+
filename=f"customization_data/processing/{relative_path}",
|
| 302 |
+
repo_type="dataset"
|
| 303 |
+
)
|
| 304 |
+
else:
|
| 305 |
+
# 本地開發模式
|
| 306 |
+
base_path = Path(__file__).parent.parent.parent / "customization" / "processing"
|
| 307 |
+
return str(base_path / relative_path)
|
| 308 |
+
|
| 309 |
+
# 全域實例
|
| 310 |
+
customization_loader = CustomizationCloudLoader()
|
| 311 |
+
```
|
| 312 |
+
|
| 313 |
+
### Step 3.2: 修改 customization/src/indexing/storage.py
|
| 314 |
+
|
| 315 |
+
**修改區域:檔案路徑設置部分**
|
| 316 |
+
|
| 317 |
+
```python
|
| 318 |
+
# 在檔案頂部添加導入
|
| 319 |
+
from ..cloud_config import customization_loader
|
| 320 |
+
|
| 321 |
+
# 修改路徑相關函數 (大約第 45 行附近)
|
| 322 |
+
def get_processing_path(filename: str) -> str:
|
| 323 |
+
"""獲取處理檔案的路徑"""
|
| 324 |
+
return customization_loader.get_processing_file_path(filename)
|
| 325 |
+
|
| 326 |
+
# 修改所有使用處理檔案的地方
|
| 327 |
+
def load_chunk_mappings():
|
| 328 |
+
"""載入 chunk mappings"""
|
| 329 |
+
mappings_path = get_processing_path("indices/chunk_mappings.json")
|
| 330 |
+
with open(mappings_path, 'r', encoding='utf-8') as f:
|
| 331 |
+
return json.load(f)
|
| 332 |
+
|
| 333 |
+
def load_tag_mappings():
|
| 334 |
+
"""載入 tag mappings"""
|
| 335 |
+
mappings_path = get_processing_path("indices/tag_mappings.json")
|
| 336 |
+
with open(mappings_path, 'r', encoding='utf-8') as f:
|
| 337 |
+
return json.load(f)
|
| 338 |
+
```
|
| 339 |
+
|
| 340 |
+
### Step 3.3: 修改 customization/src/indexing/annoy_manager.py
|
| 341 |
+
|
| 342 |
+
**修改區域:索引檔案載入部分**
|
| 343 |
+
|
| 344 |
+
```python
|
| 345 |
+
# 在檔案頂部添加導入
|
| 346 |
+
from ..cloud_config import customization_loader
|
| 347 |
+
|
| 348 |
+
# 修改索引載入函數 (大約第 134 行附近)
|
| 349 |
+
class AnnoyIndexManager:
|
| 350 |
+
def load_chunk_index(self):
|
| 351 |
+
"""載入 chunk 索引"""
|
| 352 |
+
index_path = customization_loader.get_processing_file_path("indices/chunk_embeddings.ann")
|
| 353 |
+
self.chunk_index = AnnoyIndex(self.embedding_dim, 'angular')
|
| 354 |
+
self.chunk_index.load(index_path)
|
| 355 |
+
return self.chunk_index
|
| 356 |
+
|
| 357 |
+
def load_tag_index(self):
|
| 358 |
+
"""載入 tag 索引"""
|
| 359 |
+
index_path = customization_loader.get_processing_file_path("indices/tag_embeddings.ann")
|
| 360 |
+
self.tag_index = AnnoyIndex(self.embedding_dim, 'angular')
|
| 361 |
+
self.tag_index.load(index_path)
|
| 362 |
+
return self.tag_index
|
| 363 |
+
```
|
| 364 |
+
|
| 365 |
+
### Step 3.4: 修改 customization/customization_pipeline.py
|
| 366 |
+
|
| 367 |
+
**修改區域:主要處理流程的檔案路徑**
|
| 368 |
+
|
| 369 |
+
```python
|
| 370 |
+
# 在檔案頂部添加 (大約第 12 行之後)
|
| 371 |
+
from src.cloud_config import customization_loader
|
| 372 |
+
|
| 373 |
+
# 修改資料載入函數 (大約第 31 行附近)
|
| 374 |
+
def load_processing_data():
|
| 375 |
+
"""載入處理所需的資料檔案"""
|
| 376 |
+
try:
|
| 377 |
+
# 載入文檔索引
|
| 378 |
+
doc_index_path = customization_loader.get_processing_file_path("embeddings/document_index.json")
|
| 379 |
+
with open(doc_index_path, 'r', encoding='utf-8') as f:
|
| 380 |
+
document_index = json.load(f)
|
| 381 |
+
|
| 382 |
+
# 載入對應關係
|
| 383 |
+
mapping_path = customization_loader.get_processing_file_path("mapping.json")
|
| 384 |
+
with open(mapping_path, 'r', encoding='utf-8') as f:
|
| 385 |
+
mapping_data = json.load(f)
|
| 386 |
+
|
| 387 |
+
return document_index, mapping_data
|
| 388 |
+
|
| 389 |
+
except Exception as e:
|
| 390 |
+
logger.error(f"Failed to load processing data: {e}")
|
| 391 |
+
raise
|
| 392 |
+
```
|
| 393 |
+
|
| 394 |
+
---
|
| 395 |
+
|
| 396 |
+
## Phase 4: 更新配置檔案
|
| 397 |
+
|
| 398 |
+
### Step 4.1: 更新 requirements.txt
|
| 399 |
+
|
| 400 |
+
**新增必要依賴:**
|
| 401 |
+
|
| 402 |
+
```text
|
| 403 |
+
# 在現有 requirements.txt 中確保包含:
|
| 404 |
+
huggingface-hub>=0.33,<0.35
|
| 405 |
+
```
|
| 406 |
+
|
| 407 |
+
### Step 4.2: 更新 .gitattributes
|
| 408 |
+
|
| 409 |
+
**確保 Git LFS 設置:**
|
| 410 |
+
|
| 411 |
+
```text
|
| 412 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 413 |
+
*.ann filter=lfs diff=lfs merge=lfs -text
|
| 414 |
+
*.json filter=lfs diff=lfs merge=lfs -text
|
| 415 |
+
```
|
| 416 |
+
|
| 417 |
+
---
|
| 418 |
+
|
| 419 |
+
## Phase 5: 本地測試
|
| 420 |
+
|
| 421 |
+
### Step 5.1: 測試雲端資料載入
|
| 422 |
+
|
| 423 |
+
**創建測試腳本:`test_cloud_integration.py`**
|
| 424 |
+
|
| 425 |
+
```python
|
| 426 |
+
import os
|
| 427 |
+
os.environ['USE_CLOUD_DATA'] = 'true'
|
| 428 |
+
|
| 429 |
+
from src.retrieval import BasicRetrievalSystem
|
| 430 |
+
from customization.customization_pipeline import retrieve_document_chunks
|
| 431 |
+
|
| 432 |
+
def test_integration():
|
| 433 |
+
print("🧪 測試雲端整合...")
|
| 434 |
+
|
| 435 |
+
try:
|
| 436 |
+
# 測試核心系統
|
| 437 |
+
retrieval = BasicRetrievalSystem()
|
| 438 |
+
print("✅ 核心系統初始化成功")
|
| 439 |
+
|
| 440 |
+
# 測試 customization 系統
|
| 441 |
+
results = retrieve_document_chunks("chest pain", top_k=3)
|
| 442 |
+
print(f"✅ Customization 系統測試成功,返回 {len(results)} 個結果")
|
| 443 |
+
|
| 444 |
+
print("🎉 所有整合測試通過!")
|
| 445 |
+
return True
|
| 446 |
+
|
| 447 |
+
except Exception as e:
|
| 448 |
+
print(f"❌ 測試失敗: {e}")
|
| 449 |
+
return False
|
| 450 |
+
|
| 451 |
+
if __name__ == "__main__":
|
| 452 |
+
success = test_integration()
|
| 453 |
+
exit(0 if success else 1)
|
| 454 |
+
```
|
| 455 |
+
|
| 456 |
+
### Step 5.2: 執行測試
|
| 457 |
+
|
| 458 |
+
```bash
|
| 459 |
+
python test_cloud_integration.py
|
| 460 |
+
```
|
| 461 |
+
|
| 462 |
+
---
|
| 463 |
+
|
| 464 |
+
## Phase 6: 部署到 Spaces
|
| 465 |
+
|
| 466 |
+
### Step 6.1: 準備 Spaces 檔案
|
| 467 |
+
|
| 468 |
+
**確認要上傳到 Spaces 的檔案清單:**
|
| 469 |
+
|
| 470 |
+
- ✅ `app.py` (已修改)
|
| 471 |
+
- ✅ `src/` (包含新的 cloud_loader.py)
|
| 472 |
+
- ✅ `customization/` (只包含程式碼,不含 processing/)
|
| 473 |
+
- ✅ `requirements.txt` (已更新)
|
| 474 |
+
- ✅ `README.md`
|
| 475 |
+
- ✅ `.gitattributes`
|
| 476 |
+
|
| 477 |
+
### Step 6.2: Git 提交和推送
|
| 478 |
+
|
| 479 |
+
```bash
|
| 480 |
+
# 在主專案目錄
|
| 481 |
+
cd /Users/yanbochen/Documents/Life_in_Canada/CS_study_related/Student_Course_Guide/CS7180_GenAI/GenAI-OnCallAssistant
|
| 482 |
+
|
| 483 |
+
# 移除 customization/processing 資料夾 (暫時)
|
| 484 |
+
mv customization/processing customization_processing_backup
|
| 485 |
+
|
| 486 |
+
# 添加修改的檔案
|
| 487 |
+
git add src/cloud_loader.py
|
| 488 |
+
git add customization/src/cloud_config.py
|
| 489 |
+
git add src/retrieval.py
|
| 490 |
+
git add customization/src/indexing/storage.py
|
| 491 |
+
git add customization/src/indexing/annoy_manager.py
|
| 492 |
+
git add customization/customization_pipeline.py
|
| 493 |
+
git add requirements.txt
|
| 494 |
+
git add .gitattributes
|
| 495 |
+
|
| 496 |
+
# 提交變更
|
| 497 |
+
git commit -m "Implement cloud data loading for HuggingFace Spaces deployment"
|
| 498 |
+
|
| 499 |
+
# 推送到 Spaces (不包含大檔案)
|
| 500 |
+
git push hf HuggingFace-Deployment:main --force
|
| 501 |
+
|
| 502 |
+
# 恢復 processing 資料夾 (本地開發用)
|
| 503 |
+
mv customization_processing_backup customization/processing
|
| 504 |
+
```
|
| 505 |
+
|
| 506 |
+
---
|
| 507 |
+
|
| 508 |
+
## Phase 7: 驗證部署
|
| 509 |
+
|
| 510 |
+
### Step 7.1: 檢查 Spaces 建置
|
| 511 |
+
|
| 512 |
+
1. 前往:https://huggingface.co/spaces/ybchen928/oncall-guide-ai
|
| 513 |
+
2. 檢查 **"App"** 標籤的建置狀態
|
| 514 |
+
3. 查看 **"Logs"** 確認雲端資料載入成功
|
| 515 |
+
|
| 516 |
+
### Step 7.2: 測試功能
|
| 517 |
+
|
| 518 |
+
1. **General Mode**: 測試基本醫療查詢
|
| 519 |
+
2. **Hospital Mode**: 測試 customization 功能
|
| 520 |
+
3. **Combined Mode**: 測試混合功能
|
| 521 |
+
|
| 522 |
+
---
|
| 523 |
+
|
| 524 |
+
## 🚨 故障排除
|
| 525 |
+
|
| 526 |
+
### 常見問題與解決方案:
|
| 527 |
+
|
| 528 |
+
**1. 模型下載失敗**
|
| 529 |
+
|
| 530 |
+
- 檢查 Dataset Repository 是否為 Public
|
| 531 |
+
- 確認網路連接正常
|
| 532 |
+
|
| 533 |
+
**2. 路徑錯誤**
|
| 534 |
+
|
| 535 |
+
- 檢查 cloud_loader.py 中的檔案路徑
|
| 536 |
+
- 確認 Dataset 中的檔案結構正確
|
| 537 |
+
|
| 538 |
+
**3. Import 錯誤**
|
| 539 |
+
|
| 540 |
+
- 檢查所有新增的 import 語句
|
| 541 |
+
- 確認 requirements.txt 包含 huggingface-hub
|
| 542 |
+
|
| 543 |
+
**4. Customization 功能失效**
|
| 544 |
+
|
| 545 |
+
- 檢查 customization/src/cloud_config.py 是否正確載入
|
| 546 |
+
- 確認 processing 資料在 Dataset 中的路徑
|
| 547 |
+
|
| 548 |
+
---
|
| 549 |
+
|
| 550 |
+
## 📊 檔案大小估算
|
| 551 |
+
|
| 552 |
+
**Spaces Repository (~50MB):**
|
| 553 |
+
|
| 554 |
+
- app.py + src/ + customization/src/ + configs
|
| 555 |
+
- 完全在 1GB 限制內
|
| 556 |
+
|
| 557 |
+
**Dataset Repository (~1.6GB):**
|
| 558 |
+
|
| 559 |
+
- models/ (~1.5GB)
|
| 560 |
+
- customization_data/processing/ (~100MB)
|
| 561 |
+
- 在 300GB 免費額度內
|
| 562 |
+
|
| 563 |
+
---
|
| 564 |
+
|
| 565 |
+
## 🎯 完成檢查清單
|
| 566 |
+
|
| 567 |
+
- [ ] Dataset Repository 創建並上傳資料
|
| 568 |
+
- [ ] 新增 src/cloud_loader.py
|
| 569 |
+
- [ ] 修改 src/retrieval.py 路徑邏輯
|
| 570 |
+
- [ ] 新增 customization/src/cloud_config.py
|
| 571 |
+
- [ ] 修改 customization 相關檔案路徑
|
| 572 |
+
- [ ] 本地測試雲端整合功能
|
| 573 |
+
- [ ] 部署到 Spaces (不含大檔案)
|
| 574 |
+
- [ ] 驗證完整功能正常運作
|
customization/processing/generate_mapping_json.py
DELETED
|
@@ -1,115 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Generate mapping.json from combined_er_symptoms_diagnoses.csv
|
| 4 |
-
This script creates the mapping file needed for the customization pipeline.
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
import csv
|
| 8 |
-
import json
|
| 9 |
-
import os
|
| 10 |
-
from pathlib import Path
|
| 11 |
-
|
| 12 |
-
def csv_to_mapping_json():
|
| 13 |
-
"""Convert CSV to mapping.json format"""
|
| 14 |
-
|
| 15 |
-
# Define paths
|
| 16 |
-
processing_dir = Path(__file__).parent
|
| 17 |
-
customization_dir = processing_dir.parent
|
| 18 |
-
csv_path = customization_dir / "docs" / "combined_er_symptoms_diagnoses.csv"
|
| 19 |
-
output_path = processing_dir / "mapping.json"
|
| 20 |
-
|
| 21 |
-
# Read CSV and convert to mapping format
|
| 22 |
-
mappings = []
|
| 23 |
-
|
| 24 |
-
with open(csv_path, 'r', encoding='utf-8-sig') as csvfile: # Handle BOM
|
| 25 |
-
reader = csv.DictReader(csvfile)
|
| 26 |
-
|
| 27 |
-
for row in reader:
|
| 28 |
-
# Skip empty rows
|
| 29 |
-
if not row.get('PDF Abbreviation'):
|
| 30 |
-
continue
|
| 31 |
-
|
| 32 |
-
# Extract symptoms and diagnoses
|
| 33 |
-
symptoms_raw = row['ER Symptom (Surface)'].strip()
|
| 34 |
-
diagnoses_raw = row['Underlying Diagnosis (Core)'].strip()
|
| 35 |
-
|
| 36 |
-
# Split symptoms by comma and clean
|
| 37 |
-
symptoms = [s.strip() for s in symptoms_raw.split(',') if s.strip()]
|
| 38 |
-
|
| 39 |
-
# Split diagnoses by comma and clean
|
| 40 |
-
diagnoses = [d.strip() for d in diagnoses_raw.split(',') if d.strip()]
|
| 41 |
-
|
| 42 |
-
# Create PDF filename based on abbreviation
|
| 43 |
-
pdf_name = get_pdf_filename(row['PDF Abbreviation'])
|
| 44 |
-
|
| 45 |
-
# Create mapping entry
|
| 46 |
-
mapping = {
|
| 47 |
-
"pdf": pdf_name,
|
| 48 |
-
"symptoms": symptoms,
|
| 49 |
-
"diagnoses": diagnoses
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
-
mappings.append(mapping)
|
| 53 |
-
|
| 54 |
-
# Write to JSON file
|
| 55 |
-
with open(output_path, 'w', encoding='utf-8') as jsonfile:
|
| 56 |
-
json.dump(mappings, jsonfile, indent=2, ensure_ascii=False)
|
| 57 |
-
|
| 58 |
-
print(f"✅ Generated mapping.json with {len(mappings)} entries")
|
| 59 |
-
print(f"📄 Output saved to: {output_path}")
|
| 60 |
-
|
| 61 |
-
# Verify all PDFs exist
|
| 62 |
-
docs_dir = customization_dir / "docs"
|
| 63 |
-
missing_pdfs = []
|
| 64 |
-
|
| 65 |
-
for mapping in mappings:
|
| 66 |
-
pdf_path = docs_dir / mapping['pdf']
|
| 67 |
-
if not pdf_path.exists():
|
| 68 |
-
missing_pdfs.append(mapping['pdf'])
|
| 69 |
-
|
| 70 |
-
if missing_pdfs:
|
| 71 |
-
print(f"\n⚠️ Warning: {len(missing_pdfs)} PDF files not found:")
|
| 72 |
-
for pdf in missing_pdfs[:5]: # Show first 5
|
| 73 |
-
print(f" - {pdf}")
|
| 74 |
-
if len(missing_pdfs) > 5:
|
| 75 |
-
print(f" ... and {len(missing_pdfs) - 5} more")
|
| 76 |
-
else:
|
| 77 |
-
print("\n✅ All PDF files found in docs directory")
|
| 78 |
-
|
| 79 |
-
return mappings
|
| 80 |
-
|
| 81 |
-
def get_pdf_filename(abbreviation):
|
| 82 |
-
"""Convert abbreviation to actual PDF filename based on files in docs directory"""
|
| 83 |
-
|
| 84 |
-
# Mapping of abbreviations to actual PDF filenames
|
| 85 |
-
pdf_mapping = {
|
| 86 |
-
"SpinalCordEmergencies": "Recognizing Spinal Cord Emergencies.pdf",
|
| 87 |
-
"DizzinessApproach": "*Dizziness - A Diagnostic Approach.pdf",
|
| 88 |
-
"CodeHeadache": "*Code Headache - Development of a protocol for optimizing headache management in the emergency room.pdf",
|
| 89 |
-
"EarlyAFTherapy": "Early Rhythm-Control Therapy in Patients with Atrial Fibrillation.pdf",
|
| 90 |
-
"2024ESC_AF_Guidelines": "2024 ESC Guidelines for the management of atrial fibrillation developed in collaboration with the European Association for Cardio-Thoracic Surgery.pdf",
|
| 91 |
-
"PregnancyBleeding_ED": "What assessment, intervention and diagnostics should women with early pregnancy bleeding receive in the emergency department and when A scoping review and synthesis of evidence.pdf",
|
| 92 |
-
"UGIB_Guideline": "acg_clinical_guideline__upper_gastrointestinal_and.14.pdf",
|
| 93 |
-
"PulmonaryEmbolism": "Acute Pulmonary Embolism A Review.pdf",
|
| 94 |
-
"CAP_Review": "Community-Acquired Pneumonia.pdf",
|
| 95 |
-
"AcuteIschemicStroke_Guideline": "Guidelines for the Early Management of Patients With Acute Ischemic Stroke.pdf",
|
| 96 |
-
"ChestPain_Guideline_2021": "2021 Guideline for the Evaluation and Diagnosis of Chest Pain.pdf",
|
| 97 |
-
"FUO_Neutropenia_2024": "2024 update of the AGIHO guideline on diagnosis and empirical treatment of fever of unknown origin (FUO) in adult neutropenic patients with solid tumours and hematological malignancies.pdf",
|
| 98 |
-
"Eclampsia_ER_Management": "*Management of eclampsia in the accident and emergency department.pdf",
|
| 99 |
-
"UTI_Mazzulli": "Diagnosis and Management of simple and complicated urinary tract infections (UTIs).pdf",
|
| 100 |
-
"Pediatric_Seizures_2016": "J Paediatrics Child Health - 2016 - Lawton - Seizures in the paediatric emergency department.pdf",
|
| 101 |
-
"PregnancyLoss_Review": "A REVIEW OF THE MANAGEMENT OF LOSS OF PREGNANCY IN THE EMERGENCY DEPARTMENT.pdf",
|
| 102 |
-
"FUO_Children": "Update on Fever of Unknown Origin in Children Focus on Etiologies and Clinical Apporach.pdf",
|
| 103 |
-
# New entries based on actual files in docs directory
|
| 104 |
-
"MyastheniaGravis": "[Transition of Japanese clinical guidelines for myasthenia gravis].pdf",
|
| 105 |
-
"AcutePorphyrias": "AGA Clinical Practice Update on Diagnosis and Management of Acute Hepatic Porphyrias- Expert Review.pdf",
|
| 106 |
-
"Botulism": "Clinical Guidelines for Diagnosis and Treatment of Botulism, 2021.pdf",
|
| 107 |
-
"WilsonsDisease": "EASL-ERN Clinical Practice Guidelines on Wilsons disease.pdf",
|
| 108 |
-
"HereditaryAngioedema": "The international WAO:EAACI guideline for the management of hereditary angioedema-The 2021 revision and update.pdf",
|
| 109 |
-
}
|
| 110 |
-
|
| 111 |
-
# Return mapped filename or create a generic one based on abbreviation
|
| 112 |
-
return pdf_mapping.get(abbreviation, f"{abbreviation}.pdf")
|
| 113 |
-
|
| 114 |
-
if __name__ == "__main__":
|
| 115 |
-
csv_to_mapping_json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
class CustomizationCloudLoader:
|
| 11 |
+
"""Customization-specific cloud data loader"""
|
| 12 |
+
|
| 13 |
+
def __init__(self):
|
| 14 |
+
self.dataset_repo = "ybchen928/oncall-guide-ai-models"
|
| 15 |
+
self.use_cloud = os.getenv('USE_CLOUD_DATA', 'true').lower() == 'true'
|
| 16 |
+
|
| 17 |
+
def get_processing_file_path(self, relative_path: str) -> str:
|
| 18 |
+
"""Get processing file path for Customization Pipeline"""
|
| 19 |
+
if self.use_cloud:
|
| 20 |
+
return hf_hub_download(
|
| 21 |
+
repo_id=self.dataset_repo,
|
| 22 |
+
filename=f"customization_data/processing/{relative_path}",
|
| 23 |
+
repo_type="dataset"
|
| 24 |
+
)
|
| 25 |
+
else:
|
| 26 |
+
# Local development mode - correct path to processing folder
|
| 27 |
+
base_path = Path(__file__).parent.parent.parent / "customization" / "processing"
|
| 28 |
+
return str(base_path / relative_path)
|
| 29 |
+
|
| 30 |
+
def preload_all_processing_files(self) -> tuple:
|
| 31 |
+
"""Preload all processing files and return directory paths"""
|
| 32 |
+
if self.use_cloud:
|
| 33 |
+
# Download all required files
|
| 34 |
+
files_to_download = [
|
| 35 |
+
"embeddings/document_index.json",
|
| 36 |
+
"embeddings/tag_embeddings.json",
|
| 37 |
+
"embeddings/document_tag_mapping.json",
|
| 38 |
+
"embeddings/chunk_embeddings.json",
|
| 39 |
+
"indices/chunk_mappings.json",
|
| 40 |
+
"indices/tag_mappings.json",
|
| 41 |
+
"indices/annoy_metadata.json",
|
| 42 |
+
"indices/chunk_embeddings.ann",
|
| 43 |
+
"indices/tag_embeddings.ann",
|
| 44 |
+
"mapping.json"
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
# Download each file to ensure they're all cached
|
| 48 |
+
for file_path in files_to_download:
|
| 49 |
+
try:
|
| 50 |
+
self.get_processing_file_path(file_path)
|
| 51 |
+
logger.info(f"Downloaded: {file_path}")
|
| 52 |
+
except Exception as e:
|
| 53 |
+
logger.warning(f"Failed to download {file_path}: {e}")
|
| 54 |
+
|
| 55 |
+
# Get directory paths from downloaded files
|
| 56 |
+
embeddings_dir = Path(self.get_processing_file_path("embeddings/document_index.json")).parent
|
| 57 |
+
indices_dir = Path(self.get_processing_file_path("indices/chunk_mappings.json")).parent
|
| 58 |
+
|
| 59 |
+
return str(embeddings_dir), str(indices_dir)
|
| 60 |
+
else:
|
| 61 |
+
# Local development mode
|
| 62 |
+
base_path = Path(__file__).parent.parent.parent / "customization" / "processing"
|
| 63 |
+
return str(base_path / "embeddings"), str(base_path / "indices")
|
| 64 |
+
|
| 65 |
+
# Global instance
|
| 66 |
+
customization_loader = CustomizationCloudLoader()
|
src/cloud_loader.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Cloud Data Loader - Downloads model data from HuggingFace Dataset"""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
from typing import Optional
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
class CloudDataLoader:
|
| 12 |
+
"""HuggingFace Dataset data loader"""
|
| 13 |
+
|
| 14 |
+
def __init__(self):
|
| 15 |
+
self.dataset_repo = "ybchen928/oncall-guide-ai-models"
|
| 16 |
+
self.use_cloud = os.getenv('USE_CLOUD_DATA', 'true').lower() == 'true'
|
| 17 |
+
|
| 18 |
+
def get_model_file_path(self, filename: str) -> str:
|
| 19 |
+
"""Get model file path for General Pipeline"""
|
| 20 |
+
if self.use_cloud:
|
| 21 |
+
return hf_hub_download(
|
| 22 |
+
repo_id=self.dataset_repo,
|
| 23 |
+
filename=filename,
|
| 24 |
+
repo_type="dataset"
|
| 25 |
+
)
|
| 26 |
+
else:
|
| 27 |
+
# Local development mode
|
| 28 |
+
return str(Path(__file__).parent.parent / filename)
|
| 29 |
+
|
| 30 |
+
def get_customization_file_path(self, filename: str) -> str:
|
| 31 |
+
"""Get customization data file path for Customization Pipeline"""
|
| 32 |
+
if self.use_cloud:
|
| 33 |
+
return hf_hub_download(
|
| 34 |
+
repo_id=self.dataset_repo,
|
| 35 |
+
filename=f"customization_data/{filename}",
|
| 36 |
+
repo_type="dataset"
|
| 37 |
+
)
|
| 38 |
+
else:
|
| 39 |
+
# Local development mode - correct path to processing folder
|
| 40 |
+
return str(Path(__file__).parent.parent / "customization" / "processing" / filename)
|
| 41 |
+
|
| 42 |
+
# Global instance
|
| 43 |
+
cloud_loader = CloudDataLoader()
|
src/retrieval.py
CHANGED
|
@@ -55,13 +55,11 @@ class BasicRetrievalSystem:
|
|
| 55 |
self.emergency_index = AnnoyIndex(self.embedding_dim, 'angular')
|
| 56 |
self.treatment_index = AnnoyIndex(self.embedding_dim, 'angular')
|
| 57 |
|
| 58 |
-
# Load data
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
self.
|
| 63 |
-
self._load_embeddings(base_path)
|
| 64 |
-
self._build_or_load_indices(base_path)
|
| 65 |
|
| 66 |
logger.info("Retrieval system initialized successfully")
|
| 67 |
|
|
@@ -69,6 +67,67 @@ class BasicRetrievalSystem:
|
|
| 69 |
logger.error(f"Failed to initialize retrieval system: {e}")
|
| 70 |
raise
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
def _load_chunks(self, base_path: Path) -> None:
|
| 73 |
"""Load chunk data from JSON files"""
|
| 74 |
try:
|
|
|
|
| 55 |
self.emergency_index = AnnoyIndex(self.embedding_dim, 'angular')
|
| 56 |
self.treatment_index = AnnoyIndex(self.embedding_dim, 'angular')
|
| 57 |
|
| 58 |
+
# Load data using cloud loader
|
| 59 |
+
from cloud_loader import cloud_loader
|
| 60 |
+
self._load_chunks_from_cloud()
|
| 61 |
+
self._load_embeddings_from_cloud()
|
| 62 |
+
self._build_or_load_indices_from_cloud()
|
|
|
|
|
|
|
| 63 |
|
| 64 |
logger.info("Retrieval system initialized successfully")
|
| 65 |
|
|
|
|
| 67 |
logger.error(f"Failed to initialize retrieval system: {e}")
|
| 68 |
raise
|
| 69 |
|
| 70 |
+
def _load_chunks_from_cloud(self) -> None:
|
| 71 |
+
"""Load chunk data from cloud or local files"""
|
| 72 |
+
try:
|
| 73 |
+
from cloud_loader import cloud_loader
|
| 74 |
+
|
| 75 |
+
# Load emergency chunks
|
| 76 |
+
emergency_chunks_path = cloud_loader.get_model_file_path("models/embeddings/emergency_chunks.json")
|
| 77 |
+
with open(emergency_chunks_path, 'r', encoding='utf-8') as f:
|
| 78 |
+
emergency_data = json.load(f)
|
| 79 |
+
self.emergency_chunks = {i: chunk for i, chunk in enumerate(emergency_data)}
|
| 80 |
+
|
| 81 |
+
# Load treatment chunks
|
| 82 |
+
treatment_chunks_path = cloud_loader.get_model_file_path("models/embeddings/treatment_chunks.json")
|
| 83 |
+
with open(treatment_chunks_path, 'r', encoding='utf-8') as f:
|
| 84 |
+
treatment_data = json.load(f)
|
| 85 |
+
self.treatment_chunks = {i: chunk for i, chunk in enumerate(treatment_data)}
|
| 86 |
+
|
| 87 |
+
logger.info(f"Loaded {len(self.emergency_chunks)} emergency and {len(self.treatment_chunks)} treatment chunks")
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
logger.error(f"Failed to load chunks: {e}")
|
| 91 |
+
raise
|
| 92 |
+
|
| 93 |
+
def _load_embeddings_from_cloud(self) -> None:
|
| 94 |
+
"""Load embeddings from cloud or local files"""
|
| 95 |
+
try:
|
| 96 |
+
from cloud_loader import cloud_loader
|
| 97 |
+
|
| 98 |
+
# Load emergency embeddings
|
| 99 |
+
emergency_embeddings_path = cloud_loader.get_model_file_path("models/embeddings/emergency_embeddings.npy")
|
| 100 |
+
self.emergency_embeddings = np.load(emergency_embeddings_path)
|
| 101 |
+
|
| 102 |
+
# Load treatment embeddings
|
| 103 |
+
treatment_embeddings_path = cloud_loader.get_model_file_path("models/embeddings/treatment_embeddings.npy")
|
| 104 |
+
self.treatment_embeddings = np.load(treatment_embeddings_path)
|
| 105 |
+
|
| 106 |
+
logger.info("Embeddings loaded successfully")
|
| 107 |
+
|
| 108 |
+
except Exception as e:
|
| 109 |
+
logger.error(f"Failed to load embeddings: {e}")
|
| 110 |
+
raise
|
| 111 |
+
|
| 112 |
+
def _build_or_load_indices_from_cloud(self) -> None:
|
| 113 |
+
"""Build or load Annoy indices from cloud or local files"""
|
| 114 |
+
try:
|
| 115 |
+
from cloud_loader import cloud_loader
|
| 116 |
+
|
| 117 |
+
# Load emergency index
|
| 118 |
+
emergency_index_path = cloud_loader.get_model_file_path("models/indices/annoy/emergency.ann")
|
| 119 |
+
self.emergency_index.load(emergency_index_path)
|
| 120 |
+
|
| 121 |
+
# Load treatment index
|
| 122 |
+
treatment_index_path = cloud_loader.get_model_file_path("models/indices/annoy/treatment.ann")
|
| 123 |
+
self.treatment_index.load(treatment_index_path)
|
| 124 |
+
|
| 125 |
+
logger.info("Annoy indices loaded successfully")
|
| 126 |
+
|
| 127 |
+
except Exception as e:
|
| 128 |
+
logger.error(f"Failed to load indices: {e}")
|
| 129 |
+
raise
|
| 130 |
+
|
| 131 |
def _load_chunks(self, base_path: Path) -> None:
|
| 132 |
"""Load chunk data from JSON files"""
|
| 133 |
try:
|
test_stage1_cloud_loader.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""階段 1 測試:雲端載入器獨立功能測試"""
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import sys
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
# 設置環境變數測試雲端模式
|
| 9 |
+
os.environ['USE_CLOUD_DATA'] = 'true'
|
| 10 |
+
|
| 11 |
+
# 添加 src 到路徑
|
| 12 |
+
current_dir = Path(__file__).parent
|
| 13 |
+
src_dir = current_dir / "src"
|
| 14 |
+
sys.path.insert(0, str(src_dir))
|
| 15 |
+
|
| 16 |
+
def test_cloud_loader():
|
| 17 |
+
"""測試雲端載入器基礎功能"""
|
| 18 |
+
print("🧪 階段 1 測試:雲端載入器連線...")
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
from cloud_loader import cloud_loader
|
| 22 |
+
print("✅ cloud_loader 模組載入成功")
|
| 23 |
+
|
| 24 |
+
# 測試 Dataset Repository 連線
|
| 25 |
+
print(f"📊 Dataset Repository: {cloud_loader.dataset_repo}")
|
| 26 |
+
print(f"🔗 使用雲端模式: {cloud_loader.use_cloud}")
|
| 27 |
+
|
| 28 |
+
# 測試下載一個小檔案
|
| 29 |
+
print("📁 測試下載小檔案...")
|
| 30 |
+
test_file = cloud_loader.get_model_file_path("models/data_validation_report.json")
|
| 31 |
+
print(f"✅ 檔案下載成功: {test_file}")
|
| 32 |
+
|
| 33 |
+
# 檢查檔案是否存在
|
| 34 |
+
if os.path.exists(test_file):
|
| 35 |
+
print(f"✅ 檔案確實存在: {Path(test_file).stat().st_size} bytes")
|
| 36 |
+
else:
|
| 37 |
+
print("❌ 檔案下載後不存在")
|
| 38 |
+
return False
|
| 39 |
+
|
| 40 |
+
print("🎉 階段 1 測試通過:雲端載入器連線正常!")
|
| 41 |
+
return True
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"❌ 階段 1 測試失敗: {e}")
|
| 45 |
+
import traceback
|
| 46 |
+
traceback.print_exc()
|
| 47 |
+
return False
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
success = test_cloud_loader()
|
| 51 |
+
print(f"\n📋 測試結果: {'成功' if success else '失敗'}")
|
| 52 |
+
exit(0 if success else 1)
|
test_stage2_core_retrieval.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""階段 2 測試:核心檢索系統雲端載入測試"""
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import sys
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
# 設置環境變數測試雲端模式
|
| 9 |
+
os.environ['USE_CLOUD_DATA'] = 'true'
|
| 10 |
+
|
| 11 |
+
# 添加 src 到路徑
|
| 12 |
+
current_dir = Path(__file__).parent
|
| 13 |
+
src_dir = current_dir / "src"
|
| 14 |
+
sys.path.insert(0, str(src_dir))
|
| 15 |
+
|
| 16 |
+
def test_core_retrieval_system():
|
| 17 |
+
"""測試核心檢索系統雲端載入"""
|
| 18 |
+
print("🧪 階段 2 測試:核心檢索系統雲端載入...")
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
from retrieval import BasicRetrievalSystem
|
| 22 |
+
print("✅ BasicRetrievalSystem 模組載入成功")
|
| 23 |
+
|
| 24 |
+
# 初始化檢索系統 (會觸發雲端下載)
|
| 25 |
+
print("📊 初始化檢索系統...")
|
| 26 |
+
retrieval_system = BasicRetrievalSystem()
|
| 27 |
+
print("✅ 檢索系統初始化成功")
|
| 28 |
+
|
| 29 |
+
# 測試 emergency search (使用 general search 方法)
|
| 30 |
+
print("🚨 測試 emergency search...")
|
| 31 |
+
emergency_results = retrieval_system.search("chest pain emergency", top_k=3)
|
| 32 |
+
print(f"✅ Emergency search 成功,返回 {len(emergency_results.get('processed_results', []))} 個結果")
|
| 33 |
+
|
| 34 |
+
# 測試 treatment search (使用 general search 方法)
|
| 35 |
+
print("💊 測試 treatment search...")
|
| 36 |
+
treatment_results = retrieval_system.search("chest pain treatment", top_k=3)
|
| 37 |
+
print(f"✅ Treatment search 成功,返回 {len(treatment_results.get('processed_results', []))} 個結果")
|
| 38 |
+
|
| 39 |
+
# 測試 general search
|
| 40 |
+
print("🔍 測試 general search...")
|
| 41 |
+
general_results = retrieval_system.search("medical emergency", top_k=5)
|
| 42 |
+
print(f"✅ General search 成功,返回 {len(general_results.get('processed_results', []))} 個結果")
|
| 43 |
+
|
| 44 |
+
print("🎉 階段 2 測試通過:核心檢索系統雲端載入正常!")
|
| 45 |
+
return True
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"❌ 階段 2 測試失敗: {e}")
|
| 49 |
+
import traceback
|
| 50 |
+
traceback.print_exc()
|
| 51 |
+
return False
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
success = test_core_retrieval_system()
|
| 55 |
+
print(f"\n📋 測試結果: {'成功' if success else '失敗'}")
|
| 56 |
+
exit(0 if success else 1)
|