Corvius commited on
Commit
e1d0627
β€’
1 Parent(s): cb16718
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -135,7 +135,7 @@ def export_chat(history, system_prompt):
135
 
136
  def sanitize_chatbot_history(history):
137
  """Ensure each entry in the chatbot history is a tuple of two items."""
138
- return [tuple(entry[:2]) if isinstance(entry, (list, tuple)) else (str(entry), None) for entry in history]
139
 
140
  with gr.Blocks(theme='gradio/monochrome') as demo:
141
  with gr.Row():
@@ -174,21 +174,17 @@ with gr.Blocks(theme='gradio/monochrome') as demo:
174
  return
175
  user_message = history[-1][0]
176
  bot_message = predict(user_message, history[:-1], system_prompt, temperature, top_p, top_k, frequency_penalty, presence_penalty, repetition_penalty, max_tokens)
177
- history[-1] = (history[-1][0], "")
178
  task_id = id(asyncio.current_task())
179
  active_tasks[task_id] = asyncio.current_task()
180
  try:
181
  async for chunk in bot_message:
182
  if task_id not in active_tasks:
183
  break
184
- history[-1] = (history[-1][0], chunk)
185
  yield history
186
  except asyncio.CancelledError:
187
- print("Bot generation cancelled")
188
- except GeneratorExit:
189
- print("Generator exited")
190
- except Exception as e:
191
- print(f"Error in bot generation: {e}")
192
  finally:
193
  if task_id in active_tasks:
194
  del active_tasks[task_id]
@@ -197,16 +193,18 @@ with gr.Blocks(theme='gradio/monochrome') as demo:
197
  yield history
198
 
199
  async def regenerate_response(history, system_prompt, temperature, top_p, top_k, frequency_penalty, presence_penalty, repetition_penalty, max_tokens):
 
200
  for task in list(active_tasks.values()):
201
  task.cancel()
202
 
 
203
  await asyncio.sleep(0.1)
204
 
205
  history = sanitize_chatbot_history(history or [])
206
  if history:
207
- history[-1] = (history[-1][0], None)
208
  async for new_history in bot(history, system_prompt, temperature, top_p, top_k, frequency_penalty, presence_penalty, repetition_penalty, max_tokens):
209
- yield sanitize_chatbot_history(new_history)
210
  else:
211
  yield []
212
 
@@ -216,7 +214,7 @@ with gr.Blocks(theme='gradio/monochrome') as demo:
216
 
217
  submit_event = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
218
  bot, [chatbot, system_prompt, temperature, top_p, top_k, frequency_penalty, presence_penalty, repetition_penalty, max_tokens], chatbot,
219
- concurrency_limit=10
220
  )
221
 
222
  clear.click(lambda: [], None, chatbot, queue=False)
@@ -225,16 +223,16 @@ with gr.Blocks(theme='gradio/monochrome') as demo:
225
  regenerate_response,
226
  [chatbot, system_prompt, temperature, top_p, top_k, frequency_penalty, presence_penalty, repetition_penalty, max_tokens],
227
  chatbot,
228
- concurrency_limit=10
229
  )
230
 
231
- import_button.click(import_chat_wrapper, inputs=[import_textbox], outputs=[chatbot, system_prompt], concurrency_limit=10)
232
 
233
  export_button.click(
234
  export_chat,
235
  inputs=[chatbot, system_prompt],
236
  outputs=[import_textbox],
237
- concurrency_limit=10
238
  )
239
 
240
  stop_btn.click(
@@ -246,4 +244,4 @@ with gr.Blocks(theme='gradio/monochrome') as demo:
246
  )
247
 
248
  if __name__ == "__main__":
249
- demo.launch(debug=True, max_threads=40)
 
135
 
136
  def sanitize_chatbot_history(history):
137
  """Ensure each entry in the chatbot history is a tuple of two items."""
138
+ return [tuple(entry[:2]) for entry in history]
139
 
140
  with gr.Blocks(theme='gradio/monochrome') as demo:
141
  with gr.Row():
 
174
  return
175
  user_message = history[-1][0]
176
  bot_message = predict(user_message, history[:-1], system_prompt, temperature, top_p, top_k, frequency_penalty, presence_penalty, repetition_penalty, max_tokens)
177
+ history[-1] = (history[-1][0], "") # Ensure it's a tuple
178
  task_id = id(asyncio.current_task())
179
  active_tasks[task_id] = asyncio.current_task()
180
  try:
181
  async for chunk in bot_message:
182
  if task_id not in active_tasks:
183
  break
184
+ history[-1] = (history[-1][0], chunk) # Update as a tuple
185
  yield history
186
  except asyncio.CancelledError:
187
+ pass
 
 
 
 
188
  finally:
189
  if task_id in active_tasks:
190
  del active_tasks[task_id]
 
193
  yield history
194
 
195
  async def regenerate_response(history, system_prompt, temperature, top_p, top_k, frequency_penalty, presence_penalty, repetition_penalty, max_tokens):
196
+ # Cancel any ongoing generation
197
  for task in list(active_tasks.values()):
198
  task.cancel()
199
 
200
+ # Wait for a short time to ensure cancellation is processed
201
  await asyncio.sleep(0.1)
202
 
203
  history = sanitize_chatbot_history(history or [])
204
  if history:
205
+ history[-1] = (history[-1][0], None) # Reset last response
206
  async for new_history in bot(history, system_prompt, temperature, top_p, top_k, frequency_penalty, presence_penalty, repetition_penalty, max_tokens):
207
+ yield new_history
208
  else:
209
  yield []
210
 
 
214
 
215
  submit_event = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
216
  bot, [chatbot, system_prompt, temperature, top_p, top_k, frequency_penalty, presence_penalty, repetition_penalty, max_tokens], chatbot,
217
+ concurrency_limit=5
218
  )
219
 
220
  clear.click(lambda: [], None, chatbot, queue=False)
 
223
  regenerate_response,
224
  [chatbot, system_prompt, temperature, top_p, top_k, frequency_penalty, presence_penalty, repetition_penalty, max_tokens],
225
  chatbot,
226
+ concurrency_limit=5
227
  )
228
 
229
+ import_button.click(import_chat_wrapper, inputs=[import_textbox], outputs=[chatbot, system_prompt], concurrency_limit=5)
230
 
231
  export_button.click(
232
  export_chat,
233
  inputs=[chatbot, system_prompt],
234
  outputs=[import_textbox],
235
+ concurrency_limit=5
236
  )
237
 
238
  stop_btn.click(
 
244
  )
245
 
246
  if __name__ == "__main__":
247
+ demo.launch(debug=True, max_threads=20)