DragonProgrammer commited on
Commit
449efb7
·
verified ·
1 Parent(s): bcbd180

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -18
app.py CHANGED
@@ -147,26 +147,22 @@ def safe_calculator(expression: str) -> str:
147
 
148
  # --- Basic Agent Definition ---
149
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
150
- # --- Agent Definition using HfAgent ---
151
  class HfAgentWrapper:
152
  def __init__(self):
153
  print("Initializing HfAgentWrapper...")
154
- model_repo = "google/gemma-1.1-7b-it" # Consider Mixtral for more power if resources allow
155
-
156
  print(f"Loading HfAgent with model: {model_repo}")
 
157
  try:
158
  # Ensure your tool functions have docstrings, as they are used for the description.
159
- # (Your current tool functions look like they do, which is good)
160
- if not get_current_time_in_timezone.__doc__:
161
  raise ValueError("Tool 'get_current_time_in_timezone' is missing a docstring.")
162
- if not web_search.__doc__:
163
  raise ValueError("Tool 'web_search' is missing a docstring.")
164
- if not safe_calculator.__doc__:
165
  raise ValueError("Tool 'safe_calculator' is missing a docstring.")
166
 
167
- # Manually wrap your functions into Tool objects using the Tool constructor
168
- # The constructor for Tool in this version is typically:
169
- # Tool(name: str, func: Callable, description: str)
170
  time_tool_obj = Tool(
171
  name=get_current_time_in_timezone.__name__,
172
  func=get_current_time_in_timezone,
@@ -183,28 +179,56 @@ class HfAgentWrapper:
183
  description=safe_calculator.__doc__
184
  )
185
 
186
- # This is the list of Tool objects that HfAgent will receive
187
  self.actual_tools_for_agent = [time_tool_obj, search_tool_obj, calculator_tool_obj]
188
 
189
  tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
190
  print(f"Prepared Tool objects with names: {tool_names_for_log} and descriptions.")
191
 
 
192
  self.agent = HfAgent(model_repo, additional_tools=self.actual_tools_for_agent)
193
-
194
- print(f"HfAgent loaded successfully with model {model_repo}.")
195
- # HfAgent's toolbox will also have its own representation of these tools
196
- print(f"HfAgent internal toolbox tool names: {[tool.name for tool in self.agent.toolbox.tools()]}")
197
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
  except Exception as e:
200
  print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
201
- import traceback
202
  print("Full traceback of HfAgent initialization error:")
203
  traceback.print_exc()
204
  raise RuntimeError(f"HfAgent initialization failed: {e}") from e
205
 
206
  def __call__(self, question: str) -> str:
207
- # ... (your __call__ method remains the same) ...
208
  print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
209
  try:
210
  answer = self.agent.run(question)
@@ -212,6 +236,10 @@ class HfAgentWrapper:
212
  return str(answer)
213
  except Exception as e:
214
  print(f"ERROR: HfAgent execution failed for question '{question[:50]}...': {e}")
 
 
 
 
215
  return f"Agent Error: Failed to process the question. Details: {e}"
216
 
217
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
147
 
148
  # --- Basic Agent Definition ---
149
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
150
  class HfAgentWrapper:
151
  def __init__(self):
152
  print("Initializing HfAgentWrapper...")
153
+ model_repo = "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__:
161
  raise ValueError("Tool 'web_search' is missing a docstring.")
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,
 
179
  description=safe_calculator.__doc__
180
  )
181
 
 
182
  self.actual_tools_for_agent = [time_tool_obj, search_tool_obj, calculator_tool_obj]
183
 
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(model_repo, additional_tools=self.actual_tools_for_agent)
189
+ print(f"HfAgent successfully instantiated with model {model_repo}.")
190
+
191
+ # --- New robust way to inspect the toolbox ---
192
+ print(f"Inspecting self.agent.toolbox...")
193
+ if hasattr(self.agent, 'toolbox'):
194
+ toolbox_attr = self.agent.toolbox
195
+ print(f"Type of self.agent.toolbox: {type(toolbox_attr)}")
196
+
197
+ if isinstance(toolbox_attr, dict):
198
+ print(f"Toolbox is a dict. Keys: {list(toolbox_attr.keys())}")
199
+ tools_info_from_dict = {}
200
+ # Inspect a few items if it's a large dictionary
201
+ for key, value in list(toolbox_attr.items())[:5]:
202
+ tool_name_in_dict = key # Default to key if value has no 'name'
203
+ if hasattr(value, 'name'):
204
+ tool_name_in_dict = value.name
205
+ elif isinstance(value, dict) and 'name' in value:
206
+ tool_name_in_dict = value['name']
207
+ tools_info_from_dict[key] = {'name_attr': tool_name_in_dict, 'type': str(type(value))}
208
+ print(f"Sample of toolbox dict content: {tools_info_from_dict}")
209
+ # Compare with the tools we intended to pass
210
+ print(f"Names of tools we passed to HfAgent: {[tool.name for tool in self.actual_tools_for_agent]}")
211
+
212
+ elif hasattr(toolbox_attr, 'tools') and callable(toolbox_attr.tools):
213
+ try:
214
+ tools_in_toolbox = toolbox_attr.tools()
215
+ print(f"Toolbox has .tools() method. Tool names: {[tool.name for tool in tools_in_toolbox if hasattr(tool, 'name')]}")
216
+ except Exception as e_toolbox_call:
217
+ print(f"Error calling or processing self.agent.toolbox.tools(): {e_toolbox_call}")
218
+ else:
219
+ print(f"Toolbox is of type {type(toolbox_attr)} but not a dict or recognized Toolbox object for this debug print.")
220
+ else:
221
+ print("self.agent does not have a 'toolbox' attribute (this would be unexpected).")
222
+ # --- End of new robust toolbox inspection ---
223
 
224
  except Exception as e:
225
  print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
226
+ import traceback # Import for full traceback
227
  print("Full traceback of HfAgent initialization error:")
228
  traceback.print_exc()
229
  raise RuntimeError(f"HfAgent initialization failed: {e}") from e
230
 
231
  def __call__(self, question: str) -> str:
 
232
  print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
233
  try:
234
  answer = self.agent.run(question)
 
236
  return str(answer)
237
  except Exception as e:
238
  print(f"ERROR: HfAgent execution failed for question '{question[:50]}...': {e}")
239
+ # It might be helpful to see the full traceback for agent execution errors too
240
+ import traceback
241
+ print("Full traceback of HfAgent execution error:")
242
+ traceback.print_exc()
243
  return f"Agent Error: Failed to process the question. Details: {e}"
244
 
245
  def run_and_submit_all( profile: gr.OAuthProfile | None):