Y Phung Nguyen commited on
Commit
8515412
·
1 Parent(s): 9099c51

Show final answer not error msg if any

Browse files
Files changed (1) hide show
  1. ui.py +50 -4
ui.py CHANGED
@@ -750,6 +750,7 @@ def create_demo():
750
  request = MockRequest()
751
 
752
  # Model is loaded, proceed with stream_chat (no model loading here to save time)
 
753
  try:
754
  for result in stream_chat(
755
  message, history, system_prompt, temperature, max_new_tokens,
@@ -757,15 +758,60 @@ def create_demo():
757
  use_rag, medical_model_name, use_web_search,
758
  enable_clinical_intake, disable_agentic_reasoning, show_thoughts, request
759
  ):
 
760
  yield result
761
  except Exception as e:
762
  # Handle any errors gracefully
763
- logger.error(f"Error in stream_chat_with_model_check: {e}")
 
 
 
 
764
  import traceback
765
  logger.debug(f"Full traceback: {traceback.format_exc()}")
766
- error_msg = f"⚠️ An error occurred: {str(e)[:200]}"
767
- updated_history = history + [{"role": "assistant", "content": error_msg}]
768
- yield updated_history, ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
769
 
770
  submit_button.click(
771
  fn=stream_chat_with_model_check,
 
750
  request = MockRequest()
751
 
752
  # Model is loaded, proceed with stream_chat (no model loading here to save time)
753
+ last_result = None
754
  try:
755
  for result in stream_chat(
756
  message, history, system_prompt, temperature, max_new_tokens,
 
758
  use_rag, medical_model_name, use_web_search,
759
  enable_clinical_intake, disable_agentic_reasoning, show_thoughts, request
760
  ):
761
+ last_result = result
762
  yield result
763
  except Exception as e:
764
  # Handle any errors gracefully
765
+ error_str = str(e)
766
+ error_msg_lower = error_str.lower()
767
+ is_gpu_timeout = 'gpu task aborted' in error_msg_lower or 'timeout' in error_msg_lower
768
+
769
+ logger.error(f"Error in stream_chat_with_model_check: {error_str}")
770
  import traceback
771
  logger.debug(f"Full traceback: {traceback.format_exc()}")
772
+
773
+ # Check if we have a valid answer in the last result
774
+ has_valid_answer = False
775
+ if last_result is not None:
776
+ try:
777
+ last_history, last_thoughts = last_result
778
+ # Find the last assistant message in the history
779
+ if last_history and isinstance(last_history, list):
780
+ for msg in reversed(last_history):
781
+ if isinstance(msg, dict) and msg.get("role") == "assistant":
782
+ assistant_content = msg.get("content", "")
783
+ # Check if it's a valid answer (not empty, not an error message)
784
+ if assistant_content and len(assistant_content.strip()) > 0:
785
+ # Not an error message
786
+ if not assistant_content.strip().startswith("⚠️") and not assistant_content.strip().startswith("⏳"):
787
+ has_valid_answer = True
788
+ break
789
+ except Exception as parse_error:
790
+ logger.debug(f"Error parsing last_result: {parse_error}")
791
+
792
+ # If we have a valid answer, use it (don't show error message)
793
+ if has_valid_answer:
794
+ logger.info(f"[UI] Error occurred but final answer already generated, displaying it without error message")
795
+ yield last_result
796
+ return
797
+
798
+ # For GPU timeouts, try to use last result even if it's partial
799
+ if is_gpu_timeout and last_result is not None:
800
+ logger.info(f"[UI] GPU timeout occurred, using last available result")
801
+ yield last_result
802
+ return
803
+
804
+ # Only show error for non-timeout errors when we have no valid answer
805
+ # For GPU timeouts with no result, show empty message (not error)
806
+ if is_gpu_timeout:
807
+ logger.info(f"[UI] GPU timeout with no result, showing empty assistant message")
808
+ updated_history = history + [{"role": "user", "content": message}, {"role": "assistant", "content": ""}]
809
+ yield updated_history, ""
810
+ else:
811
+ # For other errors, show minimal error message only if no result
812
+ error_display = f"⚠️ An error occurred: {error_str[:200]}"
813
+ updated_history = history + [{"role": "user", "content": message}, {"role": "assistant", "content": error_display}]
814
+ yield updated_history, ""
815
 
816
  submit_button.click(
817
  fn=stream_chat_with_model_check,