Update app.py
Browse files
app.py
CHANGED
@@ -3,36 +3,35 @@ import sys
|
|
3 |
import random
|
4 |
import gradio as gr
|
5 |
from multiprocessing import freeze_support
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "src"))
|
9 |
|
10 |
-
#
|
11 |
-
import importlib
|
12 |
import txagent.txagent
|
13 |
importlib.reload(txagent.txagent)
|
14 |
from txagent.txagent import TxAgent
|
15 |
|
16 |
-
#
|
17 |
-
import inspect
|
18 |
print(">>> TxAgent loaded from:", inspect.getfile(TxAgent))
|
19 |
print(">>> TxAgent has run_gradio_chat:", hasattr(TxAgent, "run_gradio_chat"))
|
|
|
20 |
|
21 |
-
# === Environment setup
|
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 constants
|
27 |
DESCRIPTION = '''
|
28 |
<div>
|
29 |
<h1 style="text-align: center;">TxAgent: An AI Agent for Therapeutic Reasoning Across a Universe of Tools</h1>
|
30 |
</div>
|
31 |
'''
|
32 |
-
|
33 |
INTRO = "Precision therapeutics require multimodal adaptive models..."
|
34 |
LICENSE = "DISCLAIMER: THIS WEBSITE DOES NOT PROVIDE MEDICAL ADVICE..."
|
35 |
-
|
36 |
PLACEHOLDER = '''
|
37 |
<div style="padding: 30px; text-align: center;">
|
38 |
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">TxAgent</h1>
|
@@ -40,7 +39,6 @@ PLACEHOLDER = '''
|
|
40 |
<p style="font-size: 18px;">Click retry π to see another answer.</p>
|
41 |
</div>
|
42 |
'''
|
43 |
-
|
44 |
css = """
|
45 |
h1 { text-align: center; }
|
46 |
#duplicate-button {
|
@@ -54,13 +52,12 @@ h1 { text-align: center; }
|
|
54 |
margin-bottom: 0px !important;
|
55 |
}
|
56 |
"""
|
57 |
-
|
58 |
chat_css = """
|
59 |
.gr-button { font-size: 20px !important; }
|
60 |
.gr-button svg { width: 32px !important; height: 32px !important; }
|
61 |
"""
|
62 |
|
63 |
-
# === Model and tool config
|
64 |
model_name = "mims-harvard/TxAgent-T1-Llama-3.1-8B"
|
65 |
rag_model_name = "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B"
|
66 |
new_tool_files = {
|
@@ -72,6 +69,7 @@ question_examples = [
|
|
72 |
["A 30-year-old patient is on Prozac for depression and now diagnosed with WHIM syndrome. Is Xolremdi suitable?"]
|
73 |
]
|
74 |
|
|
|
75 |
def create_ui(agent):
|
76 |
with gr.Blocks(css=css) as demo:
|
77 |
gr.Markdown(DESCRIPTION)
|
@@ -92,9 +90,9 @@ def create_ui(agent):
|
|
92 |
show_copy_button=True
|
93 |
)
|
94 |
|
95 |
-
# === Chat handler
|
96 |
async def handle_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
|
97 |
-
|
98 |
message=message,
|
99 |
history=history,
|
100 |
temperature=temperature,
|
@@ -104,14 +102,13 @@ def create_ui(agent):
|
|
104 |
conversation=conversation,
|
105 |
max_round=max_round
|
106 |
)
|
107 |
-
return response
|
108 |
|
109 |
-
# === Retry handler
|
110 |
async def handle_retry(history, retry_data, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
|
111 |
agent.update_parameters(seed=random.randint(0, 10000))
|
112 |
new_history = history[:retry_data.index]
|
113 |
prompt = history[retry_data.index]["content"]
|
114 |
-
return
|
115 |
message=prompt,
|
116 |
history=new_history,
|
117 |
temperature=temperature,
|
@@ -122,14 +119,14 @@ def create_ui(agent):
|
|
122 |
max_round=max_round
|
123 |
)
|
124 |
|
125 |
-
#
|
126 |
chatbot.retry(
|
127 |
handle_retry,
|
128 |
inputs=[chatbot, chatbot, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round],
|
129 |
outputs=chatbot
|
130 |
)
|
131 |
|
132 |
-
# ===
|
133 |
gr.ChatInterface(
|
134 |
fn=handle_chat,
|
135 |
chatbot=chatbot,
|
@@ -148,14 +145,14 @@ def create_ui(agent):
|
|
148 |
gr.Markdown(LICENSE)
|
149 |
return demo
|
150 |
|
|
|
151 |
if __name__ == "__main__":
|
152 |
freeze_support()
|
153 |
-
|
154 |
try:
|
155 |
-
# Initialize agent
|
156 |
agent = TxAgent(
|
157 |
-
model_name,
|
158 |
-
rag_model_name,
|
159 |
tool_files_dict=new_tool_files,
|
160 |
force_finish=True,
|
161 |
enable_checker=True,
|
@@ -164,15 +161,13 @@ if __name__ == "__main__":
|
|
164 |
additional_default_tools=["DirectResponse", "RequireClarification"]
|
165 |
)
|
166 |
agent.init_model()
|
167 |
-
|
168 |
-
# Verify the agent has the required method
|
169 |
if not hasattr(agent, 'run_gradio_chat'):
|
170 |
-
raise AttributeError("
|
171 |
-
|
172 |
-
# Create and launch UI
|
173 |
demo = create_ui(agent)
|
174 |
demo.launch(show_error=True)
|
175 |
-
|
176 |
except Exception as e:
|
177 |
-
print(f"Application failed to start: {
|
178 |
-
raise
|
|
|
3 |
import random
|
4 |
import gradio as gr
|
5 |
from multiprocessing import freeze_support
|
6 |
+
import importlib
|
7 |
+
import inspect
|
8 |
|
9 |
+
# === Fix: Add src directory to import path BEFORE importing anything else
|
10 |
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "src"))
|
11 |
|
12 |
+
# === Reload to avoid stale imports in Hugging Face
|
|
|
13 |
import txagent.txagent
|
14 |
importlib.reload(txagent.txagent)
|
15 |
from txagent.txagent import TxAgent
|
16 |
|
17 |
+
# === Debug: Confirm we're loading the right class
|
|
|
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 |
+
# === Environment setup
|
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 constants
|
28 |
DESCRIPTION = '''
|
29 |
<div>
|
30 |
<h1 style="text-align: center;">TxAgent: An AI Agent for Therapeutic Reasoning Across a Universe of Tools</h1>
|
31 |
</div>
|
32 |
'''
|
|
|
33 |
INTRO = "Precision therapeutics require multimodal adaptive models..."
|
34 |
LICENSE = "DISCLAIMER: THIS WEBSITE DOES NOT PROVIDE MEDICAL ADVICE..."
|
|
|
35 |
PLACEHOLDER = '''
|
36 |
<div style="padding: 30px; text-align: center;">
|
37 |
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">TxAgent</h1>
|
|
|
39 |
<p style="font-size: 18px;">Click retry π to see another answer.</p>
|
40 |
</div>
|
41 |
'''
|
|
|
42 |
css = """
|
43 |
h1 { text-align: center; }
|
44 |
#duplicate-button {
|
|
|
52 |
margin-bottom: 0px !important;
|
53 |
}
|
54 |
"""
|
|
|
55 |
chat_css = """
|
56 |
.gr-button { font-size: 20px !important; }
|
57 |
.gr-button svg { width: 32px !important; height: 32px !important; }
|
58 |
"""
|
59 |
|
60 |
+
# === Model and tool config
|
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 = {
|
|
|
69 |
["A 30-year-old patient is on Prozac for depression and now diagnosed with WHIM syndrome. Is Xolremdi suitable?"]
|
70 |
]
|
71 |
|
72 |
+
# === UI constructor
|
73 |
def create_ui(agent):
|
74 |
with gr.Blocks(css=css) as demo:
|
75 |
gr.Markdown(DESCRIPTION)
|
|
|
90 |
show_copy_button=True
|
91 |
)
|
92 |
|
93 |
+
# === Chat handler
|
94 |
async def handle_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
|
95 |
+
return agent.run_gradio_chat(
|
96 |
message=message,
|
97 |
history=history,
|
98 |
temperature=temperature,
|
|
|
102 |
conversation=conversation,
|
103 |
max_round=max_round
|
104 |
)
|
|
|
105 |
|
106 |
+
# === Retry handler
|
107 |
async def handle_retry(history, retry_data, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
|
108 |
agent.update_parameters(seed=random.randint(0, 10000))
|
109 |
new_history = history[:retry_data.index]
|
110 |
prompt = history[retry_data.index]["content"]
|
111 |
+
return agent.run_gradio_chat(
|
112 |
message=prompt,
|
113 |
history=new_history,
|
114 |
temperature=temperature,
|
|
|
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 |
+
# === Main interface
|
130 |
gr.ChatInterface(
|
131 |
fn=handle_chat,
|
132 |
chatbot=chatbot,
|
|
|
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,
|
155 |
+
rag_model_name=rag_model_name,
|
156 |
tool_files_dict=new_tool_files,
|
157 |
force_finish=True,
|
158 |
enable_checker=True,
|
|
|
161 |
additional_default_tools=["DirectResponse", "RequireClarification"]
|
162 |
)
|
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 up to date.")
|
167 |
+
|
|
|
168 |
demo = create_ui(agent)
|
169 |
demo.launch(show_error=True)
|
170 |
+
|
171 |
except Exception as e:
|
172 |
+
print(f"π¨ Application failed to start: {e}")
|
173 |
+
raise
|