Ali2206 commited on
Commit
73810ec
·
verified ·
1 Parent(s): da7f195

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -17
app.py CHANGED
@@ -58,7 +58,6 @@ def extract_text_from_excel(file_obj: Union[str, Dict[str, Any]]) -> str:
58
  """Handle Gradio file upload object which is a dictionary with 'name' and other keys"""
59
  all_text = []
60
  try:
61
- # Handle Gradio file upload object
62
  if isinstance(file_obj, dict) and 'name' in file_obj:
63
  file_path = file_obj['name']
64
  elif isinstance(file_obj, str):
@@ -120,6 +119,37 @@ Please analyze these clinical notes and provide:
120
  Provide a structured response with clear medical reasoning.
121
  """
122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  def init_agent() -> TxAgent:
124
  tool_path = os.path.join(tool_cache_dir, "new_tool.json")
125
  logger.info(f"Checking for tool file at: {tool_path}")
@@ -130,32 +160,34 @@ def init_agent() -> TxAgent:
130
  "name": "new_tool",
131
  "description": "Default tool configuration",
132
  "version": "1.0",
133
- "tools": []
 
 
134
  }
135
  logger.info(f"Creating default tool file at: {tool_path}")
136
  with open(tool_path, 'w') as f:
137
  json.dump(default_tool, f)
138
 
139
- # Validate tool file
140
- try:
141
- with open(tool_path, 'r') as f:
142
- tool_data = json.load(f)
143
- logger.info(f"Tool file contents: {tool_data}")
144
- if not isinstance(tool_data, dict):
145
- raise ValueError(f"Tool file {tool_path} must contain a JSON object")
146
- if 'tools' not in tool_data or not isinstance(tool_data['tools'], (list, dict)):
147
- raise ValueError(f"'tools' field in {tool_path} must be a list or dict, got {type(tool_data.get('tools'))}")
148
- except Exception as e:
149
- logger.error(f"Error validating tool file {tool_path}: {str(e)}")
150
- raise
151
-
152
  # Initialize TxAgent
153
  try:
154
- logger.info("Initializing TxAgent with tool_files_dict: {'new_tool': tool_path}")
155
  agent = TxAgent(
156
  model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
157
  rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
158
- tool_files_dict={"new_tool": tool_path},
159
  force_finish=True,
160
  enable_checker=True,
161
  step_rag_num=4,
 
58
  """Handle Gradio file upload object which is a dictionary with 'name' and other keys"""
59
  all_text = []
60
  try:
 
61
  if isinstance(file_obj, dict) and 'name' in file_obj:
62
  file_path = file_obj['name']
63
  elif isinstance(file_obj, str):
 
119
  Provide a structured response with clear medical reasoning.
120
  """
121
 
122
+ def validate_tool_file(tool_name: str, tool_path: str) -> None:
123
+ """Validate the structure of a tool JSON file."""
124
+ try:
125
+ if not os.path.exists(tool_path):
126
+ raise FileNotFoundError(f"Tool file not found: {tool_path}")
127
+
128
+ with open(tool_path, 'r') as f:
129
+ tool_data = json.load(f)
130
+
131
+ logger.info(f"Contents of {tool_name} ({tool_path}): {tool_data}")
132
+
133
+ if isinstance(tool_data, list):
134
+ for item in tool_data:
135
+ if not isinstance(item, dict) or 'name' not in item:
136
+ raise ValueError(f"Invalid tool format in {tool_name}: each item must be a dict with a 'name' key, got {item}")
137
+ elif isinstance(tool_data, dict):
138
+ if 'tools' in tool_data:
139
+ if not isinstance(tool_data['tools'], list):
140
+ raise ValueError(f"'tools' field in {tool_name} must be a list, got {type(tool_data['tools'])}")
141
+ for item in tool_data['tools']:
142
+ if not isinstance(item, dict) or 'name' not in item:
143
+ raise ValueError(f"Invalid tool format in {tool_name}: each tool must be a dict with a 'name' key, got {item}")
144
+ else:
145
+ if 'name' not in tool_data:
146
+ raise ValueError(f"Invalid tool format in {tool_name}: dict must have a 'name' key or 'tools' field, got {tool_data}")
147
+ else:
148
+ raise ValueError(f"Invalid tool file {tool_name}: must be a list or dict, got {type(tool_data)}")
149
+ except Exception as e:
150
+ logger.error(f"Error validating tool file {tool_name} ({tool_path}): {str(e)}")
151
+ raise
152
+
153
  def init_agent() -> TxAgent:
154
  tool_path = os.path.join(tool_cache_dir, "new_tool.json")
155
  logger.info(f"Checking for tool file at: {tool_path}")
 
160
  "name": "new_tool",
161
  "description": "Default tool configuration",
162
  "version": "1.0",
163
+ "tools": [
164
+ {"name": "dummy_tool", "description": "Dummy tool for testing", "version": "1.0"}
165
+ ]
166
  }
167
  logger.info(f"Creating default tool file at: {tool_path}")
168
  with open(tool_path, 'w') as f:
169
  json.dump(default_tool, f)
170
 
171
+ # Define tool files
172
+ tool_files_dict = {
173
+ 'opentarget': '/home/user/.pyenv/versions/3.10.17/lib/python3.10/site-packages/tooluniverse/data/opentarget_tools.json',
174
+ 'fda_drug_label': '/home/user/.pyenv/versions/3.10.17/lib/python3.10/site-packages/tooluniverse/data/fda_drug_labeling_tools.json',
175
+ 'special_tools': '/home/user/.pyenv/versions/3.10.17/lib/python3.10/site-packages/tooluniverse/data/special_tools.json',
176
+ 'monarch': '/home/user/.pyenv/versions/3.10.17/lib/python3.10/site-packages/tooluniverse/data/monarch_tools.json',
177
+ 'new_tool': tool_path
178
+ }
179
+
180
+ # Validate all tool files
181
+ for tool_name, tool_path in tool_files_dict.items():
182
+ validate_tool_file(tool_name, tool_path)
183
+
184
  # Initialize TxAgent
185
  try:
186
+ logger.info(f"Initializing TxAgent with tool_files_dict: {tool_files_dict}")
187
  agent = TxAgent(
188
  model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
189
  rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
190
+ tool_files_dict=tool_files_dict,
191
  force_finish=True,
192
  enable_checker=True,
193
  step_rag_num=4,