Update app.py
Browse files
app.py
CHANGED
@@ -6,25 +6,24 @@ from multiprocessing import freeze_support
|
|
6 |
import importlib
|
7 |
import inspect
|
8 |
|
9 |
-
# === Fix
|
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 |
-
print(">>> TxAgent methods:", [m for m in dir(TxAgent) if not m.startswith("_")])
|
21 |
|
22 |
-
# ===
|
23 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
24 |
os.environ["MKL_THREADING_LAYER"] = "GNU"
|
25 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
26 |
|
27 |
-
# === UI
|
28 |
DESCRIPTION = '''
|
29 |
<div>
|
30 |
<h1 style="text-align: center;">TxAgent: An AI Agent for Therapeutic Reasoning Across a Universe of Tools</h1>
|
@@ -32,44 +31,30 @@ DESCRIPTION = '''
|
|
32 |
'''
|
33 |
INTRO = "Precision therapeutics require multimodal adaptive models..."
|
34 |
LICENSE = "DISCLAIMER: THIS WEBSITE DOES NOT PROVIDE MEDICAL ADVICE..."
|
35 |
-
|
36 |
-
<div style="padding: 30px; text-align: center;">
|
37 |
-
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">TxAgent</h1>
|
38 |
-
<p style="font-size: 18px;">Click clear 🗑️ before asking a new question.</p>
|
39 |
-
<p style="font-size: 18px;">Click retry 🔄 to see another answer.</p>
|
40 |
-
</div>
|
41 |
-
'''
|
42 |
css = """
|
43 |
h1 { text-align: center; }
|
44 |
-
|
45 |
-
margin: auto;
|
46 |
-
color: white;
|
47 |
-
background: #1565c0;
|
48 |
-
border-radius: 100vh;
|
49 |
-
}
|
50 |
-
.gradio-accordion {
|
51 |
-
margin-top: 0px !important;
|
52 |
-
margin-bottom: 0px !important;
|
53 |
-
}
|
54 |
"""
|
55 |
chat_css = """
|
56 |
-
.gr-button { font-size:
|
57 |
-
.gr-button svg { width:
|
58 |
"""
|
59 |
|
60 |
-
# === Model
|
61 |
model_name = "mims-harvard/TxAgent-T1-Llama-3.1-8B"
|
62 |
rag_model_name = "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B"
|
63 |
new_tool_files = {
|
64 |
"new_tool": os.path.join(current_dir, "data", "new_tool.json")
|
65 |
}
|
66 |
|
|
|
67 |
question_examples = [
|
68 |
["Given a 50-year-old patient experiencing severe acute pain and considering the use of the newly approved medication, Journavx, how should the dosage be adjusted considering moderate hepatic impairment?"],
|
69 |
["A 30-year-old patient is on Prozac for depression and now diagnosed with WHIM syndrome. Is Xolremdi suitable?"]
|
70 |
]
|
71 |
|
72 |
-
# === UI
|
73 |
def create_ui(agent):
|
74 |
with gr.Blocks(css=css) as demo:
|
75 |
gr.Markdown(DESCRIPTION)
|
@@ -82,16 +67,13 @@ def create_ui(agent):
|
|
82 |
multi_agent = gr.Checkbox(label="Enable Multi-agent Reasoning", value=False)
|
83 |
conversation_state = gr.State([])
|
84 |
|
85 |
-
chatbot = gr.Chatbot(
|
86 |
-
|
87 |
-
placeholder=PLACEHOLDER,
|
88 |
-
height=700,
|
89 |
-
type="messages",
|
90 |
-
show_copy_button=True
|
91 |
-
)
|
92 |
|
93 |
-
|
94 |
-
|
|
|
|
|
95 |
return agent.run_gradio_chat(
|
96 |
message=message,
|
97 |
history=history,
|
@@ -103,52 +85,32 @@ def create_ui(agent):
|
|
103 |
max_round=max_round
|
104 |
)
|
105 |
|
106 |
-
# ===
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
return agent.run_gradio_chat(
|
112 |
-
message=prompt,
|
113 |
-
history=new_history,
|
114 |
-
temperature=temperature,
|
115 |
-
max_new_tokens=max_new_tokens,
|
116 |
-
max_token=max_tokens,
|
117 |
-
call_agent=multi_agent,
|
118 |
-
conversation=conversation,
|
119 |
-
max_round=max_round
|
120 |
-
)
|
121 |
-
|
122 |
-
# === Retry button config
|
123 |
-
chatbot.retry(
|
124 |
-
handle_retry,
|
125 |
-
inputs=[chatbot, chatbot, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round],
|
126 |
-
outputs=chatbot
|
127 |
)
|
128 |
|
129 |
-
|
130 |
-
gr.ChatInterface(
|
131 |
fn=handle_chat,
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
137 |
examples=question_examples,
|
138 |
-
|
139 |
-
cache_examples=False,
|
140 |
-
fill_height=True,
|
141 |
-
fill_width=True,
|
142 |
-
stop_btn=True
|
143 |
)
|
144 |
|
145 |
gr.Markdown(LICENSE)
|
|
|
146 |
return demo
|
147 |
|
148 |
# === App start
|
149 |
if __name__ == "__main__":
|
150 |
freeze_support()
|
151 |
-
|
152 |
try:
|
153 |
agent = TxAgent(
|
154 |
model_name=model_name,
|
@@ -163,11 +125,11 @@ if __name__ == "__main__":
|
|
163 |
agent.init_model()
|
164 |
|
165 |
if not hasattr(agent, 'run_gradio_chat'):
|
166 |
-
raise AttributeError("TxAgent is missing `run_gradio_chat`. Make sure txagent.py is
|
167 |
|
168 |
demo = create_ui(agent)
|
169 |
demo.launch(show_error=True)
|
170 |
|
171 |
except Exception as e:
|
172 |
-
print(f"🚨
|
173 |
raise
|
|
|
6 |
import importlib
|
7 |
import inspect
|
8 |
|
9 |
+
# === Fix import path BEFORE loading TxAgent
|
10 |
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "src"))
|
11 |
|
12 |
+
# === Reload to avoid stale cache
|
13 |
import txagent.txagent
|
14 |
importlib.reload(txagent.txagent)
|
15 |
from txagent.txagent import TxAgent
|
16 |
|
17 |
+
# === Debug confirmation
|
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.dirname(os.path.abspath(__file__))
|
23 |
os.environ["MKL_THREADING_LAYER"] = "GNU"
|
24 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
25 |
|
26 |
+
# === UI Text
|
27 |
DESCRIPTION = '''
|
28 |
<div>
|
29 |
<h1 style="text-align: center;">TxAgent: An AI Agent for Therapeutic Reasoning Across a Universe of Tools</h1>
|
|
|
31 |
'''
|
32 |
INTRO = "Precision therapeutics require multimodal adaptive models..."
|
33 |
LICENSE = "DISCLAIMER: THIS WEBSITE DOES NOT PROVIDE MEDICAL ADVICE..."
|
34 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
css = """
|
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 50-year-old patient experiencing severe acute pain and considering the use of the newly approved medication, Journavx, how should the dosage be adjusted considering moderate hepatic impairment?"],
|
54 |
["A 30-year-old patient is on Prozac for depression and now diagnosed with WHIM syndrome. Is Xolremdi suitable?"]
|
55 |
]
|
56 |
|
57 |
+
# === UI
|
58 |
def create_ui(agent):
|
59 |
with gr.Blocks(css=css) as demo:
|
60 |
gr.Markdown(DESCRIPTION)
|
|
|
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=700)
|
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 |
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
|
|
|
|
|
|
|
|
|
105 |
)
|
106 |
|
107 |
gr.Markdown(LICENSE)
|
108 |
+
|
109 |
return demo
|
110 |
|
111 |
# === App start
|
112 |
if __name__ == "__main__":
|
113 |
freeze_support()
|
|
|
114 |
try:
|
115 |
agent = TxAgent(
|
116 |
model_name=model_name,
|
|
|
125 |
agent.init_model()
|
126 |
|
127 |
if not hasattr(agent, 'run_gradio_chat'):
|
128 |
+
raise AttributeError("TxAgent is missing `run_gradio_chat`. Make sure the correct txagent.py is used.")
|
129 |
|
130 |
demo = create_ui(agent)
|
131 |
demo.launch(show_error=True)
|
132 |
|
133 |
except Exception as e:
|
134 |
+
print(f"🚨 App failed to start: {e}")
|
135 |
raise
|