ysharma HF staff commited on
Commit
e3ea0a6
1 Parent(s): 9fe4d8e

added examples, duplicate button, more

Browse files

regenerate and delete last turn buttons added

Files changed (1) hide show
  1. app.py +71 -3
app.py CHANGED
@@ -86,7 +86,7 @@ def parse_text(text):
86
  return text
87
 
88
 
89
- def predict(input, chatbot, max_length, top_p, temperature, history, past_key_values):
90
  chatbot.append((parse_text(input), ""))
91
  for response, history, past_key_values in model.stream_chat(tokenizer, input, history, past_key_values=past_key_values,
92
  return_past_key_values=True,
@@ -130,8 +130,52 @@ def reset_state():
130
  return [], [], None
131
 
132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
134
  gr.HTML("""<h1 align="center">ChatGLM2-6B-int4</h1>""")
 
 
135
  with gr.Accordion("Info", open=False):
136
  _ = """
137
  A query takes from 30 seconds to a few tens of seconds, dependent on the number of words/characters
@@ -156,8 +200,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
156
  with gr.Column(scale=12):
157
  user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(
158
  container=False)
 
159
  with gr.Column(min_width=32, scale=1):
160
  submitBtn = gr.Button("Submit", variant="primary")
 
 
161
  with gr.Column(scale=1):
162
  emptyBtn = gr.Button("Clear History")
163
  max_length = gr.Slider(0, 32768, value=8192/2, step=1.0, label="Maximum length", interactive=True)
@@ -167,14 +214,22 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
167
  history = gr.State([])
168
  past_key_values = gr.State(None)
169
 
170
- user_input.submit(predict, [user_input, chatbot, max_length, top_p, temperature, history, past_key_values],
171
  [chatbot, history, past_key_values], show_progress=True)
172
- submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history, past_key_values],
173
  [chatbot, history, past_key_values], show_progress=True, api_name="predict")
174
  submitBtn.click(reset_user_input, [], [user_input])
175
 
176
  emptyBtn.click(reset_state, outputs=[chatbot, history, past_key_values], show_progress=True)
177
 
 
 
 
 
 
 
 
 
178
  with gr.Accordion("For Translation API", open=False):
179
  input_text = gr.Text()
180
  tr_btn = gr.Button("Go", variant="primary")
@@ -182,6 +237,19 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
182
  tr_btn.click(trans_api, [input_text, max_length, top_p, temperature], out_text, show_progress=True, api_name="tr")
183
  input_text.submit(trans_api, [input_text, max_length, top_p, temperature], out_text, show_progress=True, api_name="tr")
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  # demo.queue().launch(share=False, inbrowser=True)
186
  # demo.queue().launch(share=True, inbrowser=True, debug=True)
187
 
 
86
  return text
87
 
88
 
89
+ def predict(RETRY_FLAG, input, chatbot, max_length, top_p, temperature, history, past_key_values):
90
  chatbot.append((parse_text(input), ""))
91
  for response, history, past_key_values in model.stream_chat(tokenizer, input, history, past_key_values=past_key_values,
92
  return_past_key_values=True,
 
130
  return [], [], None
131
 
132
 
133
+ # Delete last turn
134
+ def delete_last_turn(chat, history):
135
+ if chat and history:
136
+ chat.pop(-1)
137
+ history.pop(-1)
138
+ history.pop(-1)
139
+ return chat, history
140
+
141
+
142
+ # Regenerate response
143
+ def retry_last_answer(
144
+ user_input,
145
+ chatbot,
146
+ max_length,
147
+ top_p,
148
+ temperature,
149
+ history,
150
+ past_key_values
151
+ ):
152
+
153
+ if chat and history:
154
+ # Removing the previous conversation from chat
155
+ chat.pop(-1)
156
+ # Removing bot response from the history
157
+ history.pop(-1)
158
+ # Setting up a flag to capture a retry
159
+ RETRY_FLAG = True
160
+ # Getting last message from user
161
+ user_message = history[-1]
162
+
163
+ yield from predict(
164
+ RETRY_FLAG,
165
+ user_input,
166
+ chatbot,
167
+ max_length,
168
+ top_p,
169
+ temperature,
170
+ history,
171
+ past_key_values
172
+ )
173
+
174
+
175
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
176
  gr.HTML("""<h1 align="center">ChatGLM2-6B-int4</h1>""")
177
+ gr.HTML("""<center><a href="https://huggingface.co/spaces/ysharma/chatglm2-6b-4bit?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>To avoid the queue and for faster inference Duplicate this Space and upgrade to GPU</center>""")
178
+
179
  with gr.Accordion("Info", open=False):
