Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import torch
|
|
4 |
import logging
|
5 |
import numpy
|
6 |
import gradio as gr
|
7 |
-
import torch.serialization
|
8 |
from importlib.resources import files
|
9 |
from txagent import TxAgent
|
10 |
from tooluniverse import ToolUniverse
|
@@ -21,14 +20,7 @@ os.environ["MKL_THREADING_LAYER"] = "GNU"
|
|
21 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
22 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
23 |
|
24 |
-
#
|
25 |
-
torch.serialization.add_safe_globals([
|
26 |
-
numpy.core.multiarray._reconstruct,
|
27 |
-
numpy.ndarray,
|
28 |
-
numpy.dtype,
|
29 |
-
numpy.dtypes.Float32DType
|
30 |
-
])
|
31 |
-
|
32 |
CONFIG = {
|
33 |
"model_name": "mims-harvard/TxAgent-T1-Llama-3.1-8B",
|
34 |
"rag_model_name": "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
|
@@ -41,131 +33,135 @@ CONFIG = {
|
|
41 |
}
|
42 |
}
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
message
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
|
|
|
|
|
|
|
|
162 |
|
163 |
def main():
|
164 |
-
"""Main entry point for the application
|
165 |
try:
|
166 |
-
logger.info("Starting application
|
167 |
-
|
168 |
-
demo = create_demo(
|
169 |
|
170 |
logger.info("Launching Gradio interface...")
|
171 |
demo.launch(
|
|
|
4 |
import logging
|
5 |
import numpy
|
6 |
import gradio as gr
|
|
|
7 |
from importlib.resources import files
|
8 |
from txagent import TxAgent
|
9 |
from tooluniverse import ToolUniverse
|
|
|
20 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
21 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
22 |
|
23 |
+
# Configuration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
CONFIG = {
|
25 |
"model_name": "mims-harvard/TxAgent-T1-Llama-3.1-8B",
|
26 |
"rag_model_name": "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
|
|
|
33 |
}
|
34 |
}
|
35 |
|
36 |
+
class TxAgentApp:
|
37 |
+
def __init__(self):
|
38 |
+
self.agent = None
|
39 |
+
self.initialize_agent()
|
40 |
+
|
41 |
+
def initialize_agent(self):
|
42 |
+
"""Initialize the TxAgent with proper error handling"""
|
43 |
+
try:
|
44 |
+
self.prepare_tool_files()
|
45 |
+
logger.info("Initializing TxAgent...")
|
46 |
+
|
47 |
+
self.agent = TxAgent(
|
48 |
+
model_name=CONFIG["model_name"],
|
49 |
+
rag_model_name=CONFIG["rag_model_name"],
|
50 |
+
tool_files_dict=CONFIG["tool_files"],
|
51 |
+
force_finish=True,
|
52 |
+
enable_checker=True,
|
53 |
+
step_rag_num=10,
|
54 |
+
seed=42,
|
55 |
+
additional_default_tools=["DirectResponse", "RequireClarification"]
|
56 |
+
)
|
57 |
+
|
58 |
+
logger.info("Initializing model...")
|
59 |
+
self.agent.init_model()
|
60 |
+
logger.info("Agent initialization complete")
|
61 |
+
|
62 |
+
except Exception as e:
|
63 |
+
logger.error(f"Failed to initialize agent: {e}")
|
64 |
+
raise
|
65 |
+
|
66 |
+
def prepare_tool_files(self):
|
67 |
+
"""Prepare the tool files directory"""
|
68 |
+
try:
|
69 |
+
os.makedirs(os.path.join(current_dir, 'data'), exist_ok=True)
|
70 |
+
if not os.path.exists(CONFIG["tool_files"]["new_tool"]):
|
71 |
+
logger.info("Creating new_tool.json...")
|
72 |
+
tu = ToolUniverse()
|
73 |
+
tools = tu.get_all_tools() if hasattr(tu, "get_all_tools") else getattr(tu, "tools", [])
|
74 |
+
with open(CONFIG["tool_files"]["new_tool"], "w") as f:
|
75 |
+
json.dump(tools, f, indent=2)
|
76 |
+
except Exception as e:
|
77 |
+
logger.error(f"Failed to prepare tool files: {e}")
|
78 |
+
raise
|
79 |
+
|
80 |
+
def respond(self, msg, chat_history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
|
81 |
+
"""Handle user message and generate response"""
|
82 |
+
try:
|
83 |
+
if not isinstance(msg, str) or len(msg.strip()) <= 10:
|
84 |
+
return chat_history + [{"role": "assistant", "content": "Please provide a valid message longer than 10 characters."}]
|
85 |
+
|
86 |
+
message = msg.strip()
|
87 |
+
chat_history.append({"role": "user", "content": message})
|
88 |
+
formatted_history = [(m["role"], m["content"]) for m in chat_history if "role" in m and "content" in m]
|
89 |
+
|
90 |
+
logger.info(f"Processing message: {message[:100]}...")
|
91 |
+
|
92 |
+
response_generator = self.agent.run_gradio_chat(
|
93 |
+
message=message,
|
94 |
+
history=formatted_history,
|
95 |
+
temperature=temperature,
|
96 |
+
max_new_tokens=max_new_tokens,
|
97 |
+
max_token=max_tokens,
|
98 |
+
call_agent=multi_agent,
|
99 |
+
conversation=conversation,
|
100 |
+
max_round=max_round,
|
101 |
+
seed=42
|
102 |
+
)
|
103 |
+
|
104 |
+
collected = ""
|
105 |
+
for chunk in response_generator:
|
106 |
+
if isinstance(chunk, dict) and "content" in chunk:
|
107 |
+
collected += chunk["content"]
|
108 |
+
elif isinstance(chunk, str):
|
109 |
+
collected += chunk
|
110 |
+
elif chunk is not None:
|
111 |
+
collected += str(chunk)
|
112 |
+
|
113 |
+
chat_history.append({"role": "assistant", "content": collected or "No response generated."})
|
114 |
+
return chat_history
|
115 |
+
|
116 |
+
except Exception as e:
|
117 |
+
logger.error(f"Error in respond function: {e}")
|
118 |
+
chat_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
|
119 |
+
return chat_history
|
120 |
+
|
121 |
+
def create_demo(self):
|
122 |
+
"""Create and return the Gradio interface"""
|
123 |
+
with gr.Blocks(title="TxAgent", css=".gr-button { font-size: 18px !important; }") as demo:
|
124 |
+
gr.Markdown("# TxAgent - Biomedical AI Assistant")
|
125 |
+
|
126 |
+
with gr.Row():
|
127 |
+
with gr.Column(scale=3):
|
128 |
+
chatbot = gr.Chatbot(
|
129 |
+
label="Conversation",
|
130 |
+
height=600
|
131 |
+
)
|
132 |
+
msg = gr.Textbox(
|
133 |
+
label="Your question",
|
134 |
+
placeholder="Ask a biomedical question...",
|
135 |
+
lines=3
|
136 |
+
)
|
137 |
+
submit = gr.Button("Ask", variant="primary")
|
138 |
+
|
139 |
+
with gr.Column(scale=1):
|
140 |
+
temp = gr.Slider(0, 1, value=0.3, label="Temperature")
|
141 |
+
max_new_tokens = gr.Slider(128, 4096, value=1024, step=128, label="Max New Tokens")
|
142 |
+
max_tokens = gr.Slider(128, 81920, value=81920, step=1024, label="Max Total Tokens")
|
143 |
+
max_rounds = gr.Slider(1, 30, value=10, step=1, label="Max Rounds")
|
144 |
+
multi_agent = gr.Checkbox(label="Multi-Agent Mode", value=False)
|
145 |
+
clear_btn = gr.Button("Clear Chat")
|
146 |
+
|
147 |
+
submit.click(
|
148 |
+
self.respond,
|
149 |
+
inputs=[msg, chatbot, temp, max_new_tokens, max_tokens, multi_agent, gr.State([]), max_rounds],
|
150 |
+
outputs=[chatbot]
|
151 |
+
)
|
152 |
+
clear_btn.click(lambda: [], None, chatbot, queue=False)
|
153 |
+
|
154 |
+
# Add a dummy event to ensure the app stays alive
|
155 |
+
demo.load(lambda: None, None, None)
|
156 |
+
|
157 |
+
return demo
|
158 |
|
159 |
def main():
|
160 |
+
"""Main entry point for the application"""
|
161 |
try:
|
162 |
+
logger.info("Starting TxAgent application...")
|
163 |
+
app = TxAgentApp()
|
164 |
+
demo = app.create_demo()
|
165 |
|
166 |
logger.info("Launching Gradio interface...")
|
167 |
demo.launch(
|