Quality of life updates

#1
by ysharma HF staff - opened
Files changed (1) hide show
  1. app.py +68 -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,
@@ -129,9 +129,50 @@ def reset_user_input():
129
  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
@@ -154,10 +195,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
154
  with gr.Row():
155
  with gr.Column(scale=4):
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)
@@ -175,6 +220,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
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 +235,18 @@ 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,
 
129
  def reset_state():
130
  return [], [], None
131
 
132
+ # Delete last turn
133
+ def delete_last_turn(chat, history):
134
+ if chat and history:
135
+ chat.pop(-1)
136
+ history.pop(-1)
137
+ return chat, history
138
+
139
+
140
+ # Regenerate response
141
+ def retry_last_answer(
142
+ user_input,
143
+ chatbot,
144
+ max_length,
145
+ top_p,
146
+ temperature,
147
+ history,
148
+ past_key_values
149
+ ):
150
+
151
+ if chatbot and history:
152
+ # Removing the previous conversation from chat
153
+ chatbot.pop(-1)
154
+ # Setting up a flag to capture a retry
155
+ RETRY_FLAG = True
156
+ # Getting last message from user
157
+ user_input = history[-1][0]
158
+ # Removing bot response from the history
159
+ history.pop(-1)
160
+
161
+ yield from predict(
162
+ RETRY_FLAG,
163
+ user_input,
164
+ chatbot,
165
+ max_length,
166
+ top_p,
167
+ temperature,
168
+ history,
169
+ past_key_values
170
+ )
171
 
172
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
173
  gr.HTML("""<h1 align="center">ChatGLM2-6B-int4</h1>""")
174
+ gr.HTML("""<center><a href="https://huggingface.co/spaces/mikeee/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>""")
175
+
176
  with gr.Accordion("Info", open=False):
177
  _ = """
178
  A query takes from 30 seconds to a few tens of seconds, dependent on the number of words/characters
 
195
  with gr.Row():
196
  with gr.Column(scale=4):
197
  with gr.Column(scale=12):
198
+ user_input = gr.Textbox(show_label=False, placeholder="Input...", ).style(
199
  container=False)
200
+ RETRY_FLAG = gr.Checkbox(value=False, visible=False)
201
  with gr.Column(min_width=32, scale=1):
202
+ with gr.Row():
203
+ submitBtn = gr.Button("Submit", variant="primary")
204
+ deleteBtn = gr.Button("Delete last turn", variant="secondary")
205
+ retryBtn = gr.Button("Regenerate", variant="secondary")
206
  with gr.Column(scale=1):
207
  emptyBtn = gr.Button("Clear History")
208
  max_length = gr.Slider(0, 32768, value=8192/2, step=1.0, label="Maximum length", interactive=True)
 
220
 
221
  emptyBtn.click(reset_state, outputs=[chatbot, history, past_key_values], show_progress=True)
222
 
223
+ retryBtn.click(
224
+ retry_last_answer,
225
+ inputs = [user_input, chatbot, max_length, top_p, temperature, history, past_key_values],
226
+ #outputs = [chatbot, history, last_user_message, user_message]
227
+ outputs=[chatbot, history, past_key_values]
228
+ )
229
+ deleteBtn.click(delete_last_turn, [chatbot, history], [chatbot, history])
230
+
231
  with gr.Accordion("For Translation API", open=False):
232
  input_text = gr.Text()
233
  tr_btn = gr.Button("Go", variant="primary")
 
235
  tr_btn.click(trans_api, [input_text, max_length, top_p, temperature], out_text, show_progress=True, api_name="tr")
236
  input_text.submit(trans_api, [input_text, max_length, top_p, temperature], out_text, show_progress=True, api_name="tr")
237
 
238
+ with gr.Accordion("Example inputs", open=True):
239
+ examples = gr.Examples(
240
+ examples=[["Explain the plot of Cinderella in a sentence."],
241
+ ["How long does it take to become proficient in French, and what are the best methods for retaining information?"],
242
+ ["What are some common mistakes to avoid when writing code?"],
243
+ ["Build a prompt to generate a beautiful portrait of a horse"],
244
+ ["Suggest four metaphors to describe the benefits of AI"],
245
+ ["Write a pop song about leaving home for the sandy beaches."],
246
+ ["Write a summary demonstrating my ability to tame lions"]],
247
+ inputs = [user_input],
248
+
249
+ )
250
  # demo.queue().launch(share=False, inbrowser=True)
251
  # demo.queue().launch(share=True, inbrowser=True, debug=True)
252