Ali2206 commited on
Commit
774fd26
·
verified ·
1 Parent(s): a4738ff

Update ui/ui_core.py

Browse files
Files changed (1) hide show
  1. ui/ui_core.py +18 -29
ui/ui_core.py CHANGED
@@ -4,20 +4,17 @@ 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_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,30 +36,25 @@ def create_ui(agent: TxAgent):
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,8 +67,8 @@ def create_ui(agent: TxAgent):
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,14 +90,11 @@ def create_ui(agent: TxAgent):
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
 
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):
 
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 = (
 
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:
 
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