DragonProgrammer commited on
Commit
eca2559
·
verified ·
1 Parent(s): c146d78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -18
app.py CHANGED
@@ -8,6 +8,7 @@ import pytz
8
  import math
9
  import re
10
  import requests
 
11
 
12
  import sys
13
  print(f"--- Python version: {sys.version} ---")
@@ -150,11 +151,10 @@ def safe_calculator(expression: str) -> str:
150
  class HfAgentWrapper:
151
  def __init__(self):
152
  print("Initializing HfAgentWrapper...")
153
- model_repo = "https://google/gemma-1.1-7b-it" # Or your preferred model
154
  print(f"Loading HfAgent with model: {model_repo}")
155
 
156
  try:
157
- # Ensure your tool functions have docstrings, as they are used for the description.
158
  if not get_current_time_in_timezone.__doc__:
159
  raise ValueError("Tool 'get_current_time_in_timezone' is missing a docstring.")
160
  if not web_search.__doc__:
@@ -162,7 +162,6 @@ class HfAgentWrapper:
162
  if not safe_calculator.__doc__:
163
  raise ValueError("Tool 'safe_calculator' is missing a docstring.")
164
 
165
- # Manually wrap your functions into Tool objects using the Tool constructor directly
166
  time_tool_obj = Tool(
167
  name=get_current_time_in_timezone.__name__,
168
  func=get_current_time_in_timezone,
@@ -184,33 +183,31 @@ class HfAgentWrapper:
184
  tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
185
  print(f"Prepared Tool objects with names: {tool_names_for_log} and descriptions.")
186
 
187
- # Initialize HfAgent with the list of Tool objects
188
  self.agent = HfAgent(
189
- model_repo,
190
- tools=[], # Attempt to provide an empty list for default tools
191
  additional_tools=self.actual_tools_for_agent
192
  )
193
- # --- New robust way to inspect the toolbox ---
 
 
194
  print(f"Inspecting self.agent.toolbox...")
 
195
  if hasattr(self.agent, 'toolbox'):
196
  toolbox_attr = self.agent.toolbox
197
  print(f"Type of self.agent.toolbox: {type(toolbox_attr)}")
198
 
199
  if isinstance(toolbox_attr, dict):
 
200
  print(f"Toolbox is a dict. Keys: {list(toolbox_attr.keys())}")
201
  tools_info_from_dict = {}
202
- # Inspect a few items if it's a large dictionary
203
  for key, value in list(toolbox_attr.items())[:5]:
204
- tool_name_in_dict = key # Default to key if value has no 'name'
205
- if hasattr(value, 'name'):
206
- tool_name_in_dict = value.name
207
- elif isinstance(value, dict) and 'name' in value:
208
- tool_name_in_dict = value['name']
209
  tools_info_from_dict[key] = {'name_attr': tool_name_in_dict, 'type': str(type(value))}
210
  print(f"Sample of toolbox dict content: {tools_info_from_dict}")
211
- # Compare with the tools we intended to pass
212
  print(f"Names of tools we passed to HfAgent: {[tool.name for tool in self.actual_tools_for_agent]}")
213
-
214
  elif hasattr(toolbox_attr, 'tools') and callable(toolbox_attr.tools):
215
  try:
216
  tools_in_toolbox = toolbox_attr.tools()
@@ -221,15 +218,16 @@ class HfAgentWrapper:
221
  print(f"Toolbox is of type {type(toolbox_attr)} but not a dict or recognized Toolbox object for this debug print.")
222
  else:
223
  print("self.agent does not have a 'toolbox' attribute (this would be unexpected).")
224
- # --- End of new robust toolbox inspection ---
225
 
226
  except Exception as e:
227
  print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
228
- import traceback # Import for full traceback
229
  print("Full traceback of HfAgent initialization error:")
230
  traceback.print_exc()
231
  raise RuntimeError(f"HfAgent initialization failed: {e}") from e
232
 
 
233
  def __call__(self, question: str) -> str:
234
  print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
235
  try:
@@ -238,7 +236,6 @@ class HfAgentWrapper:
238
  return str(answer)
239
  except Exception as e:
240
  print(f"ERROR: HfAgent execution failed for question '{question[:50]}...': {e}")
241
- # It might be helpful to see the full traceback for agent execution errors too
242
  import traceback
243
  print("Full traceback of HfAgent execution error:")
244
  traceback.print_exc()
 
8
  import math
9
  import re
10
  import requests
11
+ import traceback
12
 
13
  import sys
14
  print(f"--- Python version: {sys.version} ---")
 
151
  class HfAgentWrapper:
152
  def __init__(self):
153
  print("Initializing HfAgentWrapper...")
154
+ model_repo = "google/gemma-1.1-7b-it" # <<< --- CORRECTED MODEL REPO ID
155
  print(f"Loading HfAgent with model: {model_repo}")
156
 
157
  try:
 
158
  if not get_current_time_in_timezone.__doc__:
159
  raise ValueError("Tool 'get_current_time_in_timezone' is missing a docstring.")
160
  if not web_search.__doc__:
 
162
  if not safe_calculator.__doc__:
163
  raise ValueError("Tool 'safe_calculator' is missing a docstring.")
164
 
 
165
  time_tool_obj = Tool(
166
  name=get_current_time_in_timezone.__name__,
167
  func=get_current_time_in_timezone,
 
183
  tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
184
  print(f"Prepared Tool objects with names: {tool_names_for_log} and descriptions.")
185
 
186
+ # Initialize HfAgent with the corrected model_repo and without the 'tools=[]' argument
187
  self.agent = HfAgent(
188
+ model_repo, # Uses the corrected model_repo string
 
189
  additional_tools=self.actual_tools_for_agent
190
  )
191
+ print(f"HfAgent successfully instantiated with model {model_repo}.")
192
+
193
+ # --- Robust toolbox inspection (keep this as it is) ---
194
  print(f"Inspecting self.agent.toolbox...")
195
+ # ... (the rest of your toolbox inspection code from the previous version) ...
196
  if hasattr(self.agent, 'toolbox'):
197
  toolbox_attr = self.agent.toolbox
198
  print(f"Type of self.agent.toolbox: {type(toolbox_attr)}")
199
 
200
  if isinstance(toolbox_attr, dict):
201
+ # ... (dictionary inspection logic) ...
202
  print(f"Toolbox is a dict. Keys: {list(toolbox_attr.keys())}")
203
  tools_info_from_dict = {}
 
204
  for key, value in list(toolbox_attr.items())[:5]:
205
+ tool_name_in_dict = key
206
+ if hasattr(value, 'name'): tool_name_in_dict = value.name
207
+ elif isinstance(value, dict) and 'name' in value: tool_name_in_dict = value['name']
 
 
208
  tools_info_from_dict[key] = {'name_attr': tool_name_in_dict, 'type': str(type(value))}
209
  print(f"Sample of toolbox dict content: {tools_info_from_dict}")
 
210
  print(f"Names of tools we passed to HfAgent: {[tool.name for tool in self.actual_tools_for_agent]}")
 
211
  elif hasattr(toolbox_attr, 'tools') and callable(toolbox_attr.tools):
212
  try:
213
  tools_in_toolbox = toolbox_attr.tools()
 
218
  print(f"Toolbox is of type {type(toolbox_attr)} but not a dict or recognized Toolbox object for this debug print.")
219
  else:
220
  print("self.agent does not have a 'toolbox' attribute (this would be unexpected).")
221
+ # --- End of robust toolbox inspection ---
222
 
223
  except Exception as e:
224
  print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
225
+ import traceback
226
  print("Full traceback of HfAgent initialization error:")
227
  traceback.print_exc()
228
  raise RuntimeError(f"HfAgent initialization failed: {e}") from e
229
 
230
+ # __call__ method remains the same
231
  def __call__(self, question: str) -> str:
232
  print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
233
  try:
 
236
  return str(answer)
237
  except Exception as e:
238
  print(f"ERROR: HfAgent execution failed for question '{question[:50]}...': {e}")
 
239
  import traceback
240
  print("Full traceback of HfAgent execution error:")
241
  traceback.print_exc()