180
  _ = """
181
  A query takes from 30 seconds to a few tens of seconds, dependent on the number of words/characters
 
200
  with gr.Column(scale=12):
201
  user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(
202
  container=False)
203
+ RETRY_FLAG = gr.Checkbox(value=False, visible=False)
204
  with gr.Column(min_width=32, scale=1):
205
  submitBtn = gr.Button("Submit", variant="primary")
206
+ deleteBtn = gr.Button("Delete last turn", variant="secondary")
207
+ retryBtn = gr.Button("Regenerate", variant="secondary")
208
  with gr.Column(scale=1):
209
  emptyBtn = gr.Button("Clear History")
210
  max_length = gr.Slider(0, 32768, value=8192/2, step=1.0, label="Maximum length", interactive=True)
 
214
  history = gr.State([])
215
  past_key_values = gr.State(None)
216
 
217
+ user_input.submit(predict, [RETRY_FLAG, user_input, chatbot, max_length, top_p, temperature, history, past_key_values],
218
  [chatbot, history, past_key_values], show_progress=True)
219
+ submitBtn.click(predict, [RETRY_FLAG, user_input, chatbot, max_length, top_p, temperature, history, past_key_values],
220
  [chatbot, history, past_key_values], show_progress=True, api_name="predict")
221
  submitBtn.click(reset_user_input, [], [user_input])
222
 
223
  emptyBtn.click(reset_state, outputs=[chatbot, history, past_key_values], show_progress=True)
224
 
225
+ regenerate_button.click(
226
+ retry_last_answer,
227
+ inputs = [user_input, chatbot, max_length, top_p, temperature, history, past_key_values],
228
+ #outputs = [chatbot, history, last_user_message, user_message]
229
+ outputs=[chatbot, history, past_key_values]
230
+ )
231
+ delete_turn_button.click(delete_last_turn, [chatbot, history], [chatbot, history])
232
+
233
  with gr.Accordion("For Translation API", open=False):
234
  input_text = gr.Text()
235
  tr_btn = gr.Button("Go", variant="primary")
 
237
  tr_btn.click(trans_api, [input_text, max_length, top_p, temperature], out_text, show_progress=True, api_name="tr")
238
  input_text.submit(trans_api, [input_text, max_length, top_p, temperature], out_text, show_progress=True, api_name="tr")
239
 
240
+ with gr.Accordion("Example inputs", open=True):
241
+ examples = gr.Examples(
242
+ examples=[["Explain the plot of Cinderella in a sentence."],
243
+ ["How long does it take to become proficient in French, and what are the best methods for retaining information?"],
244
+ ["What are some common mistakes to avoid when writing code?"],
245
+ ["Build a prompt to generate a beautiful portrait of a horse"],
246
+ ["Suggest four metaphors to describe the benefits of AI"],
247
+ ["Write a pop song about leaving home for the sandy beaches."],
248
+ ["Write a summary demonstrating my ability to do beat-boxing"]],
249
+ inputs = [user_input],
250
+
251
+ )
252
+
253
  # demo.queue().launch(share=False, inbrowser=True)
254
  # demo.queue().launch(share=True, inbrowser=True, debug=True)
255