Ali2206 commited on
Commit
13fb959
·
verified ·
1 Parent(s): 2e7974d

Update ui/ui_core.py

Browse files
Files changed (1) hide show
  1. ui/ui_core.py +20 -19
ui/ui_core.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import pandas as pd
4
  import pdfplumber
5
  import gradio as gr
 
6
  from typing import List
7
 
8
  # ✅ Fix: Add src to Python path
@@ -13,6 +14,9 @@ from txagent.txagent import TxAgent
13
  def sanitize_utf8(text: str) -> str:
14
  return text.encode("utf-8", "ignore").decode("utf-8")
15
 
 
 
 
16
  def extract_all_text_from_csv_or_excel(file_path: str, progress=None, index=0, total=1) -> str:
17
  try:
18
  if not os.path.exists(file_path):
@@ -95,18 +99,14 @@ def create_ui(agent: TxAgent):
95
  "You are an expert clinical AI assistant reviewing medical form or interview data. "
96
  "Your job is to analyze this data and reason about any information or red flags that a human doctor might have overlooked. "
97
  "Provide a **detailed and structured response**, including examples, supporting evidence from the form, and clinical rationale for why these items matter. "
 
98
  "Do not hallucinate. Base the response only on the provided form content. "
99
  "End with a section labeled '🧠 Final Analysis' where you summarize key findings the doctor may have missed."
100
  )
101
 
102
- # Show centered loading message in chatbot
103
- updated_history = history + [
104
- {"role": "user", "content": message},
105
- {"role": "assistant", "content": "<div style='text-align:center'>⏳ Processing... Please wait while I analyze the files.</div>"}
106
- ]
107
- yield updated_history
108
-
109
  try:
 
 
110
  extracted_text = ""
111
  if uploaded_files and isinstance(uploaded_files, list):
112
  total_files = len(uploaded_files)
@@ -145,21 +145,22 @@ def create_ui(agent: TxAgent):
145
  )
146
  for update in generator:
147
  if isinstance(update, str):
148
- all_responses += update
149
-
150
- all_responses = sanitize_utf8(all_responses.strip())
151
 
152
- # Replace the temporary loading message with the final answer
153
- updated_history[-1] = {"role": "assistant", "content": all_responses}
154
- yield updated_history
 
 
 
155
 
156
  except Exception as chat_error:
157
  print(f"Chat error: {chat_error}")
158
- updated_history[-1] = {
159
- "role": "assistant",
160
- "content": "❌ An error occurred while processing your request. Please try again."
161
- }
162
- yield updated_history
163
 
164
  inputs = [message_input, chatbot, conversation_state, file_upload]
165
  send_button.click(fn=handle_chat, inputs=inputs, outputs=chatbot)
@@ -171,4 +172,4 @@ def create_ui(agent: TxAgent):
171
  ["Is there anything abnormal in the attached blood work report?"]
172
  ], inputs=message_input)
173
 
174
- return demo
 
3
  import pandas as pd
4
  import pdfplumber
5
  import gradio as gr
6
+ import re
7
  from typing import List
8
 
9
  # ✅ Fix: Add src to Python path
 
14
  def sanitize_utf8(text: str) -> str:
15
  return text.encode("utf-8", "ignore").decode("utf-8")
16
 
17
+ def clean_final_response(response: str) -> str:
18
+ return re.split(r"\\[TOOL_CALLS\\]", response)[0].strip()
19
+
20
  def extract_all_text_from_csv_or_excel(file_path: str, progress=None, index=0, total=1) -> str:
21
  try:
22
  if not os.path.exists(file_path):
 
99
  "You are an expert clinical AI assistant reviewing medical form or interview data. "
100
  "Your job is to analyze this data and reason about any information or red flags that a human doctor might have overlooked. "
101
  "Provide a **detailed and structured response**, including examples, supporting evidence from the form, and clinical rationale for why these items matter. "
102
+ "Ensure the output is informative and helpful for improving patient care. "
103
  "Do not hallucinate. Base the response only on the provided form content. "
104
  "End with a section labeled '🧠 Final Analysis' where you summarize key findings the doctor may have missed."
105
  )
106
 
 
 
 
 
 
 
 
107
  try:
108
+ yield history + [{"role": "assistant", "content": "⏳ **Processing... Please wait while I analyze the files.**"}]
109
+
110
  extracted_text = ""
111
  if uploaded_files and isinstance(uploaded_files, list):
112
  total_files = len(uploaded_files)
 
145
  )
146
  for update in generator:
147
  if isinstance(update, str):
148
+ all_responses += update + "\n"
 
 
149
 
150
+ all_responses = clean_final_response(sanitize_utf8(all_responses.strip()))
151
+ final_history = history + [
152
+ {"role": "user", "content": message},
153
+ {"role": "assistant", "content": all_responses}
154
+ ]
155
+ yield final_history
156
 
157
  except Exception as chat_error:
158
  print(f"Chat error: {chat_error}")
159
+ final_history = history + [
160
+ {"role": "user", "content": message},
161
+ {"role": "assistant", "content": "❌ An error occurred while processing your request."}
162
+ ]
163
+ yield final_history
164
 
165
  inputs = [message_input, chatbot, conversation_state, file_upload]
166
  send_button.click(fn=handle_chat, inputs=inputs, outputs=chatbot)
 
172
  ["Is there anything abnormal in the attached blood work report?"]
173
  ], inputs=message_input)
174
 
175
+ return demo