File size: 6,747 Bytes
79fb3cd 4b0f1a8 b8c0ae3 79fb3cd 511fb62 9438945 511fb62 410d25f 79fb3cd 511fb62 12efdad ae94627 59ced24 a87f861 ae94627 59ced24 9438945 79fb3cd a87f861 12efdad 70839bb ae94627 410d25f ae94627 410d25f ae94627 bae0943 e3711be bae0943 6916257 bae0943 12efdad ae94627 79fb3cd e3711be 6916257 e3711be ae94627 6916257 ae94627 6916257 ae94627 6916257 ae94627 e3711be ae94627 e3711be ae94627 6916257 ae94627 6916257 ae94627 6916257 ae94627 79fb3cd 511fb62 ae94627 6916257 ae94627 6916257 ae94627 6916257 ae94627 6916257 511fb62 79fb3cd 511fb62 ae94627 6916257 ae94627 6916257 70839bb 6916257 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
import random
import os
import torch
import logging
import json
from importlib.resources import files
from txagent import TxAgent
from tooluniverse import ToolUniverse
import gradio as gr
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
current_dir = os.path.dirname(os.path.abspath(__file__))
os.environ["MKL_THREADING_LAYER"] = "GNU"
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# Configuration - Update paths as needed
CONFIG = {
"model_name": "mims-harvard/TxAgent-T1-Llama-3.1-8B",
"rag_model_name": "mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
"embedding_filename": "path_to_your_embeddings.pt", # Update this path
"tool_files": {
"opentarget": str(files('tooluniverse.data').joinpath('opentarget_tools.json')),
"fda_drug_label": str(files('tooluniverse.data').joinpath('fda_drug_labeling_tools.json')),
"special_tools": str(files('tooluniverse.data').joinpath('special_tools.json')),
"monarch": str(files('tooluniverse.data').joinpath('monarch_tools.json')),
"new_tool": os.path.join(current_dir, 'data', 'new_tool.json')
}
}
def safe_load_embeddings(filepath: str):
"""Handle embedding loading with fallbacks"""
try:
# Try with weights_only=True first
return torch.load(filepath, weights_only=True)
except Exception as e:
logger.warning(f"Secure load failed, trying without weights_only: {str(e)}")
try:
return torch.load(filepath, weights_only=False)
except Exception as e:
logger.error(f"Failed to load embeddings: {str(e)}")
return None
def get_tools_from_universe(tooluniverse):
"""Flexible tool extraction from ToolUniverse"""
if hasattr(tooluniverse, 'get_all_tools'):
return tooluniverse.get_all_tools()
elif hasattr(tooluniverse, 'tools'):
return tooluniverse.tools
elif hasattr(tooluniverse, 'list_tools'):
return tooluniverse.list_tools()
else:
logger.error("Could not find any tool access method in ToolUniverse")
# Try to load from files directly as fallback
tools = []
for tool_file in CONFIG["tool_files"].values():
if os.path.exists(tool_file):
with open(tool_file, 'r') as f:
tools.extend(json.load(f))
return tools if tools else None
def prepare_tool_files():
"""Ensure tool files exist and are populated"""
os.makedirs(os.path.join(current_dir, 'data'), exist_ok=True)
if not os.path.exists(CONFIG["tool_files"]["new_tool"]):
logger.info("Generating tool list...")
try:
tu = ToolUniverse()
tools = get_tools_from_universe(tu)
if tools:
with open(CONFIG["tool_files"]["new_tool"], "w") as f:
json.dump(tools, f, indent=2)
logger.info(f"Saved {len(tools)} tools")
else:
logger.error("No tools could be loaded")
except Exception as e:
logger.error(f"Tool file preparation failed: {str(e)}")
def create_agent():
"""Create and initialize the TxAgent with robust error handling"""
prepare_tool_files()
try:
agent = TxAgent(
model_name=CONFIG["model_name"],
rag_model_name=CONFIG["rag_model_name"],
tool_files_dict=CONFIG["tool_files"],
force_finish=True,
enable_checker=True,
step_rag_num=10,
seed=100,
additional_default_tools=['DirectResponse', 'RequireClarification']
)
agent.init_model()
return agent
except Exception as e:
logger.error(f"Agent creation failed: {str(e)}")
raise
def format_response(history, message):
"""Properly format responses for Gradio Chatbot"""
if isinstance(message, (str, dict)):
return history + [[None, str(message)]]
elif hasattr(message, '__iter__'):
full_response = ""
for chunk in message:
if isinstance(chunk, dict):
full_response += chunk.get("content", "")
else:
full_response += str(chunk)
return history + [[None, full_response]]
return history + [[None, str(message)]]
def create_demo(agent):
"""Create the Gradio interface with proper message handling"""
with gr.Blocks() as demo:
chatbot = gr.Chatbot(
height=800,
label='TxAgent',
show_copy_button=True,
type="messages" # Use the modern message format
)
msg = gr.Textbox(label="Input", placeholder="Type your question...")
clear = gr.ClearButton([msg, chatbot])
def respond(message, chat_history):
try:
# Convert Gradio history to agent format
agent_history = []
for user_msg, bot_msg in chat_history:
if user_msg:
agent_history.append({"role": "user", "content": user_msg})
if bot_msg:
agent_history.append({"role": "assistant", "content": bot_msg})
# Get response from agent
response = agent.run_gradio_chat(
agent_history + [{"role": "user", "content": message}],
temperature=0.3,
max_new_tokens=1024,
max_tokens=81920,
multi_agent=False,
conversation=[],
max_round=30
)
# Format the response properly
full_response = ""
for chunk in response:
if isinstance(chunk, dict):
full_response += chunk.get("content", "")
else:
full_response += str(chunk)
return chat_history + [(message, full_response)]
except Exception as e:
logger.error(f"Error in response handling: {str(e)}")
return chat_history + [(message, f"Error: {str(e)}")]
msg.submit(respond, [msg, chatbot], [chatbot])
clear.click(lambda: [], None, [chatbot])
return demo
def main():
"""Main application entry point"""
try:
agent = create_agent()
demo = create_demo(agent)
demo.launch(server_name="0.0.0.0", server_port=7860)
except Exception as e:
logger.error(f"Application failed to start: {str(e)}")
raise
if __name__ == "__main__":
main() |