DragonProgrammer commited on
Commit
6f28395
·
verified ·
1 Parent(s): 23499c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -25
app.py CHANGED
@@ -155,27 +155,24 @@ 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" # 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_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__,
@@ -195,17 +192,15 @@ class HfAgentWrapper:
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
 
@@ -239,7 +234,6 @@ class HfAgentWrapper:
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
@@ -253,7 +247,6 @@ class HfAgentWrapper:
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}"
 
155
  class HfAgentWrapper:
156
  def __init__(self):
157
  print("Initializing HfAgentWrapper...")
158
+ model_id_or_path = "bigcode/starcoderbase-1b" # Compatible model
159
 
160
  try:
161
+ print(f"Attempting to initialize HfAgent with model: {model_id_or_path}")
162
+
163
+ # Optional: Check if the secret is present for better logging.
164
+ # HfAgent will look for 'HF_TOKEN' on its own.
165
+ if not os.getenv("HF_TOKEN"):
166
+ print("CRITICAL WARNING: Space Secret 'HF_TOKEN' is not set. HfAgent will likely fail on gated models.")
167
+ # You can raise an error to stop if the token is essential
168
+ raise ValueError("HF_TOKEN secret is missing and is required for this model.")
169
  else:
170
+ print("HF_TOKEN secret is present. HfAgent will use it automatically.")
171
 
172
+ # --- CRITICAL: Ensure Tool names are correct ---
173
+ if not get_current_time_in_timezone.__doc__: raise ValueError("Tool 'get_current_time_in_timezone' is missing a docstring.")
174
+ if not web_search.__doc__: raise ValueError("Tool 'web_search' is missing a docstring.")
175
+ if not safe_calculator.__doc__: raise ValueError("Tool 'safe_calculator' is missing a docstring.")
 
 
 
176
 
177
  time_tool_obj = Tool(
178
  name=get_current_time_in_timezone.__name__,
 
192
  self.actual_tools_for_agent = [time_tool_obj, search_tool_obj, calculator_tool_obj]
193
 
194
  tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
195
+ # This print MUST show your actual tool names. If it shows empty strings, the agent will not work.
196
  print(f"Prepared Tool objects with names: {tool_names_for_log} and descriptions.")
197
  # --- End Tool Name Check ---
198
 
199
  # Let HfAgent create its own pipeline internally.
200
+ # We REMOVE the hf_token argument. HfAgent will find the HF_TOKEN environment variable itself.
 
201
  self.agent = HfAgent(
202
+ model_id_or_path, # Pass model ID string as the first positional argument
203
+ additional_tools=self.actual_tools_for_agent
 
204
  )
205
  print(f"HfAgent successfully instantiated for model {model_id_or_path}.")
206
 
 
234
 
235
  except Exception as e:
236
  print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
 
237
  print("Full traceback of HfAgent initialization error:")
238
  traceback.print_exc()
239
  raise RuntimeError(f"HfAgent initialization failed: {e}") from e
 
247
  return str(answer)
248
  except Exception as e:
249
  print(f"ERROR: HfAgent execution failed for question '{question[:50]}...': {e}")
 
250
  print("Full traceback of HfAgent execution error:")
251
  traceback.print_exc()
252
  return f"Agent Error: Failed to process the question. Details: {e}"