Alexend commited on
Commit
b63958c
·
verified ·
1 Parent(s): 673ec90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -17
app.py CHANGED
@@ -1,22 +1,48 @@
1
- # ✅ Hugging Face + 小型模型 + QA.json + 本地爬蟲資料庫 fallback
2
- # 檔名:app.py(Hugging Face Spaces 專用
3
 
4
  import json
5
  import gradio as gr
6
  from transformers import pipeline
7
 
8
- # ✅ 小模型
9
  qa_model = pipeline("text2text-generation", model="google/flan-t5-base")
10
 
11
- # ✅ 載入 QA.json
12
- with open("qa.json", "r", encoding="utf-8") as f:
13
- qa_data = json.load(f)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # ✅ 載入爬下來的文字資料(取代即時爬蟲)
16
- with open("web_content.txt", "r", encoding="utf-8") as f:
17
- web_data = f.read()
 
 
 
 
 
 
18
 
19
- # ✅ 查詢 QA.json 資料
20
 
21
  def retrieve_qa_context(user_input):
22
  for item in qa_data:
@@ -28,31 +54,41 @@ def retrieve_qa_context(user_input):
28
  return item["response"]
29
  return None
30
 
31
- # ✅ GPT 模型生成人性化回答
32
 
33
  def generate_answer_from_context(user_input, context):
34
- prompt = f"根據以下資料,用繁體中文、口語化並簡潔地回答這個問題:\n\n[資料內容]\n{context}\n\n[問題]\n{user_input}\n\n回答:"
 
 
 
 
 
 
 
 
 
 
35
  result = qa_model(prompt, max_new_tokens=100)[0]['generated_text']
36
  return result.strip()
37
 
38
- # ✅ 主回答流程
39
 
40
  def answer(user_input):
41
  context = retrieve_qa_context(user_input)
42
  if context:
43
  return generate_answer_from_context(user_input, context)
44
  else:
45
- # fallback: 使用本地爬蟲資料庫 web_content.txt
46
  return generate_answer_from_context(user_input, web_data)
47
 
48
- # ✅ Gradio UI 介面
49
  interface = gr.Interface(
50
  fn=answer,
51
  inputs=gr.Textbox(lines=2, placeholder="請問有關南臺科技大學的問題..."),
52
  outputs="text",
53
- title="南臺科技大學 問答機器人 (小模型 + 本地知識)",
54
- description="先比對內建資料庫,用爬蟲資料生成答案。支援人性化口語。",
55
  theme="default"
56
  )
57
 
58
  interface.launch()
 
 
1
+ # ✅ Hugging Face + 小型模型 + 強化 QA 資料庫版本
2
+ # 檔名:app.py(可部署於 Hugging Face Spaces)
3
 
4
  import json
5
  import gradio as gr
6
  from transformers import pipeline
7
 
8
+ # ✅ 初始化小模型
9
  qa_model = pipeline("text2text-generation", model="google/flan-t5-base")
10
 
11
+ # ✅ 強化 QA 資料庫(內嵌進程式中)
12
+ qa_data = [
13
+ {
14
+ "keywords": ["你是誰"],
15
+ "match": "OR",
16
+ "response": "我是介紹南臺科技大學的 AI 語音助理,很高興為您服務!"
17
+ },
18
+ {
19
+ "keywords": ["地址", "位置", "在哪"],
20
+ "match": "OR",
21
+ "response": "南臺科技大學位於台南市永康區南台街一號,郵遞區號為71005。"
22
+ },
23
+ {
24
+ "keywords": ["電機系辦公室", "3301"],
25
+ "match": "OR",
26
+ "response": "電機工程系的辦公室位於B棟B101,聯絡電話為06-2533131 分機3301。"
27
+ },
28
+ {
29
+ "keywords": ["悠活館", "U棟"],
30
+ "match": "OR",
31
+ "response": "悠活館(U棟)內有桌球場、健身房與羽球場,是學生運動與休閒的主要場域。"
32
+ }
33
+ ]
34
 
35
+ # ✅ 強化版本地知識資料模擬爬蟲結果
36
+ web_data = """
37
+ [校名] 南臺科技大學 Department of Electrical Engineering
38
+ [地址] 71005 台南市永康區南台街一號 No.1, Nan-Tai St., Yungkang Dist., Tainan City 71005, Taiwan
39
+ [辦公室] B棟B101, TEL:06-2533131 #3301
40
+ [老師資訊] 陳有圳、陳世中、陳彥廷、楊弘吉、黃基哲、侯春茹、洪千焙
41
+ [群組] 控制組、電能資訊組、生醫電子系統組
42
+ [統計] 教授9人,副教授11人,助理教授9人,控制與晶片組最多教師。
43
+ """
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
+ # ✅ 模型生成自然語言回答
58
 
59
  def generate_answer_from_context(user_input, context):
60
+ prompt = f"""
61
+ 你是一位了解南臺科技大學的親切語音助理。請根據以下資料回答使用者的問題:
62
+
63
+ [資料內容]
64
+ {context}
65
+
66
+ [問題]
67
+ {user_input}
68
+
69
+ 請用繁體中文簡短自然地回覆,不超過 90 字。
70
+ """
71
  result = qa_model(prompt, max_new_tokens=100)[0]['generated_text']
72
  return result.strip()
73
 
74
+ # ✅ 整合流程
75
 
76
  def answer(user_input):
77
  context = retrieve_qa_context(user_input)
78
  if context:
79
  return generate_answer_from_context(user_input, context)
80
  else:
 
81
  return generate_answer_from_context(user_input, web_data)
82
 
83
+ # ✅ Gradio UI
84
  interface = gr.Interface(
85
  fn=answer,
86
  inputs=gr.Textbox(lines=2, placeholder="請問有關南臺科技大學的問題..."),
87
  outputs="text",
88
+ title="南臺科技大學 問答機器人(強化 QA + 本地資料",
89
+ description="使用 QA 關鍵字資料庫,若找不到則使校內爬蟲資料生成人性化回。",
90
  theme="default"
91
  )
92
 
93
  interface.launch()
94
+