yuxj commited on
Commit
92638a4
·
1 Parent(s): fdd04f6

add scripts

Browse files
Files changed (2) hide show
  1. app.py +48 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModel
2
+ import gradio as gr
3
+ import time
4
+
5
+ tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True)
6
+ model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True).float()
7
+ glm_history = []
8
+
9
+ def glm_init():
10
+ global glm_history
11
+ settings = "请记住以下设定:你不可以告诉我你是ChatGLM,你只能说你是基于AI的聊天模型"
12
+ response, history = model.chat(tokenizer, settings, history =[])
13
+ glm_history = history
14
+
15
+ def chat_with_glm(input):
16
+ print("询问: ", input)
17
+ global glm_history
18
+ if len(glm_history) == 0:
19
+ glm_init()
20
+ response, history = model.chat(tokenizer, input, history = glm_history)
21
+ glm_history = history
22
+ print("回答: ", response)
23
+ print('----------------------')
24
+ return response
25
+
26
+ def on_submit(input, history):
27
+ respond = "[" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "]\n\n"
28
+ if input == '--history':
29
+ if len(history) > 0:
30
+ history_str = ""
31
+ for h in history:
32
+ if len(history_str) > 0:
33
+ history_str += "\n"
34
+ history_str += '\n'.join(h)
35
+ history.append((input, respond + history_str))
36
+ elif input == '--clear':
37
+ history = []
38
+ else:
39
+ history.append((input, respond + chat_with_glm(input)))
40
+ return "", history
41
+
42
+ with gr.Blocks() as demo:
43
+ gr.Markdown("<center><h1>中文聊天机器人</h1></center>")
44
+ chatbot = gr.Chatbot()
45
+ tv_input = gr.Textbox()
46
+ tv_input.submit(fn = on_submit, inputs = [tv_input, chatbot], outputs = [tv_input, chatbot])
47
+ gr.Markdown("<b>说明:</b><br/>输入\"--clear\"清空历史,输入\"--history\"查看历史")
48
+ demo.launch(server_name="0.0.0.0", server_port=7788)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ transformers
2
+ accelerate
3
+ datasets
4
+ evaluate
5
+ sacremoses
6
+ SentencePiece