Ali2206 commited on
Commit
0e7a2f6
·
verified ·
1 Parent(s): 9db0d8b

Update ui/ui_core.py

Browse files
Files changed (1) hide show
  1. ui/ui_core.py +13 -19
ui/ui_core.py CHANGED
@@ -1,30 +1,22 @@
1
-
2
  import sys
3
  import os
4
 
5
  # ✅ Add src to Python path
6
  sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
7
 
8
- from txagent.txagent import TxAgent # ✅ Now this will work
9
  import pandas as pd
10
  import pdfplumber
11
  import gradio as gr
12
 
13
-
14
- def extract_structured_text_from_csv(file_path):
15
  try:
16
  df = pd.read_csv(file_path)
17
- relevant_columns = [
18
- "Booking Number", "Form Name", "Form Item",
19
- "Item Response", "Interviewer", "Interview Date"
20
- ]
21
- df = df[[col for col in relevant_columns if col in df.columns]]
22
  return df.to_string(index=False)
23
  except Exception as e:
24
  return f"Error parsing CSV: {e}"
25
 
26
-
27
- def extract_structured_text_from_pdf(file_path):
28
  extracted = []
29
  try:
30
  with pdfplumber.open(file_path) as pdf:
@@ -38,10 +30,9 @@ def extract_structured_text_from_pdf(file_path):
38
  except Exception as e:
39
  return f"Error parsing PDF: {e}"
40
 
41
-
42
  def create_ui(agent: TxAgent):
43
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
44
- gr.Markdown("<h1 style='text-align: center;'>\ud83d\udc8a TxAgent: Therapeutic Reasoning</h1>")
45
  chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
46
 
47
  file_upload = gr.File(label="Upload Medical File", file_types=[".pdf", ".txt", ".docx", ".jpg", ".png", ".csv"], file_count="multiple")
@@ -51,9 +42,9 @@ def create_ui(agent: TxAgent):
51
 
52
  def handle_chat(message, history, conversation, uploaded_files):
53
  context = (
54
- "You are a clinical AI reviewing patient form data from interviews. "
55
- "Your task is to analyze the responses, dates, and items, and reason step-by-step about "
56
- "what the doctor might have overlooked. Do not summarize or answer yet — just reason step-by-step first."
57
  )
58
 
59
  if uploaded_files:
@@ -61,9 +52,12 @@ def create_ui(agent: TxAgent):
61
  for file in uploaded_files:
62
  path = file.name
63
  if path.endswith(".csv"):
64
- extracted_text += extract_structured_text_from_csv(path) + "\n"
65
  elif path.endswith(".pdf"):
66
- extracted_text += extract_structured_text_from_pdf(path) + "\n"
 
 
 
67
  message = f"{context}\n\n---\n{extracted_text.strip()}\n---\n\nNow reason what the doctor might have missed."
68
 
69
  generator = agent.run_gradio_chat(
@@ -88,4 +82,4 @@ def create_ui(agent: TxAgent):
88
  ["Upload the files"],
89
  ], inputs=message_input)
90
 
91
- return demo
 
 
1
  import sys
2
  import os
3
 
4
  # ✅ Add src to Python path
5
  sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
6
 
7
+ from txagent.txagent import TxAgent
8
  import pandas as pd
9
  import pdfplumber
10
  import gradio as gr
11
 
12
+ def extract_all_text_from_csv(file_path):
 
13
  try:
14
  df = pd.read_csv(file_path)
 
 
 
 
 
15
  return df.to_string(index=False)
16
  except Exception as e:
17
  return f"Error parsing CSV: {e}"
18
 
19
+ def extract_all_text_from_pdf(file_path):
 
20
  extracted = []
21
  try:
22
  with pdfplumber.open(file_path) as pdf:
 
30
  except Exception as e:
31
  return f"Error parsing PDF: {e}"
32
 
 
33
  def create_ui(agent: TxAgent):
34
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
35
+ gr.Markdown("<h1 style='text-align: center;'>💊 TxAgent: Therapeutic Reasoning</h1>")
36
  chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
37
 
38
  file_upload = gr.File(label="Upload Medical File", file_types=[".pdf", ".txt", ".docx", ".jpg", ".png", ".csv"], file_count="multiple")
 
42
 
43
  def handle_chat(message, history, conversation, uploaded_files):
44
  context = (
45
+ "You are a clinical AI reviewing medical interview or form data. "
46
+ "Analyze the extracted content and reason step-by-step about what the doctor could have missed. "
47
+ "Don't answer yet — just reason."
48
  )
49
 
50
  if uploaded_files:
 
52
  for file in uploaded_files:
53
  path = file.name
54
  if path.endswith(".csv"):
55
+ extracted_text += extract_all_text_from_csv(path) + "\n"
56
  elif path.endswith(".pdf"):
57
+ extracted_text += extract_all_text_from_pdf(path) + "\n"
58
+ else:
59
+ extracted_text += f"(Uploaded file: {os.path.basename(path)})\n"
60
+
61
  message = f"{context}\n\n---\n{extracted_text.strip()}\n---\n\nNow reason what the doctor might have missed."
62
 
63
  generator = agent.run_gradio_chat(
 
82
  ["Upload the files"],
83
  ], inputs=message_input)
84
 
85
+ return demo