svjack commited on
Commit
f215879
·
1 Parent(s): 17fdecf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+ import time
4
+
5
+ import chatglm_cpp
6
+ from pathlib import Path
7
+
8
+ model_file_path = "chatglm3-ggml_q4_0.bin"
9
+ #model_file_path = "../../Downloads1/chatglm3-ggml_q4_0.bin"
10
+ chatglm_llm = chatglm_cpp.Pipeline(Path(model_file_path))
11
+
12
+ def predict(message, history):
13
+ flatten_history = []
14
+ for a, b in history:
15
+ flatten_history.append(a)
16
+ flatten_history.append(b)
17
+
18
+ streamer = chatglm_llm.chat(
19
+ history= flatten_history + [message], do_sample=False,
20
+ stream = True
21
+ )
22
+
23
+ response = ""
24
+ for new_text in streamer:
25
+ response += new_text
26
+ yield response
27
+
28
+ with gr.Blocks(css = "custom.css") as demo:
29
+ title = gr.HTML(
30
+ """<h1 align="center"> <font size="+2"> ChatGLM3 Chatbot ☔️🐼 </font> </h1>""",
31
+ elem_id="title",
32
+ )
33
+
34
+ gr.HTML(
35
+ """<h1 align="left"> <font size="+0"> 与人工智能助手 ChatGLM3 进行对话 </font> </h1>""",
36
+ #elem_id="title",
37
+ )
38
+
39
+ chatbot = gr.Chatbot()
40
+
41
+ def user(user_message, history):
42
+ return "", history + [[user_message, None]]
43
+
44
+ def bot(history):
45
+ '''
46
+ bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
47
+ history[-1][1] = ""
48
+ for character in bot_message:
49
+ history[-1][1] += character
50
+ time.sleep(0.05)
51
+ yield history
52
+ '''
53
+ history[-1][1] = ""
54
+ user_message = history[-1][0]
55
+ pred_iter = predict(user_message ,history)
56
+ for ele in pred_iter:
57
+ history[-1][1] = ele
58
+ yield history
59
+
60
+ '''
61
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
62
+ bot, chatbot, chatbot
63
+ )
64
+ '''
65
+ with gr.Row():
66
+ msg = gr.Textbox(
67
+ #label="与人工智能助手 ChatGLM3 进行对话",
68
+ show_label=True, lines=1, max_lines=20,
69
+ min_width = 1024,
70
+ placeholder="你好 人工智能助手 ChatGLM3,我可以问你一些问题吗?",
71
+ elem_id="prompt",
72
+ interactive=True,
73
+ #info = "Generate by Click, and can edit by yourself, look up Examples below"
74
+ )
75
+ sub_buttom = gr.Button("Submit")
76
+ clear = gr.Button("Clear")
77
+
78
+ sub_buttom.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
79
+ bot, chatbot, chatbot
80
+ )
81
+
82
+ clear.click(lambda: None, None, chatbot, queue=False)
83
+
84
+ gr.Examples(
85
+ ["你听说过马克思吗?", "如何进行经济建设?", "明朝内阁制度的特点是什么?",
86
+ "请解释下面的emoji符号描述的情景👨👩🔥❄️",
87
+ ],
88
+ inputs = msg
89
+ )
90
+
91
+ demo.queue()
92
+ demo.launch()
93
+
94
+ '''
95
+ from gradio_client import Client
96
+ client = Client("http://localhost:7860/")
97
+
98
+ result = client.predict(
99
+ [["诸葛亮是哪个朝代的人?", "诸葛亮是三国时期的人。"],
100
+ ["为什么说明朝是一个好的时代?", "因为出了王阳明。"],
101
+ ["我之前问了哪些问题?", None]],
102
+ api_name="/bot"
103
+ )
104
+ print(result)
105
+ '''