Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,11 @@
|
|
| 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
|
|
|
|
| 19 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# === 記憶模組相容多版本 ===
|
| 22 |
try:
|
|
@@ -29,88 +18,87 @@ except ImportError:
|
|
| 29 |
|
| 30 |
|
| 31 |
# =============================================
|
| 32 |
-
# 1️⃣ Embedding
|
| 33 |
# =============================================
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
if not os.path.exists(QA_PATH):
|
| 41 |
-
raise FileNotFoundError("❌ 找不到 QA 檔案 QA_v2.txt,請確認路徑。")
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
|
| 48 |
# =============================================
|
| 49 |
-
# 2️⃣ QA
|
| 50 |
# =============================================
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
text = f.read()
|
| 54 |
-
|
| 55 |
-
# 改進版正規表達式,確保每筆 QA 含問題與答案
|
| 56 |
-
pattern = r"(Q[::].*?A[::].*?)(?=Q[::]|$)"
|
| 57 |
-
qas = re.findall(pattern, text, flags=re.S)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 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 |
-
|
|
|
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 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 |
-
|
| 87 |
client=client,
|
| 88 |
-
collection_name=
|
| 89 |
embedding_function=embedding
|
| 90 |
)
|
| 91 |
-
if
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
print("✅ 向量資料庫已建立完成。")
|
| 96 |
|
| 97 |
|
| 98 |
# =============================================
|
| 99 |
-
# 4️⃣ 初始化 LLM
|
| 100 |
# =============================================
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 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
|
| 113 |
-
"""根據關鍵詞自動偵測使用者詢問的業務類別"""
|
| 114 |
if any(k in text for k in ["股票", "證券", "開戶", "下單", "交割"]):
|
| 115 |
return "證券"
|
| 116 |
elif any(k in text for k in ["期貨", "選擇權", "保證金"]):
|
|
@@ -122,14 +110,13 @@ def auto_detect_category(text: str):
|
|
| 122 |
|
| 123 |
|
| 124 |
def chat_fn(message, history):
|
| 125 |
-
"""核心對話函式"""
|
| 126 |
category = auto_detect_category(message)
|
| 127 |
-
vectordb = vectordbs
|
| 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 |
-
|
| 133 |
---
|
| 134 |
{context}
|
| 135 |
---
|
|
@@ -137,85 +124,162 @@ def chat_fn(message, history):
|
|
| 137 |
"""
|
| 138 |
|
| 139 |
try:
|
| 140 |
-
|
| 141 |
-
|
| 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️⃣
|
| 156 |
# =============================================
|
|
|
|
| 157 |
logo_base64 = ""
|
| 158 |
-
if os.path.exists(
|
| 159 |
-
with open(
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
"""
|
| 175 |
) as demo:
|
|
|
|
| 176 |
if logo_base64:
|
| 177 |
-
gr.HTML(f"
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
with gr.Row():
|
| 183 |
with gr.Column(scale=4):
|
| 184 |
-
|
| 185 |
|
| 186 |
-
with gr.Row():
|
| 187 |
-
user_input = gr.Textbox(
|
| 188 |
-
send_btn = gr.Button(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 195 |
-
|
|
|
|
|
|
|
| 196 |
return history, gr.update(value="")
|
| 197 |
|
| 198 |
-
user_input.submit(handle_input, [user_input,
|
| 199 |
-
send_btn.click(handle_input, [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 |
-
|
| 209 |
-
"未成年可以開戶嗎?",
|
| 210 |
-
"法人開戶要準備什麼?",
|
| 211 |
-
"期貨交易保證金是什麼?",
|
| 212 |
-
"複委託要如何下單?",
|
| 213 |
-
"美股交易時間?",
|
| 214 |
-
"美股可以定期定額嗎?"
|
| 215 |
]
|
| 216 |
-
for q in
|
| 217 |
-
gr.Button(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
|
|
|
| 219 |
gr.HTML("<div id='footer'>© Fintech Assistant — 僅業務使用,非官方授權</div>")
|
| 220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os, re, base64
|
|
|
|
|
|
|
| 2 |
from langchain_core.documents import Document
|
| 3 |
from langchain_chroma import Chroma
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
from langchain.embeddings.base import Embeddings
|
| 6 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 7 |
+
import chromadb
|
| 8 |
+
import gradio as gr
|
| 9 |
|
| 10 |
# === 記憶模組相容多版本 ===
|
| 11 |
try:
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
# =============================================
|
| 21 |
+
# 1️⃣ 自訂 LM Studio Embedding 類別
|
| 22 |
# =============================================
|
| 23 |
+
class LmStudioEmbeddings(Embeddings):
|
| 24 |
+
def __init__(self, model_name, url):
|
| 25 |
+
self.model_name = model_name
|
| 26 |
+
self.client = OpenAI(base_url=url, api_key="lm-studio")
|
| 27 |
|
| 28 |
+
def embed_query(self, text: str):
|
| 29 |
+
res = self.client.embeddings.create(input=text, model=self.model_name)
|
| 30 |
+
return res.data[0].embedding
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
def embed_documents(self, texts: list[str]):
|
| 33 |
+
res = self.client.embeddings.create(input=texts, model=self.model_name)
|
| 34 |
+
return [x.embedding for x in res.data]
|
| 35 |
|
| 36 |
|
| 37 |
# =============================================
|
| 38 |
+
# 2️⃣ 載入 QA 檔案並分類
|
| 39 |
# =============================================
|
| 40 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 41 |
+
qa_path = os.path.join(BASE_DIR, "QA_v2.txt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
if not os.path.exists(qa_path):
|
| 44 |
+
raise FileNotFoundError(f"❌ 找不到 QA 檔案:{qa_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
with open(qa_path, "r", encoding="utf-8") as f:
|
| 47 |
+
text = f.read()
|
| 48 |
|
| 49 |
+
pattern = r"(Q[::].*?)(?=Q[::]|$)"
|
| 50 |
+
qas = re.findall(pattern, text, flags=re.S)
|
| 51 |
+
qa_docs = {"證券": [], "期貨": [], "複委託": []}
|
| 52 |
+
for qa in qas:
|
| 53 |
+
if "證券" in qa:
|
| 54 |
+
qa_docs["證券"].append(Document(page_content=qa.strip()))
|
| 55 |
+
elif "期貨" in qa:
|
| 56 |
+
qa_docs["期貨"].append(Document(page_content=qa.strip()))
|
| 57 |
+
elif "複委託" in qa:
|
| 58 |
+
qa_docs["複委託"].append(Document(page_content=qa.strip()))
|
| 59 |
|
| 60 |
+
print("✅ 已成功讀取 QA 並完成分類:")
|
| 61 |
+
for k, v in qa_docs.items():
|
| 62 |
+
print(f" {k}:{len(v)} 筆")
|
| 63 |
|
| 64 |
|
| 65 |
# =============================================
|
| 66 |
+
# 3️⃣ 建立向量資料庫
|
| 67 |
# =============================================
|
| 68 |
+
embedding = LmStudioEmbeddings(
|
| 69 |
+
model_name="text-embedding-bge-large-zh-v1.5",
|
| 70 |
+
url="http://127.0.0.1:1234/v1"
|
| 71 |
+
)
|
| 72 |
client = chromadb.PersistentClient(path="./chroma_db")
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
collection_names = {"證券": "stocks", "期貨": "futures", "複委託": "overseas"}
|
| 75 |
+
vectordbs = {}
|
| 76 |
for cat, docs in qa_docs.items():
|
| 77 |
+
vectordbs[cat] = Chroma(
|
| 78 |
client=client,
|
| 79 |
+
collection_name=collection_names[cat],
|
| 80 |
embedding_function=embedding
|
| 81 |
)
|
| 82 |
+
if len(vectordbs[cat].get()["documents"]) == 0:
|
| 83 |
+
vectordbs[cat].add_documents(docs)
|
| 84 |
+
print("✅ 各類別向���資料庫建立完成")
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
# =============================================
|
| 88 |
+
# 4️⃣ 初始化 Gemini LLM
|
| 89 |
# =============================================
|
| 90 |
+
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 91 |
+
if not API_KEY:
|
| 92 |
+
raise ValueError("⚠️ 未設定 GOOGLE_API_KEY,請在 Hugging Face Secrets 中新增。")
|
|
|
|
| 93 |
|
| 94 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", google_api_key=API_KEY)
|
| 95 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 96 |
|
| 97 |
|
| 98 |
# =============================================
|
| 99 |
# 5️⃣ 對話邏輯
|
| 100 |
# =============================================
|
| 101 |
+
def auto_detect_category(text):
|
|
|
|
| 102 |
if any(k in text for k in ["股票", "證券", "開戶", "下單", "交割"]):
|
| 103 |
return "證券"
|
| 104 |
elif any(k in text for k in ["期貨", "選擇權", "保證金"]):
|
|
|
|
| 110 |
|
| 111 |
|
| 112 |
def chat_fn(message, history):
|
|
|
|
| 113 |
category = auto_detect_category(message)
|
| 114 |
+
vectordb = vectordbs.get(category)
|
| 115 |
docs = vectordb.similarity_search(message, k=2)
|
| 116 |
+
context = "\n\n".join([d.page_content for d in docs]) if docs else "查無資料"
|
| 117 |
|
| 118 |
prompt = f"""
|
| 119 |
+
你是一位金融客服人員,根據以下公司QA回答客戶問題:
|
| 120 |
---
|
| 121 |
{context}
|
| 122 |
---
|
|
|
|
| 124 |
"""
|
| 125 |
|
| 126 |
try:
|
| 127 |
+
response = llm.invoke(prompt)
|
| 128 |
+
reply = response.content.strip()
|
|
|
|
|
|
|
|
|
|
| 129 |
except Exception as e:
|
| 130 |
reply = f"⚠️ 生成錯誤:{e}"
|
| 131 |
+
return reply or "請洽營業員"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
|
| 134 |
# =============================================
|
| 135 |
+
# 6️⃣ 介面(含飛出動畫 + 純白footer)
|
| 136 |
# =============================================
|
| 137 |
+
logo_path = os.path.join(BASE_DIR, "mega.png")
|
| 138 |
logo_base64 = ""
|
| 139 |
+
if os.path.exists(logo_path):
|
| 140 |
+
with open(logo_path, "rb") as f:
|
| 141 |
logo_base64 = base64.b64encode(f.read()).decode("utf-8")
|
| 142 |
|
| 143 |
with gr.Blocks(
|
| 144 |
theme="soft",
|
| 145 |
css="""
|
| 146 |
+
/* ====== logo ====== */
|
| 147 |
#logo-top {
|
| 148 |
position: fixed; top: 12px; left: 18px;
|
| 149 |
background-color: white; border-radius: 10px;
|
| 150 |
padding: 6px 8px; box-shadow: 0 0 8px rgba(0,0,0,0.15);
|
|
|
|
| 151 |
}
|
| 152 |
#logo-top img { width: 120px; height: auto; display: block; }
|
| 153 |
|
| 154 |
+
/* ====== 標題 ====== */
|
| 155 |
+
#main-title {
|
| 156 |
+
font-size: 28px; font-weight: bold; text-align: center;
|
| 157 |
+
line-height: 1.4; margin: 0; display: inline-block;
|
| 158 |
+
}
|
| 159 |
+
@media (max-width: 768px) {
|
| 160 |
+
#main-title { font-size: 24px; white-space: pre-line; }
|
| 161 |
+
#main-title::before {
|
| 162 |
+
content: "👨💼 我是小智\\A您的金融好幫手 🫰";
|
| 163 |
+
white-space: pre;
|
| 164 |
+
}
|
| 165 |
+
#main-title span { display: none; }
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
/* ====== footer(純白背景) ====== */
|
| 169 |
+
#footer {
|
| 170 |
+
position: fixed;
|
| 171 |
+
bottom: 40px;
|
| 172 |
+
left: 0;
|
| 173 |
+
width: 100%;
|
| 174 |
+
text-align: center;
|
| 175 |
+
font-size: 13px;
|
| 176 |
+
color: #aaa; /* ✅ 更淡的灰色 */
|
| 177 |
+
border-top: 1px solid #ddd; /* ✅ 細灰分隔線 */
|
| 178 |
+
padding-top: 8px;
|
| 179 |
+
background-color: transparent; /* ✅ 移除反白 */
|
| 180 |
+
}
|
| 181 |
+
@media (max-width: 768px) {
|
| 182 |
+
#footer { position: relative; margin-top: 40px; }
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
/* ====== LINE 風格輸入區 ====== */
|
| 186 |
+
#input-row { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
|
| 187 |
+
#user-input {
|
| 188 |
+
flex-grow: 1; border-radius: 20px; border: 1px solid #ccc;
|
| 189 |
+
padding: 6px 12px; font-size: 15px; background-color: #fff;
|
| 190 |
+
box-shadow: inset 0 0 1px rgba(0,0,0,0.05);
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
/* 🟢 小巧箭頭按鈕(含飛出動畫) */
|
| 194 |
+
#send-btn {
|
| 195 |
+
background-color: #00b800; border: none; border-radius: 50%;
|
| 196 |
+
width: 28px; height: 28px; cursor: pointer;
|
| 197 |
+
display: flex; align-items: center; justify-content: center;
|
| 198 |
+
transition: background-color 0.2s ease, transform 0.1s ease;
|
| 199 |
+
box-shadow: 0 1px 2px rgba(0,0,0,0.1); padding: 0; overflow: hidden;
|
| 200 |
+
}
|
| 201 |
+
#send-btn svg {
|
| 202 |
+
width: 12px; height: 12px; fill: white;
|
| 203 |
+
transition: transform 0.25s ease;
|
| 204 |
+
}
|
| 205 |
+
#send-btn:hover { background-color: #00a000; }
|
| 206 |
+
#send-btn:hover svg { transform: rotate(10deg) scale(1.15); }
|
| 207 |
+
#send-btn:active { transform: scale(0.9); }
|
| 208 |
+
#send-btn:active svg { animation: send-fly 0.5s ease-out; }
|
| 209 |
+
|
| 210 |
+
@keyframes send-fly {
|
| 211 |
+
0% { transform: translateX(0) scale(1); opacity: 1; }
|
| 212 |
+
50% { transform: translateX(8px) scale(1.2); opacity: 0; }
|
| 213 |
+
100% { transform: translateX(0) scale(1); opacity: 1; }
|
| 214 |
+
}
|
| 215 |
"""
|
| 216 |
) as demo:
|
| 217 |
+
# 左上角 logo
|
| 218 |
if logo_base64:
|
| 219 |
+
gr.HTML(f"""
|
| 220 |
+
<div id="logo-top"><img src="data:image/png;base64,{logo_base64}" alt="logo"></div>
|
| 221 |
+
""")
|
| 222 |
+
|
| 223 |
+
# 標題區
|
| 224 |
+
gr.HTML("""
|
| 225 |
+
<div id="main-title-wrapper" style="text-align:center; margin-top:20px;">
|
| 226 |
+
<h1 id='main-title'><span>👨💼 我是小智 您的金融好幫手 🫰</span></h1>
|
| 227 |
+
<p id='sub-title' style='margin-top:10px; font-size:14px; color:#666;'>Powered by Gemini & LangChain</p>
|
| 228 |
+
</div>
|
| 229 |
+
""")
|
| 230 |
+
|
| 231 |
+
# 聊天介面
|
| 232 |
with gr.Row():
|
| 233 |
with gr.Column(scale=4):
|
| 234 |
+
chatbox = gr.Chatbot(label="💬 對話紀錄", type="messages")
|
| 235 |
|
| 236 |
+
with gr.Row(elem_id="input-row"):
|
| 237 |
+
user_input = gr.Textbox(elem_id="user-input", show_label=False, placeholder="輸入訊息...", scale=8)
|
| 238 |
+
send_btn = gr.Button(
|
| 239 |
+
value="""
|
| 240 |
+
<svg viewBox="0 0 24 24"><path d="M3 12l18-9-6 9 6 9z"/></svg>
|
| 241 |
+
""", elem_id="send-btn", scale=1
|
| 242 |
+
)
|
| 243 |
|
| 244 |
def handle_input(message, history):
|
| 245 |
if not message.strip():
|
| 246 |
return history, gr.update(value="")
|
| 247 |
reply = chat_fn(message, history)
|
| 248 |
+
history = history + [
|
| 249 |
+
{"role": "user", "content": message},
|
| 250 |
+
{"role": "assistant", "content": reply}
|
| 251 |
+
]
|
| 252 |
return history, gr.update(value="")
|
| 253 |
|
| 254 |
+
user_input.submit(handle_input, [user_input, chatbox], [chatbox, user_input])
|
| 255 |
+
send_btn.click(handle_input, [user_input, chatbox], [chatbox, user_input])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
with gr.Column(scale=1):
|
| 258 |
+
gr.Markdown("### 👇 快速提問")
|
| 259 |
+
btns = [
|
| 260 |
+
("未成年可以開戶嗎?", "未成年可以開戶嗎?"),
|
| 261 |
+
("法人開戶要準備什麼?", "法人開戶要準備什麼?"),
|
| 262 |
+
("期貨交易保證金是什麼?", "期貨交易保證金是什麼?"),
|
| 263 |
+
("複委託要如何下單?", "複委託要如何下單?"),
|
| 264 |
+
("美股交易時間?", "美股交易時間?"),
|
| 265 |
+
("美股可以定期定額嗎?", "美股可以定期定額嗎?")
|
| 266 |
]
|
| 267 |
+
for label, q in btns:
|
| 268 |
+
gr.Button(label).click(lambda h, q=q: handle_input(q, h), [chatbox], [chatbox, user_input])
|
| 269 |
+
|
| 270 |
+
def clear_memory():
|
| 271 |
+
memory.clear()
|
| 272 |
+
return [], gr.update(value="", placeholder="輸入訊息...")
|
| 273 |
+
gr.Button("🧹 整理畫面").click(clear_memory, outputs=[chatbox, user_input])
|
| 274 |
|
| 275 |
+
# 底部 footer(純白)
|
| 276 |
gr.HTML("<div id='footer'>© Fintech Assistant — 僅業務使用,非官方授權</div>")
|
| 277 |
|
| 278 |
+
# 手機鍵盤彈出自動捲動
|
| 279 |
+
demo.load(None, None, None, js="""
|
| 280 |
+
window.addEventListener('focusin', () => {
|
| 281 |
+
document.querySelector('textarea')?.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
| 282 |
+
});
|
| 283 |
+
""")
|
| 284 |
+
|
| 285 |
demo.launch()
|