Luigi commited on
Commit
48e50b8
·
1 Parent(s): a73d8f4

Improve cancel generation responsiveness

Browse files

- Add additional cancel checks before each yield in the streaming loop
- Ensures generation stops immediately when cancel is triggered, preventing partial responses from being displayed
- Complements the StoppingCriteria to provide more robust cancellation

Files changed (1) hide show
  1. app.py +6 -0
app.py CHANGED
@@ -545,6 +545,8 @@ def chat_response(user_msg, chat_history, system_prompt,
545
  history.append({'role': 'assistant', 'content': answer_buf})
546
  else:
547
  history[-1]['content'] = thought_buf
 
 
548
  yield history, debug
549
  continue
550
 
@@ -560,6 +562,8 @@ def chat_response(user_msg, chat_history, system_prompt,
560
  history.append({'role': 'assistant', 'content': answer_buf})
561
  else:
562
  history[-1]['content'] = thought_buf
 
 
563
  yield history, debug
564
  continue
565
 
@@ -568,6 +572,8 @@ def chat_response(user_msg, chat_history, system_prompt,
568
  history.append({'role': 'assistant', 'content': ''})
569
  answer_buf += text
570
  history[-1]['content'] = answer_buf
 
 
571
  yield history, debug
572
 
573
  gen_thread.join()
 
545
  history.append({'role': 'assistant', 'content': answer_buf})
546
  else:
547
  history[-1]['content'] = thought_buf
548
+ if cancel_event.is_set():
549
+ break
550
  yield history, debug
551
  continue
552
 
 
562
  history.append({'role': 'assistant', 'content': answer_buf})
563
  else:
564
  history[-1]['content'] = thought_buf
565
+ if cancel_event.is_set():
566
+ break
567
  yield history, debug
568
  continue
569
 
 
572
  history.append({'role': 'assistant', 'content': ''})
573
  answer_buf += text
574
  history[-1]['content'] = answer_buf
575
+ if cancel_event.is_set():
576
+ break
577
  yield history, debug
578
 
579
  gen_thread.join()