Update ui/ui_core.py
Browse files- ui/ui_core.py +13 -13
ui/ui_core.py
CHANGED
@@ -6,16 +6,15 @@ import gradio as gr
|
|
6 |
from tabulate import tabulate
|
7 |
from typing import List, Optional
|
8 |
|
9 |
-
# ✅ Add src to Python path
|
10 |
-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src"))
|
|
|
11 |
from txagent.txagent import TxAgent
|
12 |
|
13 |
def safe_extract_table_data(table: List[List[str]]) -> List[str]:
|
14 |
-
"""Safely extract data from a PDF table row by row."""
|
15 |
extracted_rows = []
|
16 |
if not table or not isinstance(table, list):
|
17 |
return extracted_rows
|
18 |
-
|
19 |
for row in table:
|
20 |
if not row or not isinstance(row, list):
|
21 |
continue
|
@@ -29,7 +28,6 @@ def safe_extract_table_data(table: List[List[str]]) -> List[str]:
|
|
29 |
return extracted_rows
|
30 |
|
31 |
def extract_all_text_from_csv_or_excel(file_path: str, progress=None, index=0, total=1) -> str:
|
32 |
-
"""Safely extract text from CSV or Excel files with error handling."""
|
33 |
try:
|
34 |
if not os.path.exists(file_path):
|
35 |
return f"File not found: {file_path}"
|
@@ -44,7 +42,6 @@ def extract_all_text_from_csv_or_excel(file_path: str, progress=None, index=0, t
|
|
44 |
if progress:
|
45 |
progress((index + 1) / total, desc=f"Processed table: {os.path.basename(file_path)}")
|
46 |
|
47 |
-
# Safely check for grouping columns
|
48 |
group_column = None
|
49 |
for col in ["Booking Number", "Form Name"]:
|
50 |
if col in df.columns:
|
@@ -71,7 +68,6 @@ def extract_all_text_from_csv_or_excel(file_path: str, progress=None, index=0, t
|
|
71 |
return f"Error parsing file {os.path.basename(file_path)}: {str(e)}"
|
72 |
|
73 |
def extract_all_text_from_pdf(file_path: str, progress=None, index=0, total=1) -> str:
|
74 |
-
"""Safely extract text from PDF files with comprehensive error handling."""
|
75 |
extracted = []
|
76 |
try:
|
77 |
if not os.path.exists(file_path):
|
@@ -111,7 +107,6 @@ def create_ui(agent: TxAgent):
|
|
111 |
conversation_state = gr.State([])
|
112 |
|
113 |
def handle_chat(message: str, history: list, conversation: list, uploaded_files: list, progress=gr.Progress()):
|
114 |
-
"""Handle chat with comprehensive error handling for file processing."""
|
115 |
context = (
|
116 |
"You are an expert clinical AI assistant reviewing medical form or interview data. "
|
117 |
"Your job is to analyze this data and reason about any information or red flags that a human doctor might have overlooked. "
|
@@ -162,10 +157,15 @@ def create_ui(agent: TxAgent):
|
|
162 |
for update in generator:
|
163 |
try:
|
164 |
if isinstance(update, list):
|
165 |
-
cleaned = [
|
166 |
-
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
169 |
if cleaned:
|
170 |
final_response = cleaned
|
171 |
yield cleaned
|
@@ -190,4 +190,4 @@ def create_ui(agent: TxAgent):
|
|
190 |
["Is there anything abnormal in the attached blood work report?"]
|
191 |
], inputs=message_input)
|
192 |
|
193 |
-
return demo
|
|
|
6 |
from tabulate import tabulate
|
7 |
from typing import List, Optional
|
8 |
|
9 |
+
# ✅ Fix: Add src to Python path with correct parentheses
|
10 |
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
|
11 |
+
|
12 |
from txagent.txagent import TxAgent
|
13 |
|
14 |
def safe_extract_table_data(table: List[List[str]]) -> List[str]:
|
|
|
15 |
extracted_rows = []
|
16 |
if not table or not isinstance(table, list):
|
17 |
return extracted_rows
|
|
|
18 |
for row in table:
|
19 |
if not row or not isinstance(row, list):
|
20 |
continue
|
|
|
28 |
return extracted_rows
|
29 |
|
30 |
def extract_all_text_from_csv_or_excel(file_path: str, progress=None, index=0, total=1) -> str:
|
|
|
31 |
try:
|
32 |
if not os.path.exists(file_path):
|
33 |
return f"File not found: {file_path}"
|
|
|
42 |
if progress:
|
43 |
progress((index + 1) / total, desc=f"Processed table: {os.path.basename(file_path)}")
|
44 |
|
|
|
45 |
group_column = None
|
46 |
for col in ["Booking Number", "Form Name"]:
|
47 |
if col in df.columns:
|
|
|
68 |
return f"Error parsing file {os.path.basename(file_path)}: {str(e)}"
|
69 |
|
70 |
def extract_all_text_from_pdf(file_path: str, progress=None, index=0, total=1) -> str:
|
|
|
71 |
extracted = []
|
72 |
try:
|
73 |
if not os.path.exists(file_path):
|
|
|
107 |
conversation_state = gr.State([])
|
108 |
|
109 |
def handle_chat(message: str, history: list, conversation: list, uploaded_files: list, progress=gr.Progress()):
|
|
|
110 |
context = (
|
111 |
"You are an expert clinical AI assistant reviewing medical form or interview data. "
|
112 |
"Your job is to analyze this data and reason about any information or red flags that a human doctor might have overlooked. "
|
|
|
157 |
for update in generator:
|
158 |
try:
|
159 |
if isinstance(update, list):
|
160 |
+
cleaned = [
|
161 |
+
msg for msg in update
|
162 |
+
if hasattr(msg, 'role')
|
163 |
+
and not (
|
164 |
+
msg.role == "assistant"
|
165 |
+
and hasattr(msg, 'content')
|
166 |
+
and msg.content.strip().startswith("🧰")
|
167 |
+
)
|
168 |
+
]
|
169 |
if cleaned:
|
170 |
final_response = cleaned
|
171 |
yield cleaned
|
|
|
190 |
["Is there anything abnormal in the attached blood work report?"]
|
191 |
], inputs=message_input)
|
192 |
|
193 |
+
return demo
|