Update ui/ui_core.py
Browse files- ui/ui_core.py +29 -18
ui/ui_core.py
CHANGED
|
@@ -4,17 +4,20 @@ 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
|
| 13 |
try:
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
return df.to_string(index=False)
|
| 16 |
except Exception as e:
|
| 17 |
-
return f"Error parsing
|
| 18 |
|
| 19 |
|
| 20 |
def extract_all_text_from_pdf(file_path):
|
|
@@ -36,25 +39,30 @@ 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 |
-
|
| 41 |
-
with gr.Row():
|
| 42 |
uploaded_files = gr.File(
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
with gr.Column(scale=10):
|
| 48 |
message_input = gr.Textbox(
|
| 49 |
-
placeholder="Type your medical question or upload files...",
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
-
with gr.Column(scale=1, min_width=
|
| 53 |
-
file_icon = gr.UploadButton(
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
|
| 59 |
def handle_chat(message, history, conversation, new_files):
|
| 60 |
context = (
|
|
@@ -67,8 +75,8 @@ def create_ui(agent: TxAgent):
|
|
| 67 |
extracted_text = ""
|
| 68 |
for file in new_files:
|
| 69 |
path = file.name
|
| 70 |
-
if path.endswith(".csv"):
|
| 71 |
-
extracted_text +=
|
| 72 |
elif path.endswith(".pdf"):
|
| 73 |
extracted_text += extract_all_text_from_pdf(path) + "\n"
|
| 74 |
else:
|
|
@@ -90,11 +98,14 @@ def create_ui(agent: TxAgent):
|
|
| 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([
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
return demo
|
|
|
|
| 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_or_excel(file_path):
|
| 13 |
try:
|
| 14 |
+
if file_path.endswith(".xlsx"):
|
| 15 |
+
df = pd.read_excel(file_path, engine="openpyxl")
|
| 16 |
+
else:
|
| 17 |
+
df = pd.read_csv(file_path, low_memory=False)
|
| 18 |
return df.to_string(index=False)
|
| 19 |
except Exception as e:
|
| 20 |
+
return f"Error parsing file: {e}"
|
| 21 |
|
| 22 |
|
| 23 |
def extract_all_text_from_pdf(file_path):
|
|
|
|
| 39 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 40 |
gr.Markdown("<h1 style='text-align: center;'>🧠 CPS: Clinical Processing System</h1>")
|
| 41 |
chatbot = gr.Chatbot(label="CPS Assistant", height=600, type="messages")
|
| 42 |
+
conversation_state = gr.State([])
|
| 43 |
|
| 44 |
+
with gr.Row(equal_height=True):
|
|
|
|
| 45 |
uploaded_files = gr.File(
|
| 46 |
+
visible=False,
|
| 47 |
+
file_types=[".pdf", ".txt", ".docx", ".jpg", ".png", ".csv", ".xlsx"],
|
| 48 |
+
file_count="multiple"
|
| 49 |
)
|
| 50 |
|
| 51 |
with gr.Column(scale=10):
|
| 52 |
message_input = gr.Textbox(
|
| 53 |
+
placeholder="Type your medical question or upload files...",
|
| 54 |
+
show_label=False,
|
| 55 |
+
container=True,
|
| 56 |
+
scale=10
|
| 57 |
)
|
| 58 |
|
| 59 |
+
with gr.Column(scale=1, min_width=50):
|
| 60 |
+
file_icon = gr.UploadButton(
|
| 61 |
+
"📎", file_types=[".pdf", ".txt", ".docx", ".csv", ".xlsx", ".jpg", ".png"],
|
| 62 |
+
file_count="multiple"
|
| 63 |
+
)
|
| 64 |
|
| 65 |
+
send_button = gr.Button("Send", variant="primary", scale=1)
|
| 66 |
|
| 67 |
def handle_chat(message, history, conversation, new_files):
|
| 68 |
context = (
|
|
|
|
| 75 |
extracted_text = ""
|
| 76 |
for file in new_files:
|
| 77 |
path = file.name
|
| 78 |
+
if path.endswith((".csv", ".xlsx")):
|
| 79 |
+
extracted_text += extract_all_text_from_csv_or_excel(path) + "\n"
|
| 80 |
elif path.endswith(".pdf"):
|
| 81 |
extracted_text += extract_all_text_from_pdf(path) + "\n"
|
| 82 |
else:
|
|
|
|
| 98 |
for update in generator:
|
| 99 |
yield update
|
| 100 |
|
|
|
|
| 101 |
file_icon.upload(fn=None, inputs=[], outputs=[uploaded_files])
|
| 102 |
send_button.click(fn=handle_chat, inputs=[message_input, chatbot, conversation_state, uploaded_files], outputs=chatbot)
|
| 103 |
message_input.submit(fn=handle_chat, inputs=[message_input, chatbot, conversation_state, uploaded_files], outputs=chatbot)
|
| 104 |
|
| 105 |
+
gr.Examples([
|
| 106 |
+
["Upload your medical form and ask what the doctor might’ve missed."],
|
| 107 |
+
["This patient was treated with antibiotics for UTI. What else should we check?"],
|
| 108 |
+
["Is there anything abnormal in the attached blood work report?"]
|
| 109 |
+
], inputs=message_input)
|
| 110 |
|
| 111 |
return demo
|