Update app.py
Browse files
app.py
CHANGED
@@ -6,15 +6,16 @@ from multiprocessing import freeze_support
|
|
6 |
import importlib
|
7 |
import inspect
|
8 |
|
9 |
-
# ===
|
10 |
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "src"))
|
11 |
|
12 |
-
# === Reload to avoid stale
|
13 |
import txagent.txagent
|
14 |
importlib.reload(txagent.txagent)
|
15 |
from txagent.txagent import TxAgent
|
|
|
16 |
|
17 |
-
# === Debug
|
18 |
print(">>> TxAgent loaded from:", inspect.getfile(TxAgent))
|
19 |
print(">>> TxAgent has run_gradio_chat:", hasattr(TxAgent, "run_gradio_chat"))
|
20 |
|
@@ -23,40 +24,28 @@ current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
23 |
os.environ["MKL_THREADING_LAYER"] = "GNU"
|
24 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
25 |
|
26 |
-
# === UI
|
27 |
DESCRIPTION = '''
|
28 |
-
<
|
29 |
-
<h1 style="text-align: center;">TxAgent: An AI Agent for Therapeutic Reasoning Across a Universe of Tools</h1>
|
30 |
-
</div>
|
31 |
'''
|
32 |
-
INTRO = "
|
33 |
-
LICENSE = "DISCLAIMER: THIS WEBSITE DOES NOT PROVIDE MEDICAL ADVICE
|
34 |
-
|
35 |
-
|
36 |
-
h1 { text-align: center; }
|
37 |
-
.gradio-accordion { margin-top: 0 !important; margin-bottom: 0 !important; }
|
38 |
-
"""
|
39 |
-
chat_css = """
|
40 |
-
.gr-button { font-size: 18px !important; }
|
41 |
-
.gr-button svg { width: 26px !important; height: 26px !important; }
|
42 |
-
"""
|
43 |
-
|
44 |
-
# === Model setup
|
45 |
model_name = "mims-harvard/TxAgent-T1-Llama-3.1-8B"
|
46 |
rag_model_name = "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B"
|
47 |
new_tool_files = {
|
48 |
"new_tool": os.path.join(current_dir, "data", "new_tool.json")
|
49 |
}
|
50 |
|
51 |
-
# === Sample prompts
|
52 |
question_examples = [
|
53 |
-
["Given a
|
54 |
-
["
|
55 |
]
|
56 |
|
57 |
-
# === UI
|
58 |
def create_ui(agent):
|
59 |
-
with gr.Blocks(
|
60 |
gr.Markdown(DESCRIPTION)
|
61 |
gr.Markdown(INTRO)
|
62 |
|
@@ -67,13 +56,12 @@ def create_ui(agent):
|
|
67 |
multi_agent = gr.Checkbox(label="Enable Multi-agent Reasoning", value=False)
|
68 |
conversation_state = gr.State([])
|
69 |
|
70 |
-
chatbot = gr.Chatbot(label="TxAgent", height=
|
71 |
message_input = gr.Textbox(placeholder="Ask a biomedical question...", show_label=False)
|
72 |
-
|
73 |
send_btn = gr.Button("Send", variant="primary")
|
74 |
|
75 |
-
# === Streaming handler
|
76 |
def handle_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
|
|
|
77 |
return agent.run_gradio_chat(
|
78 |
message=message,
|
79 |
history=history,
|
@@ -85,20 +73,18 @@ def create_ui(agent):
|
|
85 |
max_round=max_round
|
86 |
)
|
87 |
|
88 |
-
# === Submit handlers
|
89 |
send_btn.click(
|
90 |
fn=handle_chat,
|
91 |
inputs=[message_input, chatbot, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round],
|
92 |
-
outputs=chatbot
|
93 |
)
|
94 |
|
95 |
message_input.submit(
|
96 |
fn=handle_chat,
|
97 |
inputs=[message_input, chatbot, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round],
|
98 |
-
outputs=chatbot
|
99 |
)
|
100 |
|
101 |
-
# === Example buttons
|
102 |
gr.Examples(
|
103 |
examples=question_examples,
|
104 |
inputs=message_input
|
@@ -108,7 +94,7 @@ def create_ui(agent):
|
|
108 |
|
109 |
return demo
|
110 |
|
111 |
-
# === App
|
112 |
if __name__ == "__main__":
|
113 |
freeze_support()
|
114 |
try:
|
@@ -124,12 +110,12 @@ if __name__ == "__main__":
|
|
124 |
)
|
125 |
agent.init_model()
|
126 |
|
127 |
-
if not hasattr(agent,
|
128 |
-
raise AttributeError("TxAgent is missing `run_gradio_chat`.
|
129 |
|
130 |
demo = create_ui(agent)
|
131 |
demo.launch(show_error=True)
|
132 |
|
133 |
except Exception as e:
|
134 |
-
print(f"🚨
|
135 |
raise
|
|
|
6 |
import importlib
|
7 |
import inspect
|
8 |
|
9 |
+
# === Path fix
|
10 |
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "src"))
|
11 |
|
12 |
+
# === Reload to avoid stale module
|
13 |
import txagent.txagent
|
14 |
importlib.reload(txagent.txagent)
|
15 |
from txagent.txagent import TxAgent
|
16 |
+
from gradio import ChatMessage
|
17 |
|
18 |
+
# === Debug
|
19 |
print(">>> TxAgent loaded from:", inspect.getfile(TxAgent))
|
20 |
print(">>> TxAgent has run_gradio_chat:", hasattr(TxAgent, "run_gradio_chat"))
|
21 |
|
|
|
24 |
os.environ["MKL_THREADING_LAYER"] = "GNU"
|
25 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
26 |
|
27 |
+
# === UI text
|
28 |
DESCRIPTION = '''
|
29 |
+
<h1 style="text-align: center;">TxAgent: AI for Therapeutic Reasoning</h1>
|
|
|
|
|
30 |
'''
|
31 |
+
INTRO = "Ask biomedical or therapeutic questions. Results are powered by tools and reasoning."
|
32 |
+
LICENSE = "DISCLAIMER: THIS WEBSITE DOES NOT PROVIDE MEDICAL ADVICE."
|
33 |
+
|
34 |
+
# === Model & tool config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
model_name = "mims-harvard/TxAgent-T1-Llama-3.1-8B"
|
36 |
rag_model_name = "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B"
|
37 |
new_tool_files = {
|
38 |
"new_tool": os.path.join(current_dir, "data", "new_tool.json")
|
39 |
}
|
40 |
|
|
|
41 |
question_examples = [
|
42 |
+
["Given a patient with WHIM syndrome on prophylactic antibiotics, is it advisable to co-administer Xolremdi with fluconazole?"],
|
43 |
+
["What treatment options exist for HER2+ breast cancer resistant to trastuzumab?"]
|
44 |
]
|
45 |
|
46 |
+
# === Gradio UI
|
47 |
def create_ui(agent):
|
48 |
+
with gr.Blocks() as demo:
|
49 |
gr.Markdown(DESCRIPTION)
|
50 |
gr.Markdown(INTRO)
|
51 |
|
|
|
56 |
multi_agent = gr.Checkbox(label="Enable Multi-agent Reasoning", value=False)
|
57 |
conversation_state = gr.State([])
|
58 |
|
59 |
+
chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
|
60 |
message_input = gr.Textbox(placeholder="Ask a biomedical question...", show_label=False)
|
|
|
61 |
send_btn = gr.Button("Send", variant="primary")
|
62 |
|
|
|
63 |
def handle_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
|
64 |
+
# Ensure response is a generator that yields list of {role, content} dictionaries
|
65 |
return agent.run_gradio_chat(
|
66 |
message=message,
|
67 |
history=history,
|
|
|
73 |
max_round=max_round
|
74 |
)
|
75 |
|
|
|
76 |
send_btn.click(
|
77 |
fn=handle_chat,
|
78 |
inputs=[message_input, chatbot, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round],
|
79 |
+
outputs=chatbot
|
80 |
)
|
81 |
|
82 |
message_input.submit(
|
83 |
fn=handle_chat,
|
84 |
inputs=[message_input, chatbot, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round],
|
85 |
+
outputs=chatbot
|
86 |
)
|
87 |
|
|
|
88 |
gr.Examples(
|
89 |
examples=question_examples,
|
90 |
inputs=message_input
|
|
|
94 |
|
95 |
return demo
|
96 |
|
97 |
+
# === App startup
|
98 |
if __name__ == "__main__":
|
99 |
freeze_support()
|
100 |
try:
|
|
|
110 |
)
|
111 |
agent.init_model()
|
112 |
|
113 |
+
if not hasattr(agent, "run_gradio_chat"):
|
114 |
+
raise AttributeError("❌ TxAgent is missing `run_gradio_chat`.")
|
115 |
|
116 |
demo = create_ui(agent)
|
117 |
demo.launch(show_error=True)
|
118 |
|
119 |
except Exception as e:
|
120 |
+
print(f"🚨 Startup error: {e}")
|
121 |
raise
|