Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# ✅ app.py -
|
| 2 |
|
| 3 |
import json
|
| 4 |
import os
|
|
@@ -8,7 +8,7 @@ import torch
|
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
|
| 11 |
-
# ✅
|
| 12 |
QA_FILE = "qa.json"
|
| 13 |
TEXT_FILE = "web_data.txt"
|
| 14 |
DOCS_FILE = "docs.json"
|
|
@@ -42,7 +42,7 @@ tokenizer = AutoTokenizer.from_pretrained(GEN_MODEL, trust_remote_code=True)
|
|
| 42 |
model = AutoModelForCausalLM.from_pretrained(GEN_MODEL, trust_remote_code=True).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 43 |
model.eval()
|
| 44 |
|
| 45 |
-
# ✅ QA
|
| 46 |
|
| 47 |
def retrieve_qa_context(user_input):
|
| 48 |
for item in qa_data:
|
|
@@ -54,26 +54,52 @@ def retrieve_qa_context(user_input):
|
|
| 54 |
return item["response"]
|
| 55 |
return None
|
| 56 |
|
| 57 |
-
# ✅ 向量
|
| 58 |
|
| 59 |
def search_context_faiss(user_input, top_k=3):
|
| 60 |
vec = embedder.encode([user_input])
|
| 61 |
D, I = index.search(vec, top_k)
|
| 62 |
return "\n".join([docs[i] for i in I[0] if i < len(docs)])
|
| 63 |
|
| 64 |
-
# ✅
|
| 65 |
|
| 66 |
def generate_answer(user_input, context):
|
| 67 |
prompt = f"""
|
| 68 |
-
你是一位
|
| 69 |
|
| 70 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
{context}
|
| 72 |
|
| 73 |
[問題]
|
| 74 |
{user_input}
|
| 75 |
-
|
| 76 |
-
請根據資料,用一至兩句話自然回答問題。不要重複問題本身、不要說你是誰,回答務必明確,約 90 字內。
|
| 77 |
"""
|
| 78 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 79 |
outputs = model.generate(**inputs, max_new_tokens=150)
|
|
@@ -83,7 +109,7 @@ def generate_answer(user_input, context):
|
|
| 83 |
return line.strip()
|
| 84 |
return response[-90:]
|
| 85 |
|
| 86 |
-
# ✅
|
| 87 |
|
| 88 |
def answer(user_input):
|
| 89 |
direct = retrieve_qa_context(user_input)
|
|
@@ -93,13 +119,13 @@ def answer(user_input):
|
|
| 93 |
context = search_context_faiss(user_input)
|
| 94 |
return generate_answer(user_input, context)
|
| 95 |
|
| 96 |
-
# ✅
|
| 97 |
interface = gr.Interface(
|
| 98 |
fn=answer,
|
| 99 |
inputs=gr.Textbox(lines=2, placeholder="請輸入與南臺科技大學相關的問題..."),
|
| 100 |
outputs="text",
|
| 101 |
-
title="南臺科技大學 問答機器人(向量式 RAG 自動建構)",
|
| 102 |
-
description="
|
| 103 |
theme="default"
|
| 104 |
)
|
| 105 |
|
|
|
|
| 1 |
+
# ✅ app.py - 向量式 RAG + 強化 Few-shot Prompt(完整版)
|
| 2 |
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
|
| 11 |
+
# ✅ 檔案與模型設定
|
| 12 |
QA_FILE = "qa.json"
|
| 13 |
TEXT_FILE = "web_data.txt"
|
| 14 |
DOCS_FILE = "docs.json"
|
|
|
|
| 42 |
model = AutoModelForCausalLM.from_pretrained(GEN_MODEL, trust_remote_code=True).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 43 |
model.eval()
|
| 44 |
|
| 45 |
+
# ✅ QA 快速匹配
|
| 46 |
|
| 47 |
def retrieve_qa_context(user_input):
|
| 48 |
for item in qa_data:
|
|
|
|
| 54 |
return item["response"]
|
| 55 |
return None
|
| 56 |
|
| 57 |
+
# ✅ 向量檢索 top-k 段落
|
| 58 |
|
| 59 |
def search_context_faiss(user_input, top_k=3):
|
| 60 |
vec = embedder.encode([user_input])
|
| 61 |
D, I = index.search(vec, top_k)
|
| 62 |
return "\n".join([docs[i] for i in I[0] if i < len(docs)])
|
| 63 |
|
| 64 |
+
# ✅ 使用 Few-shot Prompt 生成答案
|
| 65 |
|
| 66 |
def generate_answer(user_input, context):
|
| 67 |
prompt = f"""
|
| 68 |
+
你是一位了解南臺科技大學的智慧語音助理。請根據以下資料回答問題,僅用一至兩句話,以繁體中文表達,回答需清楚具體,不重複問題,不加入身份說明。
|
| 69 |
|
| 70 |
+
[範例格式]
|
| 71 |
+
問題:學校地址在哪裡?
|
| 72 |
+
回答:南臺科技大學位於台南市永康區南台街一號。
|
| 73 |
+
|
| 74 |
+
問題:學校電話是多少?
|
| 75 |
+
回答:總機電話是 06-2533131,電機工程系分機為 3301。
|
| 76 |
+
|
| 77 |
+
問題:電機工程系辦公室在哪?
|
| 78 |
+
回答:電機工程系辦公室位於 B 棟 B101。
|
| 79 |
+
|
| 80 |
+
問題:電機工程系有哪些組別?
|
| 81 |
+
回答:電機系設有控制組、生醫電子系統組與電能資訊組三個方向。
|
| 82 |
+
|
| 83 |
+
問題:學生社團活動如何?
|
| 84 |
+
回答:南臺有超過 80 個學生社團,涵蓋學術、康樂、服務、體育與藝術領域。
|
| 85 |
+
|
| 86 |
+
問題:圖書館提供哪些服務?
|
| 87 |
+
回答:圖書館提供借書、自修空間、期刊查詢與電子資源服務。
|
| 88 |
+
|
| 89 |
+
問題:師資如何?
|
| 90 |
+
回答:本校師資陣容堅強,擁有 30 多位教授、副教授與助理教授。
|
| 91 |
+
|
| 92 |
+
問題:悠活館是做什麼的?
|
| 93 |
+
回答:悠活館是學生休閒與運動中心,設有羽球場、健身房、桌球室等設施。
|
| 94 |
+
|
| 95 |
+
問題:怎麼到南臺科技大學?
|
| 96 |
+
回答:可從台南火車站搭乘公車,或經永康交流道開車約 10 分鐘抵達。
|
| 97 |
+
|
| 98 |
+
[資料]
|
| 99 |
{context}
|
| 100 |
|
| 101 |
[問題]
|
| 102 |
{user_input}
|
|
|
|
|
|
|
| 103 |
"""
|
| 104 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 105 |
outputs = model.generate(**inputs, max_new_tokens=150)
|
|
|
|
| 109 |
return line.strip()
|
| 110 |
return response[-90:]
|
| 111 |
|
| 112 |
+
# ✅ 問答主流程
|
| 113 |
|
| 114 |
def answer(user_input):
|
| 115 |
direct = retrieve_qa_context(user_input)
|
|
|
|
| 119 |
context = search_context_faiss(user_input)
|
| 120 |
return generate_answer(user_input, context)
|
| 121 |
|
| 122 |
+
# ✅ Gradio 介面
|
| 123 |
interface = gr.Interface(
|
| 124 |
fn=answer,
|
| 125 |
inputs=gr.Textbox(lines=2, placeholder="請輸入與南臺科技大學相關的問題..."),
|
| 126 |
outputs="text",
|
| 127 |
+
title="南臺科技大學 問答機器人(向量式 RAG 自動建構 + Few-shot)",
|
| 128 |
+
description="支援 QA 關鍵字與語意檢索,自動建立嵌入庫,輸出繁體中文自然回答。",
|
| 129 |
theme="default"
|
| 130 |
)
|
| 131 |
|