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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -56
app.py CHANGED
@@ -155,90 +155,66 @@ 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" # 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__,
179
  func=get_current_time_in_timezone,
180
  description=get_current_time_in_timezone.__doc__
181
  )
182
  search_tool_obj = Tool(
183
- name=web_search.__name__,
184
  func=web_search,
185
  description=web_search.__doc__
186
  )
187
  calculator_tool_obj = Tool(
188
- name=safe_calculator.__name__,
189
  func=safe_calculator,
190
  description=safe_calculator.__doc__
191
  )
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
-
207
- # --- Robust toolbox inspection (still useful) ---
208
- print(f"Inspecting self.agent.toolbox...")
209
- if hasattr(self.agent, 'toolbox'):
210
- toolbox_attr = self.agent.toolbox
211
- print(f"Type of self.agent.toolbox: {type(toolbox_attr)}")
212
-
213
- if isinstance(toolbox_attr, dict):
214
- print(f"Toolbox is a dict. Keys: {list(toolbox_attr.keys())}")
215
- tools_info_from_dict = {}
216
- for key, value in list(toolbox_attr.items())[:5]:
217
- tool_name_in_dict = key
218
- if hasattr(value, 'name'): tool_name_in_dict = value.name
219
- elif isinstance(value, dict) and 'name' in value: tool_name_in_dict = value['name']
220
- tools_info_from_dict[key] = {'name_attr': tool_name_in_dict, 'type': str(type(value))}
221
- print(f"Sample of toolbox dict content: {tools_info_from_dict}")
222
- print(f"Names of tools we passed to HfAgent: {[tool.name for tool in self.actual_tools_for_agent]}")
223
- elif hasattr(toolbox_attr, 'tools') and callable(toolbox_attr.tools):
224
- try:
225
- tools_in_toolbox = toolbox_attr.tools()
226
- print(f"Toolbox has .tools() method. Tool names: {[tool.name for tool in tools_in_toolbox if hasattr(tool, 'name')]}")
227
- except Exception as e_toolbox_call:
228
- print(f"Error calling or processing self.agent.toolbox.tools(): {e_toolbox_call}")
229
- else:
230
- print(f"Toolbox is of type {type(toolbox_attr)} but not a dict or recognized Toolbox object for this debug print.")
231
- else:
232
- print("self.agent does not have a 'toolbox' attribute (this would be unexpected).")
233
- # --- End of robust toolbox inspection ---
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
240
 
241
- # __call__ method remains the same
242
  def __call__(self, question: str) -> str:
243
  print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
244
  try:
 
155
  class HfAgentWrapper:
156
  def __init__(self):
157
  print("Initializing HfAgentWrapper...")
158
+ model_id_or_path = "bigcode/starcoderbase-1b" # A model compatible with transformers v4.36.0
 
 
 
159
 
160
+ try:
161
+ print(f"Strategy: Pre-creating pipeline for model: {model_id_or_path}")
162
+ hf_auth_token = os.getenv("HF_TOKEN") # Secret should be named HF_TOKEN
163
+ if not hf_auth_token:
164
+ print("WARNING: HF_TOKEN secret not found. This may fail if model requires token.")
165
+ # Starcoderbase is gated, so this is needed.
166
  raise ValueError("HF_TOKEN secret is missing and is required for this model.")
167
  else:
168
+ print(f"HF_TOKEN secret found (length: {len(hf_auth_token)}).")
169
+
170
+ # --- Step 1: Create the pipeline object FIRST ---
171
+ # This allows us to handle errors from pipeline creation directly.
172
+ llm_pipeline = pipeline(
173
+ task="text-generation",
174
+ model=model_id_or_path,
175
+ token=hf_auth_token
176
+ # trust_remote_code=True # Not generally needed for starcoder with this version
177
+ )
178
+ print("Successfully created LLM pipeline object.")
179
 
180
+ # --- Step 2: Ensure your tools are created WITH proper names ---
181
  if not get_current_time_in_timezone.__doc__: raise ValueError("Tool 'get_current_time_in_timezone' is missing a docstring.")
182
  if not web_search.__doc__: raise ValueError("Tool 'web_search' is missing a docstring.")
183
  if not safe_calculator.__doc__: raise ValueError("Tool 'safe_calculator' is missing a docstring.")
184
 
185
  time_tool_obj = Tool(
186
+ name=get_current_time_in_timezone.__name__, # Use the function's name
187
  func=get_current_time_in_timezone,
188
  description=get_current_time_in_timezone.__doc__
189
  )
190
  search_tool_obj = Tool(
191
+ name=web_search.__name__, # Use the function's name
192
  func=web_search,
193
  description=web_search.__doc__
194
  )
195
  calculator_tool_obj = Tool(
196
+ name=safe_calculator.__name__, # Use the function's name
197
  func=safe_calculator,
198
  description=safe_calculator.__doc__
199
  )
200
  self.actual_tools_for_agent = [time_tool_obj, search_tool_obj, calculator_tool_obj]
201
+ print(f"Prepared Tool objects with names: {[tool.name for tool in self.actual_tools_for_agent]}")
202
+
203
+ # --- Step 3: Pass the PRE-INITIALIZED pipeline object to HfAgent ---
204
+ print("Initializing HfAgent with the pre-created pipeline...")
 
 
 
 
205
  self.agent = HfAgent(
206
+ llm_pipeline, # Pass the pipeline object directly as the first argument
207
  additional_tools=self.actual_tools_for_agent
208
  )
209
+ print("HfAgent successfully instantiated with pre-initialized pipeline.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
 
211
  except Exception as e:
212
+ print(f"CRITICAL ERROR: Failed to initialize HfAgent or Pipeline: {e}")
213
+ print("Full traceback of HfAgent/Pipeline initialization error:")
214
  traceback.print_exc()
215
+ raise RuntimeError(f"HfAgent/Pipeline initialization failed: {e}") from e
216
 
217
+ # The __call__ method remains the same
218
  def __call__(self, question: str) -> str:
219
  print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
220
  try: