Commit
·
6a19924
1
Parent(s):
e8b97b2
Update app.py
Browse files
app.py
CHANGED
@@ -70,13 +70,15 @@ def parse_text(text):
|
|
70 |
return text
|
71 |
|
72 |
|
73 |
-
def predict(input, chatbot, max_length, top_p, temperature, history):
|
74 |
chatbot.append((parse_text(input), ""))
|
75 |
-
for response, history in model.stream_chat(tokenizer, input, history,
|
76 |
-
|
77 |
-
|
|
|
|
|
78 |
|
79 |
-
yield chatbot, history
|
80 |
|
81 |
|
82 |
def reset_user_input():
|
@@ -84,31 +86,33 @@ def reset_user_input():
|
|
84 |
|
85 |
|
86 |
def reset_state():
|
87 |
-
return [], []
|
88 |
|
89 |
|
90 |
with gr.Blocks() as demo:
|
91 |
-
gr.HTML("""<h1 align="center">
|
92 |
|
93 |
chatbot = gr.Chatbot()
|
94 |
with gr.Row():
|
95 |
with gr.Column(scale=4):
|
96 |
with gr.Column(scale=12):
|
97 |
-
user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10)
|
|
|
98 |
with gr.Column(min_width=32, scale=1):
|
99 |
submitBtn = gr.Button("Submit", variant="primary")
|
100 |
with gr.Column(scale=1):
|
101 |
emptyBtn = gr.Button("Clear History")
|
102 |
-
max_length = gr.Slider(0,
|
103 |
-
top_p = gr.Slider(0, 1, value=0.
|
104 |
temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)
|
105 |
|
106 |
history = gr.State([])
|
|
|
107 |
|
108 |
-
submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history
|
109 |
-
show_progress=True)
|
110 |
submitBtn.click(reset_user_input, [], [user_input])
|
111 |
|
112 |
-
emptyBtn.click(reset_state, outputs=[chatbot, history], show_progress=True)
|
113 |
|
114 |
-
demo.launch(show_error=True)
|
|
|
70 |
return text
|
71 |
|
72 |
|
73 |
+
def predict(input, chatbot, max_length, top_p, temperature, history, past_key_values):
|
74 |
chatbot.append((parse_text(input), ""))
|
75 |
+
for response, history, past_key_values in model.stream_chat(tokenizer, input, history, past_key_values=past_key_values,
|
76 |
+
return_past_key_values=True,
|
77 |
+
max_length=max_length, top_p=top_p,
|
78 |
+
temperature=temperature):
|
79 |
+
chatbot[-1] = (parse_text(input), parse_text(response))
|
80 |
|
81 |
+
yield chatbot, history, past_key_values
|
82 |
|
83 |
|
84 |
def reset_user_input():
|
|
|
86 |
|
87 |
|
88 |
def reset_state():
|
89 |
+
return [], [], None
|
90 |
|
91 |
|
92 |
with gr.Blocks() as demo:
|
93 |
+
gr.HTML("""<h1 align="center">ChatGLM2-6B</h1>""")
|
94 |
|
95 |
chatbot = gr.Chatbot()
|
96 |
with gr.Row():
|
97 |
with gr.Column(scale=4):
|
98 |
with gr.Column(scale=12):
|
99 |
+
user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(
|
100 |
+
container=False)
|
101 |
with gr.Column(min_width=32, scale=1):
|
102 |
submitBtn = gr.Button("Submit", variant="primary")
|
103 |
with gr.Column(scale=1):
|
104 |
emptyBtn = gr.Button("Clear History")
|
105 |
+
max_length = gr.Slider(0, 32768, value=8192, step=1.0, label="Maximum length", interactive=True)
|
106 |
+
top_p = gr.Slider(0, 1, value=0.8, step=0.01, label="Top P", interactive=True)
|
107 |
temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)
|
108 |
|
109 |
history = gr.State([])
|
110 |
+
past_key_values = gr.State(None)
|
111 |
|
112 |
+
submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history, past_key_values],
|
113 |
+
[chatbot, history, past_key_values], show_progress=True)
|
114 |
submitBtn.click(reset_user_input, [], [user_input])
|
115 |
|
116 |
+
emptyBtn.click(reset_state, outputs=[chatbot, history, past_key_values], show_progress=True)
|
117 |
|
118 |
+
demo.launch(show_error=True)
|