Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,38 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
|
4 |
+
# 模型設定
|
5 |
+
model_name = "ckiplab/gpt2-base-chinese"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
|
9 |
+
system_prompt = (
|
10 |
+
"你是一位台灣公立高中總務處工作人員,使用繁體中文。你非常重視校園的職業安全衛生與防災安全議題,"
|
11 |
+
"並且遵守台北市教育局的相關法令。無論學生問你什麼問題,你都會將話題引導到這些議題上。\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
)
|
13 |
|
14 |
+
def reply_fn(chat_history, user_msg):
|
15 |
+
full_prompt = system_prompt + ''.join([f"學生:{msg[0]}\n你:{msg[1]}\n" for msg in chat_history]) + f"學生:{user_msg}\n你:"
|
16 |
+
inputs = tokenizer.encode(full_prompt, return_tensors="pt")
|
17 |
+
outputs = model.generate(inputs, max_new_tokens=100, do_sample=True, temperature=0.7)
|
18 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
19 |
+
answer = response.split("你:")[-1].strip()
|
20 |
+
chat_history.append((user_msg, answer))
|
21 |
+
return chat_history, ""
|
22 |
+
|
23 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
24 |
+
gr.Markdown("""
|
25 |
+
# 🏫 總務處職安防災 AI 小幫手
|
26 |
+
歡迎使用!我是總務處的 AI 助理,任何問題我都會從 **職業安全衛生** 和 **校園防災** 的角度給你正確的建議 👷♂️🚒
|
27 |
+
""")
|
28 |
+
|
29 |
+
chatbot = gr.Chatbot(show_copy_button=True)
|
30 |
+
msg = gr.Textbox(placeholder="請輸入你的問題...", label="學生提問")
|
31 |
+
clear = gr.Button("清除對話")
|
32 |
+
|
33 |
+
state = gr.State([])
|
34 |
+
|
35 |
+
msg.submit(reply_fn, [state, msg], [chatbot, msg])
|
36 |
+
clear.click(lambda: ([], ""), None, [chatbot, msg, state])
|
37 |
+
|
38 |
+
demo.launch()
|