Update app.py
Browse files
app.py
CHANGED
@@ -3,12 +3,14 @@ import logging
|
|
3 |
import os
|
4 |
import multiprocessing
|
5 |
|
|
|
6 |
logging.basicConfig(level=logging.INFO)
|
7 |
logger = logging.getLogger(__name__)
|
8 |
|
9 |
tx_app = None
|
10 |
TOOL_CACHE_PATH = "/home/user/.cache/tool_embeddings_done"
|
11 |
|
|
|
12 |
def respond(message, chat_history, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round):
|
13 |
global tx_app
|
14 |
if tx_app is None:
|
@@ -50,7 +52,7 @@ with gr.Blocks(title="TxAgent Biomedical Assistant") as app:
|
|
50 |
gr.Markdown("# π§ TxAgent Biomedical Assistant")
|
51 |
|
52 |
chatbot = gr.Chatbot(label="Conversation", height=600, type="messages")
|
53 |
-
msg = gr.Textbox(label="Your medical query", placeholder="Type
|
54 |
|
55 |
with gr.Row():
|
56 |
temp = gr.Slider(0, 1, value=0.3, label="Temperature")
|
@@ -75,25 +77,24 @@ with gr.Blocks(title="TxAgent Biomedical Assistant") as app:
|
|
75 |
chatbot
|
76 |
)
|
77 |
|
78 |
-
# === Safe model
|
79 |
if __name__ == "__main__":
|
80 |
multiprocessing.set_start_method("spawn", force=True)
|
81 |
|
|
|
82 |
from txagent import TxAgent
|
83 |
-
|
84 |
|
85 |
-
#
|
86 |
-
original_infer =
|
87 |
|
88 |
def patched_infer(self, *args, **kwargs):
|
89 |
original_infer(self, *args, **kwargs)
|
90 |
print("β
Patched: Skipping forced exit() after embedding.")
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
from importlib.resources import files
|
95 |
|
96 |
-
logger.info("π₯ Initializing
|
97 |
|
98 |
tool_files = {
|
99 |
"opentarget": str(files('tooluniverse.data').joinpath('opentarget_tools.json')),
|
@@ -122,6 +123,7 @@ if __name__ == "__main__":
|
|
122 |
additional_default_tools=["DirectResponse", "RequireClarification"]
|
123 |
)
|
124 |
|
|
|
125 |
if not os.path.exists(TOOL_CACHE_PATH):
|
126 |
tx_app.init_model()
|
127 |
os.makedirs(os.path.dirname(TOOL_CACHE_PATH), exist_ok=True)
|
@@ -129,3 +131,5 @@ if __name__ == "__main__":
|
|
129 |
f.write("done")
|
130 |
else:
|
131 |
tx_app.init_model(skip_tool_embedding=True)
|
|
|
|
|
|
3 |
import os
|
4 |
import multiprocessing
|
5 |
|
6 |
+
# Setup logging
|
7 |
logging.basicConfig(level=logging.INFO)
|
8 |
logger = logging.getLogger(__name__)
|
9 |
|
10 |
tx_app = None
|
11 |
TOOL_CACHE_PATH = "/home/user/.cache/tool_embeddings_done"
|
12 |
|
13 |
+
# Chatbot response function
|
14 |
def respond(message, chat_history, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round):
|
15 |
global tx_app
|
16 |
if tx_app is None:
|
|
|
52 |
gr.Markdown("# π§ TxAgent Biomedical Assistant")
|
53 |
|
54 |
chatbot = gr.Chatbot(label="Conversation", height=600, type="messages")
|
55 |
+
msg = gr.Textbox(label="Your medical query", placeholder="Type your biomedical question...", lines=3)
|
56 |
|
57 |
with gr.Row():
|
58 |
temp = gr.Slider(0, 1, value=0.3, label="Temperature")
|
|
|
77 |
chatbot
|
78 |
)
|
79 |
|
80 |
+
# === Safe model initialization ===
|
81 |
if __name__ == "__main__":
|
82 |
multiprocessing.set_start_method("spawn", force=True)
|
83 |
|
84 |
+
import tooluniverse
|
85 |
from txagent import TxAgent
|
86 |
+
from importlib.resources import files
|
87 |
|
88 |
+
# β
Patch ToolUniverse to prevent exit() after embedding
|
89 |
+
original_infer = tooluniverse.ToolUniverse.infer_tool_embeddings
|
90 |
|
91 |
def patched_infer(self, *args, **kwargs):
|
92 |
original_infer(self, *args, **kwargs)
|
93 |
print("β
Patched: Skipping forced exit() after embedding.")
|
94 |
|
95 |
+
tooluniverse.ToolUniverse.infer_tool_embeddings = patched_infer
|
|
|
|
|
96 |
|
97 |
+
logger.info("π₯ Initializing TxAgent...")
|
98 |
|
99 |
tool_files = {
|
100 |
"opentarget": str(files('tooluniverse.data').joinpath('opentarget_tools.json')),
|
|
|
123 |
additional_default_tools=["DirectResponse", "RequireClarification"]
|
124 |
)
|
125 |
|
126 |
+
# π Run full embedding once, then cache
|
127 |
if not os.path.exists(TOOL_CACHE_PATH):
|
128 |
tx_app.init_model()
|
129 |
os.makedirs(os.path.dirname(TOOL_CACHE_PATH), exist_ok=True)
|
|
|
131 |
f.write("done")
|
132 |
else:
|
133 |
tx_app.init_model(skip_tool_embedding=True)
|
134 |
+
|
135 |
+
logger.info("β
TxAgent ready.")
|