Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
-
import torch
|
4 |
|
|
|
5 |
model_name = "thu-coai/CDial-GPT_LCCC-base"
|
6 |
-
|
7 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
|
9 |
system_prompt = (
|
10 |
"你是一位台灣公立高中總務處工作人員,使用繁體中文。你非常重視校園的職業安全衛生與防災安全議題,"
|
@@ -12,12 +11,10 @@ system_prompt = (
|
|
12 |
)
|
13 |
|
14 |
def reply_fn(chat_history, user_msg):
|
|
|
15 |
try:
|
16 |
-
|
17 |
-
|
18 |
-
outputs = model.generate(inputs, max_new_tokens=100, do_sample=True, temperature=0.7)
|
19 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
20 |
-
answer = response.split("你:")[-1].strip()
|
21 |
chat_history.append((user_msg, answer))
|
22 |
except Exception as e:
|
23 |
chat_history.append((user_msg, f"⚠️ 發生錯誤:{str(e)}"))
|
@@ -29,13 +26,12 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
29 |
歡迎使用!我是總務處的 AI 助理,任何問題我都會從 **職業安全衛生** 和 **校園防災** 的角度給你正確的建議 👷♂️🚒
|
30 |
""")
|
31 |
|
32 |
-
|
33 |
msg = gr.Textbox(placeholder="請輸入你的問題...", label="學生提問")
|
34 |
clear = gr.Button("清除對話")
|
35 |
-
|
36 |
state = gr.State([])
|
37 |
|
38 |
-
msg.submit(reply_fn, [state, msg], [
|
39 |
-
clear.click(lambda: ([], ""), None, [
|
40 |
|
41 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
|
|
3 |
|
4 |
+
# 模型與角色設定
|
5 |
model_name = "thu-coai/CDial-GPT_LCCC-base"
|
6 |
+
chatbot = pipeline("text-generation", model=model_name, tokenizer=model_name)
|
|
|
7 |
|
8 |
system_prompt = (
|
9 |
"你是一位台灣公立高中總務處工作人員,使用繁體中文。你非常重視校園的職業安全衛生與防災安全議題,"
|
|
|
11 |
)
|
12 |
|
13 |
def reply_fn(chat_history, user_msg):
|
14 |
+
prompt = system_prompt + ''.join([f"學生:{m[0]}\n你:{m[1]}\n" for m in chat_history]) + f"學生:{user_msg}\n你:"
|
15 |
try:
|
16 |
+
result = chatbot(prompt, max_length=300, do_sample=True, temperature=0.7)[0]["generated_text"]
|
17 |
+
answer = result.split("你:")[-1].strip()
|
|
|
|
|
|
|
18 |
chat_history.append((user_msg, answer))
|
19 |
except Exception as e:
|
20 |
chat_history.append((user_msg, f"⚠️ 發生錯誤:{str(e)}"))
|
|
|
26 |
歡迎使用!我是總務處的 AI 助理,任何問題我都會從 **職業安全衛生** 和 **校園防災** 的角度給你正確的建議 👷♂️🚒
|
27 |
""")
|
28 |
|
29 |
+
chatbot_ui = gr.Chatbot(show_copy_button=True)
|
30 |
msg = gr.Textbox(placeholder="請輸入你的問題...", label="學生提問")
|
31 |
clear = gr.Button("清除對話")
|
|
|
32 |
state = gr.State([])
|
33 |
|
34 |
+
msg.submit(reply_fn, [state, msg], [chatbot_ui, msg])
|
35 |
+
clear.click(lambda: ([], ""), None, [chatbot_ui, msg, state])
|
36 |
|
37 |
demo.launch()
|