Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os, re, base64
|
|
|
|
|
|
|
| 2 |
from langchain_core.documents import Document
|
| 3 |
from langchain_chroma import Chroma
|
| 4 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 5 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 6 |
-
import chromadb
|
| 7 |
-
import gradio as gr
|
| 8 |
|
|
|
|
| 9 |
try:
|
| 10 |
from langchain_memory import ConversationBufferMemory
|
| 11 |
except ImportError:
|
|
@@ -14,59 +27,90 @@ except ImportError:
|
|
| 14 |
except ImportError:
|
| 15 |
from langchain_community.memory import ConversationBufferMemory
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
embedding = HuggingFaceEmbeddings(model_name="BAAI/bge-small-zh-v1.5")
|
| 19 |
|
| 20 |
-
# === 載入 QA ===
|
| 21 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
client = chromadb.PersistentClient(path="./chroma_db")
|
| 46 |
-
|
| 47 |
vectordbs = {}
|
|
|
|
| 48 |
for cat, docs in qa_docs.items():
|
| 49 |
-
|
| 50 |
client=client,
|
| 51 |
-
collection_name=
|
| 52 |
embedding_function=embedding
|
| 53 |
)
|
| 54 |
-
if
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
print(f"⚙️ 已載入現有向量資料庫:{collections[cat]}")
|
| 58 |
-
print("✅ 各類別向量資料庫建立完成")
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", google_api_key=API_KEY)
|
| 66 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
if any(k in text for k in ["股票", "證券", "開戶", "下單", "交割"]):
|
| 71 |
return "證券"
|
| 72 |
elif any(k in text for k in ["期貨", "選擇權", "保證金"]):
|
|
@@ -76,95 +120,102 @@ def auto_detect_category(text):
|
|
| 76 |
else:
|
| 77 |
return "證券"
|
| 78 |
|
|
|
|
| 79 |
def chat_fn(message, history):
|
|
|
|
| 80 |
category = auto_detect_category(message)
|
| 81 |
-
vectordb = vectordbs
|
| 82 |
docs = vectordb.similarity_search(message, k=2)
|
| 83 |
-
context = "\n\n".join(
|
|
|
|
| 84 |
prompt = f"""
|
| 85 |
-
|
| 86 |
---
|
| 87 |
{context}
|
| 88 |
---
|
| 89 |
使用者問題:{message}
|
| 90 |
"""
|
|
|
|
| 91 |
try:
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
reply = f"⚠️ 生成錯誤:{e}"
|
| 96 |
-
return reply or "請洽營業員"
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
logo_base64 = ""
|
| 101 |
-
if os.path.exists(
|
| 102 |
-
with open(
|
| 103 |
logo_base64 = base64.b64encode(f.read()).decode("utf-8")
|
| 104 |
|
| 105 |
with gr.Blocks(
|
| 106 |
theme="soft",
|
| 107 |
css="""
|
| 108 |
-
#logo-top {
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
#main-title{text-align:center;font-size:28px;font-weight:bold;margin:0;line-height:1.4;}
|
| 114 |
-
@media (max-width:768px){
|
| 115 |
-
#main-title{white-space:pre-line;font-size:24px;}
|
| 116 |
}
|
|
|
|
| 117 |
|
| 118 |
-
#footer{text-align:center;font-size:13px;color:#
|
| 119 |
-
padding-top:8px;margin-top:30px;background:transparent;}
|
| 120 |
-
|
| 121 |
-
#input-row{display:flex;align-items:center;gap:8px;margin-top:10px;}
|
| 122 |
-
#user-input{flex-grow:1;border-radius:20px;border:1px solid #ccc;
|
| 123 |
-
padding:6px 12px;font-size:15px;background-color:#fff;}
|
| 124 |
-
#send-btn{background:#00b800;color:white;border:none;border-radius:14px;
|
| 125 |
-
height:26px;padding:0 10px;font-size:13px;font-weight:600;cursor:pointer;}
|
| 126 |
-
#send-btn:hover{background:#00a000;}
|
| 127 |
"""
|
| 128 |
) as demo:
|
| 129 |
if logo_base64:
|
| 130 |
gr.HTML(f"<div id='logo-top'><img src='data:image/png;base64,{logo_base64}'></div>")
|
| 131 |
|
| 132 |
-
gr.
|
| 133 |
-
|
| 134 |
-
<p style='text-align:center;margin-top:8px;color:#666;'>Powered by Gemini & LangChain</p>
|
| 135 |
-
""")
|
| 136 |
|
| 137 |
with gr.Row():
|
| 138 |
with gr.Column(scale=4):
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
def handle_input(message, history):
|
| 144 |
if not message.strip():
|
| 145 |
return history, gr.update(value="")
|
| 146 |
reply = chat_fn(message, history)
|
| 147 |
-
history += [{"role":"user","content":message},
|
| 148 |
-
{"role":"assistant","content":reply}]
|
| 149 |
return history, gr.update(value="")
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
("未成年可以開戶嗎?","未成年可以開戶嗎?"),
|
| 156 |
-
("法人開戶要準備什麼?","法人開戶要準備什麼?"),
|
| 157 |
-
("期貨交易保證金是什麼?","期貨交易保證金是什麼?"),
|
| 158 |
-
("複委託要如何下單?","複委託要如何下單?"),
|
| 159 |
-
("美股交易時間?","美股交易時間?"),
|
| 160 |
-
("美股可以定期定額嗎?","美股可以定期定額嗎?")
|
| 161 |
-
]:
|
| 162 |
-
gr.Button(label).click(lambda h,q=q: handle_input(q,h),[chatbox],[chatbox,user_input])
|
| 163 |
-
def clear_memory():
|
| 164 |
memory.clear()
|
| 165 |
-
return [],gr.update(value=""
|
| 166 |
-
gr.Button("🧹
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
gr.HTML("<div id='footer'>© Fintech Assistant — 僅業務使用,非官方授權</div>")
|
| 169 |
|
| 170 |
-
demo.launch()
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
======================================================
|
| 3 |
+
📘 金融客服小智(Fintech Assistant)
|
| 4 |
+
版本:v2 (重構示範 by Supervisor)
|
| 5 |
+
改進重點:
|
| 6 |
+
1. 模組化程式結構(易維護)
|
| 7 |
+
2. 加入記憶體保存(多輪對話)
|
| 8 |
+
3. 改善 Chroma 初始化與 QA 擷取
|
| 9 |
+
4. 加強異常處理與容錯提示
|
| 10 |
+
======================================================
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
import os, re, base64
|
| 14 |
+
import chromadb
|
| 15 |
+
import gradio as gr
|
| 16 |
from langchain_core.documents import Document
|
| 17 |
from langchain_chroma import Chroma
|
| 18 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 19 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# === 記憶模組相容多版本 ===
|
| 22 |
try:
|
| 23 |
from langchain_memory import ConversationBufferMemory
|
| 24 |
except ImportError:
|
|
|
|
| 27 |
except ImportError:
|
| 28 |
from langchain_community.memory import ConversationBufferMemory
|
| 29 |
|
| 30 |
+
|
| 31 |
+
# =============================================
|
| 32 |
+
# 1️⃣ Embedding 與基礎設定
|
| 33 |
+
# =============================================
|
| 34 |
embedding = HuggingFaceEmbeddings(model_name="BAAI/bge-small-zh-v1.5")
|
| 35 |
|
|
|
|
| 36 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 37 |
+
QA_PATH = os.path.join(BASE_DIR, "QA_v2.txt")
|
| 38 |
+
LOGO_PATH = os.path.join(BASE_DIR, "mega.png")
|
| 39 |
+
|
| 40 |
+
if not os.path.exists(QA_PATH):
|
| 41 |
+
raise FileNotFoundError("❌ 找不到 QA 檔案 QA_v2.txt,請確認路徑。")
|
| 42 |
+
|
| 43 |
+
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 44 |
+
if not API_KEY:
|
| 45 |
+
print("⚠️ 尚未設定 GOOGLE_API_KEY,系統將以模擬回覆運行。")
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# =============================================
|
| 49 |
+
# 2️⃣ QA 載入與分類(改進版正規化)
|
| 50 |
+
# =============================================
|
| 51 |
+
def load_qa_documents(path: str):
|
| 52 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 53 |
+
text = f.read()
|
| 54 |
+
|
| 55 |
+
# 改進版正規表達式,確保每筆 QA 含問題與答案
|
| 56 |
+
pattern = r"(Q[::].*?A[::].*?)(?=Q[::]|$)"
|
| 57 |
+
qas = re.findall(pattern, text, flags=re.S)
|
| 58 |
+
|
| 59 |
+
categories = {"證券": [], "期貨": [], "複委託": []}
|
| 60 |
+
for qa in qas:
|
| 61 |
+
doc = Document(page_content=qa.strip())
|
| 62 |
+
if "證券" in qa:
|
| 63 |
+
categories["證券"].append(doc)
|
| 64 |
+
elif "期貨" in qa:
|
| 65 |
+
categories["期貨"].append(doc)
|
| 66 |
+
elif "複委託" in qa:
|
| 67 |
+
categories["複委託"].append(doc)
|
| 68 |
+
else:
|
| 69 |
+
categories["證券"].append(doc) # 預設分類
|
| 70 |
+
|
| 71 |
+
return categories
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
qa_docs = load_qa_documents(QA_PATH)
|
| 75 |
+
print("✅ 已成功載入 QA 檔案,共分為:", {k: len(v) for k, v in qa_docs.items()})
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# =============================================
|
| 79 |
+
# 3️⃣ 向量資料庫初始化(避免重複寫入)
|
| 80 |
+
# =============================================
|
| 81 |
client = chromadb.PersistentClient(path="./chroma_db")
|
| 82 |
+
collection_map = {"證券": "stocks", "期貨": "futures", "複委託": "overseas"}
|
| 83 |
vectordbs = {}
|
| 84 |
+
|
| 85 |
for cat, docs in qa_docs.items():
|
| 86 |
+
vectordb = Chroma(
|
| 87 |
client=client,
|
| 88 |
+
collection_name=collection_map[cat],
|
| 89 |
embedding_function=embedding
|
| 90 |
)
|
| 91 |
+
if vectordb._collection.count() == 0:
|
| 92 |
+
vectordb.add_documents(docs)
|
| 93 |
+
vectordbs[cat] = vectordb
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
print("✅ 向量資料庫已建立完成。")
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# =============================================
|
| 99 |
+
# 4️⃣ 初始化 LLM 與對話記憶
|
| 100 |
+
# =============================================
|
| 101 |
+
if API_KEY:
|
| 102 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", google_api_key=API_KEY)
|
| 103 |
+
else:
|
| 104 |
+
llm = None # 模擬模式
|
| 105 |
|
|
|
|
| 106 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 107 |
|
| 108 |
+
|
| 109 |
+
# =============================================
|
| 110 |
+
# 5️⃣ 對話邏輯
|
| 111 |
+
# =============================================
|
| 112 |
+
def auto_detect_category(text: str):
|
| 113 |
+
"""根據關鍵詞自動偵測使用者詢問的業務類別"""
|
| 114 |
if any(k in text for k in ["股票", "證券", "開戶", "下單", "交割"]):
|
| 115 |
return "證券"
|
| 116 |
elif any(k in text for k in ["期貨", "選擇權", "保證金"]):
|
|
|
|
| 120 |
else:
|
| 121 |
return "證券"
|
| 122 |
|
| 123 |
+
|
| 124 |
def chat_fn(message, history):
|
| 125 |
+
"""核心對話函式"""
|
| 126 |
category = auto_detect_category(message)
|
| 127 |
+
vectordb = vectordbs[category]
|
| 128 |
docs = vectordb.similarity_search(message, k=2)
|
| 129 |
+
context = "\n\n".join(d.page_content for d in docs) if docs else "查無相關資料"
|
| 130 |
+
|
| 131 |
prompt = f"""
|
| 132 |
+
你是一位金融客服人員,請根據以下QA知識回答:
|
| 133 |
---
|
| 134 |
{context}
|
| 135 |
---
|
| 136 |
使用者問題:{message}
|
| 137 |
"""
|
| 138 |
+
|
| 139 |
try:
|
| 140 |
+
if llm:
|
| 141 |
+
response = llm.invoke(prompt)
|
| 142 |
+
reply = getattr(response, "content", None) or getattr(response, "text", "⚠️ 無回覆")
|
| 143 |
+
else:
|
| 144 |
+
reply = "(模擬模式)這是示範回覆:請確認是否已設定 GOOGLE_API_KEY。"
|
| 145 |
except Exception as e:
|
| 146 |
reply = f"⚠️ 生成錯誤:{e}"
|
|
|
|
| 147 |
|
| 148 |
+
# 保存對話記憶
|
| 149 |
+
memory.save_context({"role": "user", "content": message},
|
| 150 |
+
{"role": "assistant", "content": reply})
|
| 151 |
+
return reply
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
# =============================================
|
| 155 |
+
# 6️⃣ Gradio 介面(重構版)
|
| 156 |
+
# =============================================
|
| 157 |
logo_base64 = ""
|
| 158 |
+
if os.path.exists(LOGO_PATH):
|
| 159 |
+
with open(LOGO_PATH, "rb") as f:
|
| 160 |
logo_base64 = base64.b64encode(f.read()).decode("utf-8")
|
| 161 |
|
| 162 |
with gr.Blocks(
|
| 163 |
theme="soft",
|
| 164 |
css="""
|
| 165 |
+
#logo-top {
|
| 166 |
+
position: fixed; top: 12px; left: 18px;
|
| 167 |
+
background-color: white; border-radius: 10px;
|
| 168 |
+
padding: 6px 8px; box-shadow: 0 0 8px rgba(0,0,0,0.15);
|
| 169 |
+
pointer-events: none;
|
|
|
|
|
|
|
|
|
|
| 170 |
}
|
| 171 |
+
#logo-top img { width: 120px; height: auto; display: block; }
|
| 172 |
|
| 173 |
+
#footer { text-align:center; font-size:13px; color:#aaa; margin-top: 20px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
"""
|
| 175 |
) as demo:
|
| 176 |
if logo_base64:
|
| 177 |
gr.HTML(f"<div id='logo-top'><img src='data:image/png;base64,{logo_base64}'></div>")
|
| 178 |
|
| 179 |
+
gr.Markdown("## 👨💼 我是小智 · 您的金融好幫手 🫰")
|
| 180 |
+
gr.Markdown("Powered by Gemini & LangChain")
|
|
|
|
|
|
|
| 181 |
|
| 182 |
with gr.Row():
|
| 183 |
with gr.Column(scale=4):
|
| 184 |
+
chatbot = gr.Chatbot(label="💬 對話紀錄", type="messages", height=500)
|
| 185 |
+
|
| 186 |
+
with gr.Row():
|
| 187 |
+
user_input = gr.Textbox(placeholder="請輸入問題...", show_label=False)
|
| 188 |
+
send_btn = gr.Button("送出", variant="primary")
|
| 189 |
+
|
| 190 |
def handle_input(message, history):
|
| 191 |
if not message.strip():
|
| 192 |
return history, gr.update(value="")
|
| 193 |
reply = chat_fn(message, history)
|
| 194 |
+
history += [{"role": "user", "content": message},
|
| 195 |
+
{"role": "assistant", "content": reply}]
|
| 196 |
return history, gr.update(value="")
|
| 197 |
+
|
| 198 |
+
user_input.submit(handle_input, [user_input, chatbot], [chatbot, user_input])
|
| 199 |
+
send_btn.click(handle_input, [user_input, chatbot], [chatbot, user_input])
|
| 200 |
+
|
| 201 |
+
def clear_all():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
memory.clear()
|
| 203 |
+
return [], gr.update(value="")
|
| 204 |
+
gr.Button("🧹 清除對話").click(clear_all, outputs=[chatbot, user_input])
|
| 205 |
+
|
| 206 |
+
with gr.Column(scale=1):
|
| 207 |
+
gr.Markdown("### 🔍 常見問題")
|
| 208 |
+
examples = [
|
| 209 |
+
"未成年可以開戶嗎?",
|
| 210 |
+
"法人開戶要準備什麼?",
|
| 211 |
+
"期貨交易保證金是什麼?",
|
| 212 |
+
"複委託要如何下單?",
|
| 213 |
+
"美股交易時間?",
|
| 214 |
+
"美股可以定期定額嗎?"
|
| 215 |
+
]
|
| 216 |
+
for q in examples:
|
| 217 |
+
gr.Button(q).click(lambda h, q=q: handle_input(q, h), [chatbot], [chatbot, user_input])
|
| 218 |
|
| 219 |
gr.HTML("<div id='footer'>© Fintech Assistant — 僅業務使用,非官方授權</div>")
|
| 220 |
|
| 221 |
+
demo.launch()
|