Update app.py
Browse files
app.py
CHANGED
|
@@ -4,42 +4,60 @@ import gradio as gr
|
|
| 4 |
from multiprocessing import freeze_support
|
| 5 |
import importlib
|
| 6 |
import inspect
|
|
|
|
| 7 |
|
|
|
|
| 8 |
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
|
|
|
|
|
|
|
| 9 |
import txagent.txagent
|
| 10 |
importlib.reload(txagent.txagent)
|
| 11 |
from txagent.txagent import TxAgent
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
current_dir = os.path.abspath(os.path.dirname(__file__))
|
| 14 |
os.environ["MKL_THREADING_LAYER"] = "GNU"
|
| 15 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 16 |
|
|
|
|
| 17 |
model_name = "mims-harvard/TxAgent-T1-Llama-3.1-8B"
|
| 18 |
rag_model_name = "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B"
|
| 19 |
new_tool_files = {
|
| 20 |
"new_tool": os.path.join(current_dir, "data", "new_tool.json")
|
| 21 |
}
|
| 22 |
|
|
|
|
| 23 |
question_examples = [
|
| 24 |
-
["Given a patient with WHIM syndrome on antibiotics, is Xolremdi
|
| 25 |
["What treatment options exist for HER2+ breast cancer resistant to trastuzumab?"]
|
| 26 |
]
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def create_ui(agent):
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
gr.Markdown("<h1 style='text-align: center;'>TxAgent: Therapeutic Reasoning</h1>")
|
| 42 |
-
gr.Markdown("Ask
|
| 43 |
|
| 44 |
temperature = gr.Slider(0, 1, value=0.3, label="Temperature")
|
| 45 |
max_new_tokens = gr.Slider(128, 4096, value=1024, label="Max New Tokens")
|
|
@@ -48,25 +66,11 @@ def create_ui(agent):
|
|
| 48 |
multi_agent = gr.Checkbox(label="Enable Multi-agent Reasoning", value=False)
|
| 49 |
conversation_state = gr.State([])
|
| 50 |
|
| 51 |
-
chatbot = gr.
|
| 52 |
-
summary_box = gr.Markdown(label="Summary")
|
| 53 |
-
studies_box = gr.Markdown(label="Clinical Studies")
|
| 54 |
-
interactions_box = gr.Markdown(label="Drug Interactions")
|
| 55 |
-
kinetics_box = gr.Markdown(label="Pharmacokinetics")
|
| 56 |
-
|
| 57 |
-
with chatbot:
|
| 58 |
-
with gr.TabItem("Summary"):
|
| 59 |
-
summary_display = summary_box
|
| 60 |
-
with gr.TabItem("Clinical Studies"):
|
| 61 |
-
studies_display = studies_box
|
| 62 |
-
with gr.TabItem("Drug Interactions"):
|
| 63 |
-
interactions_display = interactions_box
|
| 64 |
-
with gr.TabItem("Pharmacokinetics"):
|
| 65 |
-
kinetics_display = kinetics_box
|
| 66 |
-
|
| 67 |
message_input = gr.Textbox(placeholder="Ask your biomedical question...", show_label=False)
|
| 68 |
send_button = gr.Button("Send", variant="primary")
|
| 69 |
|
|
|
|
| 70 |
def handle_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
|
| 71 |
generator = agent.run_gradio_chat(
|
| 72 |
message=message,
|
|
@@ -79,36 +83,32 @@ def create_ui(agent):
|
|
| 79 |
max_round=max_round
|
| 80 |
)
|
| 81 |
|
| 82 |
-
final_output = ""
|
| 83 |
for update in generator:
|
|
|
|
| 84 |
for m in update:
|
| 85 |
role = m["role"] if isinstance(m, dict) else getattr(m, "role", "assistant")
|
| 86 |
content = m["content"] if isinstance(m, dict) else getattr(m, "content", "")
|
| 87 |
-
if role == "assistant":
|
| 88 |
-
final_output += content + "\n"
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
inputs=[message_input, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round],
|
| 96 |
-
outputs=[summary_box, studies_box, interactions_box, kinetics_box]
|
| 97 |
-
)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
)
|
| 104 |
|
| 105 |
gr.Examples(examples=question_examples, inputs=message_input)
|
| 106 |
-
gr.Markdown("**DISCLAIMER**:
|
| 107 |
|
| 108 |
return demo
|
| 109 |
|
|
|
|
| 110 |
if __name__ == "__main__":
|
| 111 |
freeze_support()
|
|
|
|
| 112 |
try:
|
| 113 |
agent = TxAgent(
|
| 114 |
model_name=model_name,
|
|
@@ -118,12 +118,12 @@ if __name__ == "__main__":
|
|
| 118 |
enable_checker=True,
|
| 119 |
step_rag_num=10,
|
| 120 |
seed=100,
|
| 121 |
-
additional_default_tools=[]
|
| 122 |
)
|
| 123 |
agent.init_model()
|
| 124 |
|
| 125 |
if not hasattr(agent, "run_gradio_chat"):
|
| 126 |
-
raise AttributeError("
|
| 127 |
|
| 128 |
demo = create_ui(agent)
|
| 129 |
demo.launch(show_error=True)
|
|
|
|
| 4 |
from multiprocessing import freeze_support
|
| 5 |
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 |
+
def format_collapsible(content):
|
| 41 |
+
if isinstance(content, (dict, list)):
|
| 42 |
+
try:
|
| 43 |
+
formatted = json.dumps(content, indent=2)
|
| 44 |
+
except Exception:
|
| 45 |
+
formatted = str(content)
|
| 46 |
+
else:
|
| 47 |
+
formatted = str(content)
|
| 48 |
+
|
| 49 |
+
return (
|
| 50 |
+
"<details style='border: 1px solid #ccc; padding: 8px; margin-top: 8px;'>"
|
| 51 |
+
"<summary style='font-weight: bold;'>Answer</summary>"
|
| 52 |
+
f"<pre style='white-space: pre-wrap;'>{formatted}</pre>"
|
| 53 |
+
"</details>"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# === UI setup
|
| 57 |
def create_ui(agent):
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
gr.Markdown("<h1 style='text-align: center;'>TxAgent: Therapeutic Reasoning</h1>")
|
| 60 |
+
gr.Markdown("Ask biomedical or therapeutic questions. Powered by step-by-step reasoning and tools.")
|
| 61 |
|
| 62 |
temperature = gr.Slider(0, 1, value=0.3, label="Temperature")
|
| 63 |
max_new_tokens = gr.Slider(128, 4096, value=1024, label="Max New Tokens")
|
|
|
|
| 66 |
multi_agent = gr.Checkbox(label="Enable Multi-agent Reasoning", value=False)
|
| 67 |
conversation_state = gr.State([])
|
| 68 |
|
| 69 |
+
chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
message_input = gr.Textbox(placeholder="Ask your biomedical question...", show_label=False)
|
| 71 |
send_button = gr.Button("Send", variant="primary")
|
| 72 |
|
| 73 |
+
# Main handler
|
| 74 |
def handle_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
|
| 75 |
generator = agent.run_gradio_chat(
|
| 76 |
message=message,
|
|
|
|
| 83 |
max_round=max_round
|
| 84 |
)
|
| 85 |
|
|
|
|
| 86 |
for update in generator:
|
| 87 |
+
formatted = []
|
| 88 |
for m in update:
|
| 89 |
role = m["role"] if isinstance(m, dict) else getattr(m, "role", "assistant")
|
| 90 |
content = m["content"] if isinstance(m, dict) else getattr(m, "content", "")
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
if role == "assistant":
|
| 93 |
+
content = format_collapsible(content)
|
| 94 |
|
| 95 |
+
formatted.append({"role": role, "content": content})
|
| 96 |
+
yield formatted
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# Button and Enter triggers
|
| 99 |
+
inputs = [message_input, chatbot, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round]
|
| 100 |
+
send_button.click(fn=handle_chat, inputs=inputs, outputs=chatbot)
|
| 101 |
+
message_input.submit(fn=handle_chat, inputs=inputs, outputs=chatbot)
|
|
|
|
| 102 |
|
| 103 |
gr.Examples(examples=question_examples, inputs=message_input)
|
| 104 |
+
gr.Markdown("**DISCLAIMER**: This demo is for research purposes only and does not provide medical advice.")
|
| 105 |
|
| 106 |
return demo
|
| 107 |
|
| 108 |
+
# === Entry point
|
| 109 |
if __name__ == "__main__":
|
| 110 |
freeze_support()
|
| 111 |
+
|
| 112 |
try:
|
| 113 |
agent = TxAgent(
|
| 114 |
model_name=model_name,
|
|
|
|
| 118 |
enable_checker=True,
|
| 119 |
step_rag_num=10,
|
| 120 |
seed=100,
|
| 121 |
+
additional_default_tools=[] # Avoid loading unimplemented tools
|
| 122 |
)
|
| 123 |
agent.init_model()
|
| 124 |
|
| 125 |
if not hasattr(agent, "run_gradio_chat"):
|
| 126 |
+
raise AttributeError("TxAgent missing run_gradio_chat")
|
| 127 |
|
| 128 |
demo = create_ui(agent)
|
| 129 |
demo.launch(show_error=True)
|