Update app.py
Browse files
app.py
CHANGED
|
@@ -10,9 +10,12 @@ import time
|
|
| 10 |
# --- 系統設定 ---
|
| 11 |
SYSTEM_TITLE = "花蓮慈濟醫院公文輔助判決系統"
|
| 12 |
FILE_PATH = 'data.csv'
|
| 13 |
-
# ▼▼▼ 關鍵:定義索引檔案儲存路徑 ▼▼▼
|
| 14 |
INDEX_FILE = 'corpus_embeddings.pt'
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# --- 1. 讀取資料 ---
|
| 17 |
print("🚀 正在啟動快取模式...")
|
| 18 |
|
|
@@ -21,7 +24,6 @@ if not os.path.exists(FILE_PATH):
|
|
| 21 |
sys.exit(1)
|
| 22 |
|
| 23 |
try:
|
| 24 |
-
# 讀取檔案 (維持 CP950 容錯)
|
| 25 |
df = pd.read_csv(FILE_PATH, encoding='cp950')
|
| 26 |
except UnicodeDecodeError:
|
| 27 |
try:
|
|
@@ -50,9 +52,7 @@ else:
|
|
| 50 |
corpus = []
|
| 51 |
total_records = 0
|
| 52 |
|
| 53 |
-
# --- 3. 載入模型與建立索引
|
| 54 |
-
|
| 55 |
-
# 檢查模型是否已經載入
|
| 56 |
model = None
|
| 57 |
try:
|
| 58 |
print("🧠 正在載入模型 (BAAI/bge-small-zh-v1.5)...")
|
|
@@ -64,21 +64,18 @@ corpus_embeddings = None
|
|
| 64 |
|
| 65 |
if total_records > 0 and model is not None:
|
| 66 |
if os.path.exists(INDEX_FILE):
|
| 67 |
-
|
| 68 |
-
print(f"⚡ 偵測到快取檔案 ({INDEX_FILE}),正在秒速載入...")
|
| 69 |
try:
|
| 70 |
corpus_embeddings = torch.load(INDEX_FILE)
|
| 71 |
-
print("✅
|
| 72 |
except Exception as e:
|
| 73 |
-
print(f"❌
|
| 74 |
-
corpus_embeddings = None
|
| 75 |
|
| 76 |
if corpus_embeddings is None:
|
| 77 |
-
|
| 78 |
-
print(f"🔥 第一次啟動或快取失效,開始分批計算索引 (這需要約 2-4 分鐘)...")
|
| 79 |
chunk_size = 500
|
| 80 |
embeddings_chunks = []
|
| 81 |
-
start_time = time.time()
|
| 82 |
|
| 83 |
try:
|
| 84 |
for i in range(0, total_records, chunk_size):
|
|
@@ -88,23 +85,18 @@ if total_records > 0 and model is not None:
|
|
| 88 |
print(f" -> 已處理 {min(i + chunk_size, total_records)} / {total_records} 筆...")
|
| 89 |
gc.collect()
|
| 90 |
|
| 91 |
-
# 合併與儲存
|
| 92 |
-
print("🔗 正在合併並儲存索引...")
|
| 93 |
corpus_embeddings = torch.cat(embeddings_chunks)
|
| 94 |
-
torch.save(corpus_embeddings, INDEX_FILE)
|
| 95 |
-
|
| 96 |
-
end_time = time.time()
|
| 97 |
-
print(f"✅ 全量索引計算並儲存完成!耗時 {int(end_time - start_time)} 秒。")
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
-
print(f"❌
|
| 101 |
corpus_embeddings = None
|
| 102 |
|
| 103 |
# --- 4. 定義搜尋 ---
|
| 104 |
def search_department(query):
|
| 105 |
-
# 這裡的邏輯與之前相同,不需要修改
|
| 106 |
if corpus_embeddings is None:
|
| 107 |
-
return "⚠️
|
| 108 |
|
| 109 |
if not query.strip():
|
| 110 |
return "請輸入公文主旨..."
|
|
@@ -134,15 +126,16 @@ def search_department(query):
|
|
| 134 |
|
| 135 |
return output_text
|
| 136 |
|
| 137 |
-
# --- 5. 介面 ---
|
| 138 |
iface = gr.Interface(
|
| 139 |
fn=search_department,
|
| 140 |
inputs=gr.Textbox(lines=3, placeholder="請輸入公文主旨..."),
|
| 141 |
outputs=gr.Textbox(lines=12, label="AI 判決建議"),
|
| 142 |
title=SYSTEM_TITLE,
|
| 143 |
-
description=f"系統狀態:{'🟢
|
| 144 |
examples=[["檢送本署彙整人工生殖機構之捐贈生殖細胞使用情形"], ["函轉衛生局關於流感疫苗接種計畫"]]
|
| 145 |
)
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
-
|
|
|
|
|
|
| 10 |
# --- 系統設定 ---
|
| 11 |
SYSTEM_TITLE = "花蓮慈濟醫院公文輔助判決系統"
|
| 12 |
FILE_PATH = 'data.csv'
|
|
|
|
| 13 |
INDEX_FILE = 'corpus_embeddings.pt'
|
| 14 |
|
| 15 |
+
# ▼▼▼ 設定登入帳號密碼 (您可以修改這裡) ▼▼▼
|
| 16 |
+
# 格式:("帳號", "密碼")
|
| 17 |
+
LOGIN_DATA = ("admin", "1234")
|
| 18 |
+
|
| 19 |
# --- 1. 讀取資料 ---
|
| 20 |
print("🚀 正在啟動快取模式...")
|
| 21 |
|
|
|
|
| 24 |
sys.exit(1)
|
| 25 |
|
| 26 |
try:
|
|
|
|
| 27 |
df = pd.read_csv(FILE_PATH, encoding='cp950')
|
| 28 |
except UnicodeDecodeError:
|
| 29 |
try:
|
|
|
|
| 52 |
corpus = []
|
| 53 |
total_records = 0
|
| 54 |
|
| 55 |
+
# --- 3. 載入模型與建立索引 ---
|
|
|
|
|
|
|
| 56 |
model = None
|
| 57 |
try:
|
| 58 |
print("🧠 正在載入模型 (BAAI/bge-small-zh-v1.5)...")
|
|
|
|
| 64 |
|
| 65 |
if total_records > 0 and model is not None:
|
| 66 |
if os.path.exists(INDEX_FILE):
|
| 67 |
+
print(f"⚡ 偵測到快取檔案,正在秒速載入...")
|
|
|
|
| 68 |
try:
|
| 69 |
corpus_embeddings = torch.load(INDEX_FILE)
|
| 70 |
+
print("✅ 索引載入完成!")
|
| 71 |
except Exception as e:
|
| 72 |
+
print(f"❌ 快取檔案損壞,將重新計算。錯誤: {e}")
|
| 73 |
+
corpus_embeddings = None
|
| 74 |
|
| 75 |
if corpus_embeddings is None:
|
| 76 |
+
print(f"🔥 開始計算索引 (需時約 2-4 分鐘)...")
|
|
|
|
| 77 |
chunk_size = 500
|
| 78 |
embeddings_chunks = []
|
|
|
|
| 79 |
|
| 80 |
try:
|
| 81 |
for i in range(0, total_records, chunk_size):
|
|
|
|
| 85 |
print(f" -> 已處理 {min(i + chunk_size, total_records)} / {total_records} 筆...")
|
| 86 |
gc.collect()
|
| 87 |
|
|
|
|
|
|
|
| 88 |
corpus_embeddings = torch.cat(embeddings_chunks)
|
| 89 |
+
torch.save(corpus_embeddings, INDEX_FILE)
|
| 90 |
+
print("✅ 索引計算並儲存完成!")
|
|
|
|
|
|
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
+
print(f"❌ 索引計算失敗: {e}")
|
| 94 |
corpus_embeddings = None
|
| 95 |
|
| 96 |
# --- 4. 定義搜尋 ---
|
| 97 |
def search_department(query):
|
|
|
|
| 98 |
if corpus_embeddings is None:
|
| 99 |
+
return "⚠️ 系統初始化失敗。"
|
| 100 |
|
| 101 |
if not query.strip():
|
| 102 |
return "請輸入公文主旨..."
|
|
|
|
| 126 |
|
| 127 |
return output_text
|
| 128 |
|
| 129 |
+
# --- 5. 介面 (包含密碼鎖) ---
|
| 130 |
iface = gr.Interface(
|
| 131 |
fn=search_department,
|
| 132 |
inputs=gr.Textbox(lines=3, placeholder="請輸入公文主旨..."),
|
| 133 |
outputs=gr.Textbox(lines=12, label="AI 判決建議"),
|
| 134 |
title=SYSTEM_TITLE,
|
| 135 |
+
description=f"系統狀態:{'🟢 系統正常' if corpus_embeddings is not None else '🔴 異常'}\n資料庫收錄:{total_records} 筆歷史資料",
|
| 136 |
examples=[["檢送本署彙整人工生殖機構之捐贈生殖細胞使用情形"], ["函轉衛生局關於流感疫苗接種計畫"]]
|
| 137 |
)
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
+
# ▼▼▼ 這裡加上了 auth 參數,啟動時會要求輸入帳號密碼 ▼▼▼
|
| 141 |
+
iface.launch(auth=LOGIN_DATA)
|