Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
3 |
|
4 |
# 模型設定(繁體中文 GPT2)
|
5 |
model_name = "ckiplab/gpt2-base-chinese"
|
@@ -12,12 +13,15 @@ system_prompt = (
|
|
12 |
)
|
13 |
|
14 |
def reply_fn(chat_history, user_msg):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
return chat_history, ""
|
22 |
|
23 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
@@ -25,7 +29,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
25 |
# 🏫 總務處職安防災 AI 小幫手
|
26 |
歡迎使用!我是總務處的 AI 助理,任何問題我都會從 **職業安全衛生** 和 **校園防災** 的角度給你正確的建議 👷♂️🚒
|
27 |
""")
|
28 |
-
|
29 |
chatbot = gr.Chatbot(show_copy_button=True)
|
30 |
msg = gr.Textbox(placeholder="請輸入你的問題...", label="學生提問")
|
31 |
clear = gr.Button("清除對話")
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
|
5 |
# 模型設定(繁體中文 GPT2)
|
6 |
model_name = "ckiplab/gpt2-base-chinese"
|
|
|
13 |
)
|
14 |
|
15 |
def reply_fn(chat_history, user_msg):
|
16 |
+
try:
|
17 |
+
full_prompt = system_prompt + ''.join([f"學生:{msg[0]}\n你:{msg[1]}\n" for msg in chat_history]) + f"學生:{user_msg}\n你:"
|
18 |
+
inputs = tokenizer.encode(full_prompt, return_tensors="pt", truncation=True, max_length=512)
|
19 |
+
outputs = model.generate(inputs, max_new_tokens=100, do_sample=True, temperature=0.7)
|
20 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
21 |
+
answer = response.split("你:")[-1].strip()
|
22 |
+
chat_history.append((user_msg, answer))
|
23 |
+
except Exception as e:
|
24 |
+
chat_history.append((user_msg, f"⚠️ 發生錯誤:{str(e)}"))
|
25 |
return chat_history, ""
|
26 |
|
27 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
|
29 |
# 🏫 總務處職安防災 AI 小幫手
|
30 |
歡迎使用!我是總務處的 AI 助理,任何問題我都會從 **職業安全衛生** 和 **校園防災** 的角度給你正確的建議 👷♂️🚒
|
31 |
""")
|
32 |
+
|
33 |
chatbot = gr.Chatbot(show_copy_button=True)
|
34 |
msg = gr.Textbox(placeholder="請輸入你的問題...", label="學生提問")
|
35 |
clear = gr.Button("清除對話")
|