Update ui/ui_core.py
Browse files- ui/ui_core.py +40 -45
ui/ui_core.py
CHANGED
|
@@ -1,42 +1,32 @@
|
|
| 1 |
-
import
|
| 2 |
import os
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import pdfplumber
|
| 5 |
-
import gradio as gr
|
| 6 |
|
| 7 |
-
#
|
| 8 |
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
|
| 9 |
from txagent.txagent import TxAgent
|
| 10 |
|
| 11 |
|
| 12 |
-
def
|
| 13 |
try:
|
| 14 |
-
|
| 15 |
-
df = pd.read_csv(file_path, low_memory=False)
|
| 16 |
-
elif file_path.endswith((".xls", ".xlsx")):
|
| 17 |
-
df = pd.read_excel(file_path)
|
| 18 |
-
else:
|
| 19 |
-
return f"Unsupported spreadsheet format: {file_path}"
|
| 20 |
-
if progress:
|
| 21 |
-
progress((index + 1) / total, desc=f"Processed table: {os.path.basename(file_path)}")
|
| 22 |
return df.to_string(index=False)
|
| 23 |
except Exception as e:
|
| 24 |
-
return f"Error parsing
|
| 25 |
|
| 26 |
|
| 27 |
-
def extract_all_text_from_pdf(file_path
|
| 28 |
extracted = []
|
| 29 |
try:
|
| 30 |
with pdfplumber.open(file_path) as pdf:
|
| 31 |
-
|
| 32 |
-
for i, page in enumerate(pdf.pages):
|
| 33 |
tables = page.extract_tables()
|
| 34 |
for table in tables:
|
| 35 |
for row in table:
|
| 36 |
if any(row):
|
| 37 |
extracted.append("\t".join([cell or "" for cell in row]))
|
| 38 |
-
if progress:
|
| 39 |
-
progress((index + i / num_pages) / total, desc=f"Parsing PDF: {os.path.basename(file_path)} ({i+1}/{num_pages})")
|
| 40 |
return "\n".join(extracted)
|
| 41 |
except Exception as e:
|
| 42 |
return f"Error parsing PDF: {e}"
|
|
@@ -44,39 +34,45 @@ def extract_all_text_from_pdf(file_path, progress=None, index=0, total=1):
|
|
| 44 |
|
| 45 |
def create_ui(agent: TxAgent):
|
| 46 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 47 |
-
gr.Markdown("<h1 style='text-align: center;'>
|
| 48 |
-
chatbot = gr.Chatbot(label="
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
conversation_state = gr.State([])
|
| 58 |
|
| 59 |
-
def handle_chat(message, history, conversation,
|
| 60 |
context = (
|
| 61 |
"You are a clinical AI reviewing medical interview or form data. "
|
| 62 |
"Analyze the extracted content and reason step-by-step about what the doctor could have missed. "
|
| 63 |
"Don't answer yet — just reason."
|
| 64 |
)
|
| 65 |
|
| 66 |
-
if
|
| 67 |
extracted_text = ""
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
for index, file in enumerate(uploaded_files):
|
| 71 |
path = file.name
|
| 72 |
-
if path.endswith(
|
| 73 |
-
extracted_text +=
|
| 74 |
elif path.endswith(".pdf"):
|
| 75 |
-
extracted_text += extract_all_text_from_pdf(path
|
| 76 |
else:
|
| 77 |
extracted_text += f"(Uploaded file: {os.path.basename(path)})\n"
|
| 78 |
-
if progress:
|
| 79 |
-
progress((index + 1) / total_files, desc=f"Skipping unsupported file: {os.path.basename(path)}")
|
| 80 |
|
| 81 |
message = f"{context}\n\n---\n{extracted_text.strip()}\n---\n\nNow reason what the doctor might have missed."
|
| 82 |
|
|
@@ -88,18 +84,17 @@ def create_ui(agent: TxAgent):
|
|
| 88 |
max_token=8192,
|
| 89 |
call_agent=False,
|
| 90 |
conversation=conversation,
|
| 91 |
-
uploaded_files=
|
| 92 |
max_round=30
|
| 93 |
)
|
| 94 |
for update in generator:
|
| 95 |
yield update
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 100 |
|
| 101 |
-
gr.Examples([
|
| 102 |
-
["Upload the files"],
|
| 103 |
-
], inputs=message_input)
|
| 104 |
|
| 105 |
return demo
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import sys
|
| 4 |
import pandas as pd
|
| 5 |
import pdfplumber
|
|
|
|
| 6 |
|
| 7 |
+
# Add src to Python path
|
| 8 |
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
|
| 9 |
from txagent.txagent import TxAgent
|
| 10 |
|
| 11 |
|
| 12 |
+
def extract_all_text_from_csv(file_path):
|
| 13 |
try:
|
| 14 |
+
df = pd.read_csv(file_path, low_memory=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
return df.to_string(index=False)
|
| 16 |
except Exception as e:
|
| 17 |
+
return f"Error parsing CSV: {e}"
|
| 18 |
|
| 19 |
|
| 20 |
+
def extract_all_text_from_pdf(file_path):
|
| 21 |
extracted = []
|
| 22 |
try:
|
| 23 |
with pdfplumber.open(file_path) as pdf:
|
| 24 |
+
for page in pdf.pages:
|
|
|
|
| 25 |
tables = page.extract_tables()
|
| 26 |
for table in tables:
|
| 27 |
for row in table:
|
| 28 |
if any(row):
|
| 29 |
extracted.append("\t".join([cell or "" for cell in row]))
|
|
|
|
|
|
|
| 30 |
return "\n".join(extracted)
|
| 31 |
except Exception as e:
|
| 32 |
return f"Error parsing PDF: {e}"
|
|
|
|
| 34 |
|
| 35 |
def create_ui(agent: TxAgent):
|
| 36 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 37 |
+
gr.Markdown("<h1 style='text-align: center;'>🧠 CPS: Clinical Processing System</h1>")
|
| 38 |
+
chatbot = gr.Chatbot(label="CPS Assistant", height=600, type="messages")
|
| 39 |
+
|
| 40 |
+
# Hidden file upload, attached to input bar
|
| 41 |
+
with gr.Row():
|
| 42 |
+
uploaded_files = gr.File(
|
| 43 |
+
label="📎", file_types=[".pdf", ".txt", ".docx", ".jpg", ".png", ".csv"],
|
| 44 |
+
file_count="multiple", visible=False
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
with gr.Column(scale=10):
|
| 48 |
+
message_input = gr.Textbox(
|
| 49 |
+
placeholder="Type your medical question or upload files...", show_label=False, scale=10
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
with gr.Column(scale=1, min_width=60):
|
| 53 |
+
file_icon = gr.UploadButton("📎", file_types=[".pdf", ".csv", ".docx", ".txt", ".jpg", ".png"], file_count="multiple")
|
| 54 |
+
|
| 55 |
+
send_button = gr.Button("Send", variant="primary")
|
| 56 |
+
|
| 57 |
conversation_state = gr.State([])
|
| 58 |
|
| 59 |
+
def handle_chat(message, history, conversation, new_files):
|
| 60 |
context = (
|
| 61 |
"You are a clinical AI reviewing medical interview or form data. "
|
| 62 |
"Analyze the extracted content and reason step-by-step about what the doctor could have missed. "
|
| 63 |
"Don't answer yet — just reason."
|
| 64 |
)
|
| 65 |
|
| 66 |
+
if new_files:
|
| 67 |
extracted_text = ""
|
| 68 |
+
for file in new_files:
|
|
|
|
|
|
|
| 69 |
path = file.name
|
| 70 |
+
if path.endswith(".csv"):
|
| 71 |
+
extracted_text += extract_all_text_from_csv(path) + "\n"
|
| 72 |
elif path.endswith(".pdf"):
|
| 73 |
+
extracted_text += extract_all_text_from_pdf(path) + "\n"
|
| 74 |
else:
|
| 75 |
extracted_text += f"(Uploaded file: {os.path.basename(path)})\n"
|
|
|
|
|
|
|
| 76 |
|
| 77 |
message = f"{context}\n\n---\n{extracted_text.strip()}\n---\n\nNow reason what the doctor might have missed."
|
| 78 |
|
|
|
|
| 84 |
max_token=8192,
|
| 85 |
call_agent=False,
|
| 86 |
conversation=conversation,
|
| 87 |
+
uploaded_files=new_files,
|
| 88 |
max_round=30
|
| 89 |
)
|
| 90 |
for update in generator:
|
| 91 |
yield update
|
| 92 |
|
| 93 |
+
# Bind send logic
|
| 94 |
+
file_icon.upload(fn=None, inputs=[], outputs=[uploaded_files])
|
| 95 |
+
send_button.click(fn=handle_chat, inputs=[message_input, chatbot, conversation_state, uploaded_files], outputs=chatbot)
|
| 96 |
+
message_input.submit(fn=handle_chat, inputs=[message_input, chatbot, conversation_state, uploaded_files], outputs=chatbot)
|
| 97 |
|
| 98 |
+
gr.Examples([["Upload your medical form and ask what the doctor might’ve missed."]], inputs=message_input)
|
|
|
|
|
|
|
| 99 |
|
| 100 |
return demo
|