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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -1,12 +1,16 @@
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 = [
@@ -32,7 +36,7 @@ qa_data = [
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
@@ -42,7 +46,7 @@ web_data = """
42
  [統計] 教授9人,副教授11人,助理教授9人,控制與晶片組最多教師。
43
  """
44
 
45
- # ✅ QA 匹配檢索
46
 
47
  def retrieve_qa_context(user_input):
48
  for item in qa_data:
@@ -54,7 +58,7 @@ def retrieve_qa_context(user_input):
54
  return item["response"]
55
  return None
56
 
57
- # ✅ 小模型生成自然語言
58
 
59
  def generate_answer_from_context(user_input, context):
60
  prompt = f"""
@@ -68,10 +72,12 @@ def generate_answer_from_context(user_input, context):
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)
@@ -80,15 +86,16 @@ def answer(user_input):
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
 
 
 
1
+ # ✅ Hugging Face + ChatGLM3 + 強化 QA 與資料庫版本
2
  # 檔名:app.py(可部署於 Hugging Face Spaces)
3
 
4
  import json
5
  import gradio as gr
6
+ from transformers import AutoTokenizer, AutoModelForCausalLM
7
+ import torch
8
 
9
+ # ✅ 載入 ChatGLM3 模型
10
+ model_name = "THUDM/chatglm3-6b"
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
12
+ model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True).half().to("cuda" if torch.cuda.is_available() else "cpu")
13
+ model.eval()
14
 
15
  # ✅ 強化 QA 資料庫(內嵌進程式中)
16
  qa_data = [
 
36
  }
37
  ]
38
 
39
+ # ✅ 本地知識資料庫
40
  web_data = """
41
  [校名] 南臺科技大學 Department of Electrical Engineering
42
  [地址] 71005 台南市永康區南台街一號 No.1, Nan-Tai St., Yungkang Dist., Tainan City 71005, Taiwan
 
46
  [統計] 教授9人,副教授11人,助理教授9人,控制與晶片組最多教師。
47
  """
48
 
49
+ # ✅ QA 檢索
50
 
51
  def retrieve_qa_context(user_input):
52
  for item in qa_data:
 
58
  return item["response"]
59
  return None
60
 
61
+ # ✅ 使用 ChatGLM3 生成人性化
62
 
63
  def generate_answer_from_context(user_input, context):
64
  prompt = f"""
 
72
 
73
  請用繁體中文簡短自然地回覆,不超過 90 字。
74
  """
75
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
76
+ outputs = model.generate(**inputs, max_new_tokens=128, do_sample=False)
77
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
78
+ return response.strip().split("回答:")[-1].strip()
79
 
80
+ # ✅ 流程
81
 
82
  def answer(user_input):
83
  context = retrieve_qa_context(user_input)
 
86
  else:
87
  return generate_answer_from_context(user_input, web_data)
88
 
89
+ # ✅ Gradio 介面
90
  interface = gr.Interface(
91
  fn=answer,
92
  inputs=gr.Textbox(lines=2, placeholder="請問有關南臺科技大學的問題..."),
93
  outputs="text",
94
+ title="南臺科技大學 問答機器人(ChatGLM3 強化知識庫)",
95
+ description="結合關鍵字 QA 與 ChatGLM3 模型能夠流暢回答與校內資訊有關的問題。",
96
  theme="default"
97
  )
98
 
99
  interface.launch()
100
 
101
+