Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -148,32 +148,35 @@ def safe_calculator(expression: str) -> str:
|
|
148 |
|
149 |
# --- Basic Agent Definition ---
|
150 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
151 |
class HfAgentWrapper:
|
152 |
def __init__(self):
|
153 |
print("Initializing HfAgentWrapper...")
|
154 |
-
model_repo = "google/gemma-1.1-7b-it" #
|
155 |
print(f"Loading HfAgent with model: {model_repo}")
|
156 |
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
|
|
|
|
165 |
time_tool_obj = Tool(
|
166 |
-
name=
|
167 |
func=get_current_time_in_timezone,
|
168 |
description=get_current_time_in_timezone.__doc__
|
169 |
)
|
170 |
search_tool_obj = Tool(
|
171 |
-
name=
|
172 |
func=web_search,
|
173 |
description=web_search.__doc__
|
174 |
)
|
175 |
calculator_tool_obj = Tool(
|
176 |
-
name=
|
177 |
func=safe_calculator,
|
178 |
description=safe_calculator.__doc__
|
179 |
)
|
@@ -183,22 +186,21 @@ class HfAgentWrapper:
|
|
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
|
187 |
self.agent = HfAgent(
|
188 |
-
model_repo,
|
189 |
-
additional_tools=self.actual_tools_for_agent
|
|
|
190 |
)
|
191 |
print(f"HfAgent successfully instantiated with model {model_repo}.")
|
192 |
|
193 |
-
# --- Robust toolbox inspection
|
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]:
|
@@ -208,6 +210,7 @@ class HfAgentWrapper:
|
|
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()
|
@@ -220,14 +223,13 @@ class HfAgentWrapper:
|
|
220 |
print("self.agent does not have a 'toolbox' attribute (this would be unexpected).")
|
221 |
# --- End of robust toolbox inspection ---
|
222 |
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
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,7 +238,7 @@ class HfAgentWrapper:
|
|
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()
|
242 |
return f"Agent Error: Failed to process the question. Details: {e}"
|
|
|
148 |
|
149 |
# --- Basic Agent Definition ---
|
150 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
151 |
+
# --- Agent Definition using HfAgent ---
|
152 |
class HfAgentWrapper:
|
153 |
def __init__(self):
|
154 |
print("Initializing HfAgentWrapper...")
|
155 |
+
model_repo = "google/gemma-1.1-7b-it" # Correct Hugging Face Hub ID
|
156 |
print(f"Loading HfAgent with model: {model_repo}")
|
157 |
|
158 |
+
try:
|
159 |
+
# Ensure your tool functions have docstrings.
|
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
|
168 |
time_tool_obj = Tool(
|
169 |
+
name=get_current_time_in_timezone.__name__,
|
170 |
func=get_current_time_in_timezone,
|
171 |
description=get_current_time_in_timezone.__doc__
|
172 |
)
|
173 |
search_tool_obj = Tool(
|
174 |
+
name=web_search.__name__,
|
175 |
func=web_search,
|
176 |
description=web_search.__doc__
|
177 |
)
|
178 |
calculator_tool_obj = Tool(
|
179 |
+
name=safe_calculator.__name__,
|
180 |
func=safe_calculator,
|
181 |
description=safe_calculator.__doc__
|
182 |
)
|
|
|
186 |
tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
|
187 |
print(f"Prepared Tool objects with names: {tool_names_for_log} and descriptions.")
|
188 |
|
189 |
+
# Initialize HfAgent, now including trust_remote_code=True
|
190 |
self.agent = HfAgent(
|
191 |
+
model_repo,
|
192 |
+
additional_tools=self.actual_tools_for_agent,
|
193 |
+
trust_remote_code=True # <<< --- ENSURE THIS IS ADDED CORRECTLY WITH A COMMA BEFORE IT
|
194 |
)
|
195 |
print(f"HfAgent successfully instantiated with model {model_repo}.")
|
196 |
|
197 |
+
# --- Robust toolbox inspection ---
|
198 |
print(f"Inspecting self.agent.toolbox...")
|
|
|
199 |
if hasattr(self.agent, 'toolbox'):
|
200 |
toolbox_attr = self.agent.toolbox
|
201 |
print(f"Type of self.agent.toolbox: {type(toolbox_attr)}")
|
202 |
|
203 |
if isinstance(toolbox_attr, dict):
|
|
|
204 |
print(f"Toolbox is a dict. Keys: {list(toolbox_attr.keys())}")
|
205 |
tools_info_from_dict = {}
|
206 |
for key, value in list(toolbox_attr.items())[:5]:
|
|
|
210 |
tools_info_from_dict[key] = {'name_attr': tool_name_in_dict, 'type': str(type(value))}
|
211 |
print(f"Sample of toolbox dict content: {tools_info_from_dict}")
|
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()
|
|
|
223 |
print("self.agent does not have a 'toolbox' attribute (this would be unexpected).")
|
224 |
# --- End of robust toolbox inspection ---
|
225 |
|
226 |
+
except Exception as e: # This is the main except block for the __init__ try
|
227 |
+
print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
|
228 |
+
# import traceback # Already imported at the top of your file
|
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 |
return str(answer)
|
239 |
except Exception as e:
|
240 |
print(f"ERROR: HfAgent execution failed for question '{question[:50]}...': {e}")
|
241 |
+
# import traceback # Already imported at the top of your file
|
242 |
print("Full traceback of HfAgent execution error:")
|
243 |
traceback.print_exc()
|
244 |
return f"Agent Error: Failed to process the question. Details: {e}"
|