DragonProgrammer commited on
Commit
04d206a
·
verified ·
1 Parent(s): 33ee0b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -21
app.py CHANGED
@@ -155,29 +155,41 @@ 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 create LLM pipeline for model: {model_id_or_path}")
162
-
163
- # Retrieve the token from Space Secrets
164
- hf_auth_token = os.getenv("HF_AUTH_TOKEN") # Use the name you gave the secret
165
 
 
166
  if not hf_auth_token:
167
- print("WARNING: HF_AUTH_TOKEN secret not found. This will likely fail for gated models like Gemma.")
168
- # You might want to raise an error here if the token is absolutely essential
169
- # raise ValueError("HF_AUTH_TOKEN secret is missing. Please add it to your Space secrets.")
170
  else:
171
- print("HF_AUTH_TOKEN found, will use it for pipeline creation.")
172
-
173
- # 1. Create the pipeline first, with trust_remote_code=True AND the token
174
- llm_pipeline = pipeline(
175
- task="text-generation",
176
- model=model_id_or_path,
177
- trust_remote_code=True,
178
- token=hf_auth_token # <<< --- PASS THE TOKEN HERE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  )
180
- print(f"Successfully created LLM pipeline for {model_id_or_path}.")
181
 
182
  # ... (rest of your Tool object creations - these were correct)
183
  if not get_current_time_in_timezone.__doc__: raise ValueError("Tool 'get_current_time_in_timezone' is missing a docstring.")
@@ -203,11 +215,10 @@ class HfAgentWrapper:
203
  # ...
204
 
205
  except Exception as e:
206
- print(f"CRITICAL ERROR: Failed to initialize HfAgent or Pipeline: {e}")
207
- # import traceback # Already imported at the top of your file
208
- print("Full traceback of HfAgent/Pipeline initialization error:")
209
  traceback.print_exc()
210
- raise RuntimeError(f"HfAgent/Pipeline initialization failed: {e}") from e
211
 
212
  # __call__ method remains the same
213
  def __call__(self, question: 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
+ hf_auth_token = os.getenv("HF_AUTH_TOKEN")
164
  if not hf_auth_token:
165
+ print("WARNING: HF_AUTH_TOKEN secret not found. Starcoder is gated and requires a token.")
166
+ # For Starcoder, token is necessary
167
+ raise ValueError("HF_AUTH_TOKEN secret is missing and is required for bigcode/starcoderbase-1b.")
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__: raise ValueError("Tool 'get_current_time_in_timezone' is missing a docstring.")
173
+ if not web_search.__doc__: raise ValueError("Tool 'web_search' is missing a docstring.")
174
+ if not safe_calculator.__doc__: raise ValueError("Tool 'safe_calculator' is missing a docstring.")
175
+
176
+ time_tool_obj = Tool(name=get_current_time_in_timezone.__name__, func=get_current_time_in_timezone, description=get_current_time_in_timezone.__doc__)
177
+ search_tool_obj = Tool(name=web_search.__name__, func=web_search, description=web_search.__doc__)
178
+ calculator_tool_obj = Tool(name=safe_calculator.__name__, func=safe_calculator, description=safe_calculator.__doc__)
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
+ print(f"Prepared Tool objects with names: {tool_names_for_log} and descriptions.") # Verify this prints correct names!
182
+ # --- End Tool Name Check ---
183
+
184
+ # Let HfAgent create its own pipeline internally.
185
+ # Pass the model ID string and the hf_token.
186
+ # trust_remote_code=True is not needed for HfAgent constructor and not for starcoder with 4.36.0.
187
+ self.agent = HfAgent(
188
+ llm_endpoint=model_id_or_path, # Pass model ID string
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
  # ... (rest of your Tool object creations - these were correct)
195
  if not get_current_time_in_timezone.__doc__: raise ValueError("Tool 'get_current_time_in_timezone' is missing a docstring.")
 
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: