Ali2206 commited on
Commit
eaac969
·
verified ·
1 Parent(s): 781bcb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -53
app.py CHANGED
@@ -6,81 +6,73 @@ import importlib
6
  import inspect
7
  import json
8
 
9
- # Fix path to include src
10
  sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
11
 
12
- # Reload TxAgent from txagent.py
13
  import txagent.txagent
14
  importlib.reload(txagent.txagent)
15
  from txagent.txagent import TxAgent
16
 
17
- # Debug info
18
  print(">>> TxAgent loaded from:", inspect.getfile(TxAgent))
19
  print(">>> TxAgent has run_gradio_chat:", hasattr(TxAgent, "run_gradio_chat"))
20
 
21
- # Env vars
22
  current_dir = os.path.abspath(os.path.dirname(__file__))
23
  os.environ["MKL_THREADING_LAYER"] = "GNU"
24
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
25
 
26
- # Model config
27
  model_name = "mims-harvard/TxAgent-T1-Llama-3.1-8B"
28
  rag_model_name = "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B"
29
  new_tool_files = {
30
  "new_tool": os.path.join(current_dir, "data", "new_tool.json")
31
  }
32
 
33
- # Sample questions
34
  question_examples = [
35
  ["Given a patient with WHIM syndrome on prophylactic antibiotics, is it advisable to co-administer Xolremdi with fluconazole?"],
36
  ["What treatment options exist for HER2+ breast cancer resistant to trastuzumab?"]
37
  ]
38
 
39
- # Helper: format assistant responses in collapsible panels
40
-
41
- def format_collapsible(content, tool_name=None):
42
- # Try parsing if it's a JSON string
43
- if isinstance(content, str):
44
- try:
45
- content = json.loads(content)
46
- except Exception:
47
- pass
48
-
49
- if isinstance(content, dict) and "results" in content:
50
- readable = ""
51
- for i, result in enumerate(content["results"], 1):
52
- readable += f"\n🔹 **Result {i}:**\n"
53
- for key, value in result.items():
54
- key_str = key.replace("openfda.", "").replace("_", " ").capitalize()
55
- val_str = ", ".join(value) if isinstance(value, list) else str(value)
56
- readable += f"- **{key_str}**: {val_str}\n"
57
- formatted = readable.strip()
58
- elif isinstance(content, (dict, list)):
59
- formatted = json.dumps(content, indent=2)
60
- else:
61
- formatted = str(content)
62
-
63
- title = f"{tool_name or 'Answer'}"
64
  return (
65
- "<details style='border: 1px solid #aaa; border-radius: 8px; padding: 10px; margin: 12px 0; background-color: #f8f8f8;'>"
66
- f"<summary style='font-weight: bold; font-size: 16px; color: #333;'>{title}</summary>"
67
- f"<div style='white-space: pre-wrap; font-family: sans-serif; color: #222; padding-top: 6px;'>{formatted}</div>"
68
- "</details>"
69
  )
70
 
71
- # === UI setup
72
  def create_ui(agent):
73
- with gr.Blocks(css="body { background-color: #f5f5f5; font-family: sans-serif; }") as demo:
74
  gr.Markdown("<h1 style='text-align: center;'>TxAgent: Therapeutic Reasoning</h1>")
75
- gr.Markdown("<p style='text-align: center;'>Ask biomedical or therapeutic questions. Powered by step-by-step reasoning and tools.</p>")
76
-
77
- conversation_state = gr.State([])
78
 
79
  chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
80
  message_input = gr.Textbox(placeholder="Ask your biomedical question...", show_label=False)
81
  send_button = gr.Button("Send", variant="primary")
 
82
 
83
- # Main handler
84
  def handle_chat(message, history, conversation):
85
  generator = agent.run_gradio_chat(
86
  message=message,
@@ -94,30 +86,30 @@ def create_ui(agent):
94
  )
95
 
96
  for update in generator:
97
- formatted = []
98
  for m in update:
99
  role = m["role"] if isinstance(m, dict) else getattr(m, "role", "assistant")
100
  content = m["content"] if isinstance(m, dict) else getattr(m, "content", "")
101
- tool_name = m.get("tool_name") if isinstance(m, dict) else getattr(m, "tool_name", None)
102
-
103
  if role == "assistant":
104
- content = format_collapsible(content, tool_name)
105
-
106
- formatted.append({"role": role, "content": content})
107
- yield formatted
108
 
 
109
  inputs = [message_input, chatbot, conversation_state]
110
  send_button.click(fn=handle_chat, inputs=inputs, outputs=chatbot)
111
  message_input.submit(fn=handle_chat, inputs=inputs, outputs=chatbot)
112
 
113
  gr.Examples(examples=question_examples, inputs=message_input)
114
- gr.Markdown("<p style='font-size: 12px; text-align: center; color: gray;'>This demo is for research purposes only and does not provide medical advice.</p>")
115
 
116
  return demo
117
 
118
- # === Entry point
119
  if __name__ == "__main__":
120
  freeze_support()
 
121
  try:
122
  agent = TxAgent(
123
  model_name=model_name,
@@ -132,11 +124,16 @@ if __name__ == "__main__":
132
  agent.init_model()
133
 
134
  if not hasattr(agent, "run_gradio_chat"):
135
- raise AttributeError("TxAgent missing run_gradio_chat")
136
 
137
  demo = create_ui(agent)
138
- demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True, show_error=True)
 
 
 
 
 
139
 
140
  except Exception as e:
141
- print(f"\u274c App failed to start: {e}")
142
  raise
 
6
  import inspect
7
  import json
8
 
9
+ # === Fix path to include src/txagent
10
  sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
11
 
12
+ # === Import and reload to ensure correct file
13
  import txagent.txagent
14
  importlib.reload(txagent.txagent)
15
  from txagent.txagent import TxAgent
16
 
17
+ # === Debug print
18
  print(">>> TxAgent loaded from:", inspect.getfile(TxAgent))
19
  print(">>> TxAgent has run_gradio_chat:", hasattr(TxAgent, "run_gradio_chat"))
20
 
21
+ # === Environment
22
  current_dir = os.path.abspath(os.path.dirname(__file__))
23
  os.environ["MKL_THREADING_LAYER"] = "GNU"
24
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
25
 
26
+ # === Model config
27
  model_name = "mims-harvard/TxAgent-T1-Llama-3.1-8B"
28
  rag_model_name = "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B"
29
  new_tool_files = {
30
  "new_tool": os.path.join(current_dir, "data", "new_tool.json")
31
  }
32
 
33
+ # === Example prompts
34
  question_examples = [
35
  ["Given a patient with WHIM syndrome on prophylactic antibiotics, is it advisable to co-administer Xolremdi with fluconazole?"],
36
  ["What treatment options exist for HER2+ breast cancer resistant to trastuzumab?"]
37
  ]
38
 
39
+ # === Helper: extract tool name from content
40
+ def extract_tool_name_and_clean_content(raw_content):
41
+ if isinstance(raw_content, (dict, list)):
42
+ return "Answer", json.dumps(raw_content, indent=2)
43
+ if not isinstance(raw_content, str):
44
+ return "Answer", str(raw_content)
45
+
46
+ lines = raw_content.strip().splitlines()
47
+ title = "Answer"
48
+ clean = raw_content.strip()
49
+ for line in lines:
50
+ if line.strip().lower().startswith("tool_"):
51
+ title = f"Answer ({line.strip()})"
52
+ clean = raw_content.replace(line, "", 1).strip()
53
+ break
54
+ return title, clean
55
+
56
+ # === Helper: formatted collapsible output
57
+ def format_collapsible(content, title="Answer"):
 
 
 
 
 
 
58
  return (
59
+ f"<details style='border: 1px solid #ccc; padding: 8px; margin-top: 8px;'>"
60
+ f"<summary style='font-weight: bold;'>{title}</summary>"
61
+ f"<div style='margin-top: 8px; white-space: pre-wrap;'>{content}</div></details>"
 
62
  )
63
 
64
+ # === UI creation
65
  def create_ui(agent):
66
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
67
  gr.Markdown("<h1 style='text-align: center;'>TxAgent: Therapeutic Reasoning</h1>")
68
+ gr.Markdown("Ask biomedical or therapeutic questions. Powered by step-by-step reasoning and tools.")
 
 
69
 
70
  chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
71
  message_input = gr.Textbox(placeholder="Ask your biomedical question...", show_label=False)
72
  send_button = gr.Button("Send", variant="primary")
73
+ conversation_state = gr.State([])
74
 
75
+ # === Core handler (streaming generator)
76
  def handle_chat(message, history, conversation):
77
  generator = agent.run_gradio_chat(
78
  message=message,
 
86
  )
87
 
88
  for update in generator:
89
+ formatted_messages = []
90
  for m in update:
91
  role = m["role"] if isinstance(m, dict) else getattr(m, "role", "assistant")
92
  content = m["content"] if isinstance(m, dict) else getattr(m, "content", "")
 
 
93
  if role == "assistant":
94
+ title, clean = extract_tool_name_and_clean_content(content)
95
+ content = format_collapsible(clean, title)
96
+ formatted_messages.append({"role": role, "content": content})
97
+ yield formatted_messages
98
 
99
+ # === Trigger handlers
100
  inputs = [message_input, chatbot, conversation_state]
101
  send_button.click(fn=handle_chat, inputs=inputs, outputs=chatbot)
102
  message_input.submit(fn=handle_chat, inputs=inputs, outputs=chatbot)
103
 
104
  gr.Examples(examples=question_examples, inputs=message_input)
105
+ gr.Markdown("**DISCLAIMER**: This demo is for research purposes only and does not provide medical advice.")
106
 
107
  return demo
108
 
109
+ # === Startup
110
  if __name__ == "__main__":
111
  freeze_support()
112
+
113
  try:
114
  agent = TxAgent(
115
  model_name=model_name,
 
124
  agent.init_model()
125
 
126
  if not hasattr(agent, "run_gradio_chat"):
127
+ raise AttributeError("TxAgent is missing `run_gradio_chat`.")
128
 
129
  demo = create_ui(agent)
130
+ demo.queue().launch(
131
+ server_name="0.0.0.0",
132
+ server_port=7860,
133
+ show_error=True,
134
+ share=True
135
+ )
136
 
137
  except Exception as e:
138
+ print(f" App failed to start: {e}")
139
  raise