Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -155,74 +155,97 @@ def safe_calculator(expression: str) -> str:
|
|
155 |
class HfAgentWrapper:
|
156 |
def __init__(self):
|
157 |
print("Initializing HfAgentWrapper...")
|
158 |
-
model_id_or_path = "bigcode/starcoderbase-1b"
|
159 |
-
|
160 |
try:
|
161 |
-
print(f"Attempting to initialize HfAgent with model: {model_id_or_path}")
|
162 |
-
|
163 |
hf_auth_token = os.getenv("HF_AUTH_TOKEN")
|
164 |
if not hf_auth_token:
|
165 |
-
|
166 |
-
|
167 |
-
|
|
|
168 |
else:
|
169 |
print(f"HF_AUTH_TOKEN found (length: {len(hf_auth_token)}), will pass it to HfAgent.")
|
170 |
|
171 |
# --- Ensure Tool names are correct ---
|
172 |
-
if not get_current_time_in_timezone.__doc__:
|
173 |
-
|
174 |
-
if not
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
self.actual_tools_for_agent = [time_tool_obj, search_tool_obj, calculator_tool_obj]
|
|
|
180 |
tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
|
181 |
-
|
|
|
182 |
# --- End Tool Name Check ---
|
183 |
|
184 |
# Let HfAgent create its own pipeline internally.
|
185 |
-
# Pass
|
186 |
-
#
|
187 |
self.agent = HfAgent(
|
188 |
-
|
189 |
additional_tools=self.actual_tools_for_agent,
|
190 |
hf_token=hf_auth_token # HfAgent uses this for its internal LLMProxy
|
191 |
)
|
192 |
print(f"HfAgent successfully instantiated for model {model_id_or_path}.")
|
193 |
|
194 |
-
#
|
195 |
-
|
196 |
-
if
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
|
|
|
|
|
|
|
|
|
|
216 |
|
217 |
except Exception as e:
|
218 |
print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
|
|
|
219 |
print("Full traceback of HfAgent initialization error:")
|
220 |
traceback.print_exc()
|
221 |
raise RuntimeError(f"HfAgent initialization failed: {e}") from e
|
222 |
|
223 |
# __call__ method remains the same
|
224 |
def __call__(self, question: str) -> str:
|
225 |
-
# ... (your existing __call__ method) ...
|
226 |
print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
|
227 |
try:
|
228 |
answer = self.agent.run(question)
|
@@ -230,6 +253,7 @@ class HfAgentWrapper:
|
|
230 |
return str(answer)
|
231 |
except Exception as e:
|
232 |
print(f"ERROR: HfAgent execution failed for question '{question[:50]}...': {e}")
|
|
|
233 |
print("Full traceback of HfAgent execution error:")
|
234 |
traceback.print_exc()
|
235 |
return f"Agent Error: Failed to process the question. Details: {e}"
|
|
|
155 |
class HfAgentWrapper:
|
156 |
def __init__(self):
|
157 |
print("Initializing HfAgentWrapper...")
|
158 |
+
model_id_or_path = "bigcode/starcoderbase-1b" # Using a model compatible with transformers 4.36.0
|
159 |
+
|
160 |
try:
|
161 |
+
print(f"Attempting to initialize HfAgent directly with model: {model_id_or_path}")
|
162 |
+
|
163 |
hf_auth_token = os.getenv("HF_AUTH_TOKEN")
|
164 |
if not hf_auth_token:
|
165 |
+
# Starcoderbase might be gated or require login for some operations/configs
|
166 |
+
print("WARNING: HF_AUTH_TOKEN secret not found. This might cause issues if the model requires full authentication.")
|
167 |
+
# For now, we'll let it proceed and see if HfAgent can fetch it without an explicit token here
|
168 |
+
# if it's publicly accessible enough. If not, this will error out.
|
169 |
else:
|
170 |
print(f"HF_AUTH_TOKEN found (length: {len(hf_auth_token)}), will pass it to HfAgent.")
|
171 |
|
172 |
# --- Ensure Tool names are correct ---
|
173 |
+
if not get_current_time_in_timezone.__doc__:
|
174 |
+
raise ValueError("Tool 'get_current_time_in_timezone' is missing a docstring.")
|
175 |
+
if not web_search.__doc__:
|
176 |
+
raise ValueError("Tool 'web_search' is missing a docstring.")
|
177 |
+
if not safe_calculator.__doc__:
|
178 |
+
raise ValueError("Tool 'safe_calculator' is missing a docstring.")
|
179 |
+
|
180 |
+
time_tool_obj = Tool(
|
181 |
+
name=get_current_time_in_timezone.__name__,
|
182 |
+
func=get_current_time_in_timezone,
|
183 |
+
description=get_current_time_in_timezone.__doc__
|
184 |
+
)
|
185 |
+
search_tool_obj = Tool(
|
186 |
+
name=web_search.__name__,
|
187 |
+
func=web_search,
|
188 |
+
description=web_search.__doc__
|
189 |
+
)
|
190 |
+
calculator_tool_obj = Tool(
|
191 |
+
name=safe_calculator.__name__,
|
192 |
+
func=safe_calculator,
|
193 |
+
description=safe_calculator.__doc__
|
194 |
+
)
|
195 |
self.actual_tools_for_agent = [time_tool_obj, search_tool_obj, calculator_tool_obj]
|
196 |
+
|
197 |
tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
|
198 |
+
# This print MUST show your actual tool names, not empty strings
|
199 |
+
print(f"Prepared Tool objects with names: {tool_names_for_log} and descriptions.")
|
200 |
# --- End Tool Name Check ---
|
201 |
|
202 |
# Let HfAgent create its own pipeline internally.
|
203 |
+
# Pass model_id_or_path as the FIRST POSITIONAL argument.
|
204 |
+
# Pass hf_token as a keyword argument.
|
205 |
self.agent = HfAgent(
|
206 |
+
model_id_or_path, # <<<--- First argument, NO 'llm_endpoint=' keyword
|
207 |
additional_tools=self.actual_tools_for_agent,
|
208 |
hf_token=hf_auth_token # HfAgent uses this for its internal LLMProxy
|
209 |
)
|
210 |
print(f"HfAgent successfully instantiated for model {model_id_or_path}.")
|
211 |
|
212 |
+
# --- Robust toolbox inspection (still useful) ---
|
213 |
+
print(f"Inspecting self.agent.toolbox...")
|
214 |
+
if hasattr(self.agent, 'toolbox'):
|
215 |
+
toolbox_attr = self.agent.toolbox
|
216 |
+
print(f"Type of self.agent.toolbox: {type(toolbox_attr)}")
|
217 |
+
|
218 |
+
if isinstance(toolbox_attr, dict):
|
219 |
+
print(f"Toolbox is a dict. Keys: {list(toolbox_attr.keys())}")
|
220 |
+
tools_info_from_dict = {}
|
221 |
+
for key, value in list(toolbox_attr.items())[:5]:
|
222 |
+
tool_name_in_dict = key
|
223 |
+
if hasattr(value, 'name'): tool_name_in_dict = value.name
|
224 |
+
elif isinstance(value, dict) and 'name' in value: tool_name_in_dict = value['name']
|
225 |
+
tools_info_from_dict[key] = {'name_attr': tool_name_in_dict, 'type': str(type(value))}
|
226 |
+
print(f"Sample of toolbox dict content: {tools_info_from_dict}")
|
227 |
+
print(f"Names of tools we passed to HfAgent: {[tool.name for tool in self.actual_tools_for_agent]}")
|
228 |
+
elif hasattr(toolbox_attr, 'tools') and callable(toolbox_attr.tools):
|
229 |
+
try:
|
230 |
+
tools_in_toolbox = toolbox_attr.tools()
|
231 |
+
print(f"Toolbox has .tools() method. Tool names: {[tool.name for tool in tools_in_toolbox if hasattr(tool, 'name')]}")
|
232 |
+
except Exception as e_toolbox_call:
|
233 |
+
print(f"Error calling or processing self.agent.toolbox.tools(): {e_toolbox_call}")
|
234 |
+
else:
|
235 |
+
print(f"Toolbox is of type {type(toolbox_attr)} but not a dict or recognized Toolbox object for this debug print.")
|
236 |
+
else:
|
237 |
+
print("self.agent does not have a 'toolbox' attribute (this would be unexpected).")
|
238 |
+
# --- End of robust toolbox inspection ---
|
239 |
|
240 |
except Exception as e:
|
241 |
print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
|
242 |
+
# import traceback # Already imported at the top of your file
|
243 |
print("Full traceback of HfAgent initialization error:")
|
244 |
traceback.print_exc()
|
245 |
raise RuntimeError(f"HfAgent initialization failed: {e}") from e
|
246 |
|
247 |
# __call__ method remains the same
|
248 |
def __call__(self, question: str) -> str:
|
|
|
249 |
print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
|
250 |
try:
|
251 |
answer = self.agent.run(question)
|
|
|
253 |
return str(answer)
|
254 |
except Exception as e:
|
255 |
print(f"ERROR: HfAgent execution failed for question '{question[:50]}...': {e}")
|
256 |
+
# import traceback # Already imported at the top of your file
|
257 |
print("Full traceback of HfAgent execution error:")
|
258 |
traceback.print_exc()
|
259 |
return f"Agent Error: Failed to process the question. Details: {e}"
|