Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -155,82 +155,52 @@ def safe_calculator(expression: str) -> str:
|
|
155 |
class HfAgentWrapper:
|
156 |
def __init__(self):
|
157 |
print("Initializing HfAgentWrapper...")
|
158 |
-
model_id_or_path = "google/gemma-1.1-7b-it"
|
159 |
|
160 |
try:
|
161 |
-
print(f"Attempting to create LLM pipeline for model: {model_id_or_path}
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
llm_pipeline = pipeline(
|
164 |
task="text-generation",
|
165 |
model=model_id_or_path,
|
166 |
-
trust_remote_code=True
|
167 |
-
#
|
168 |
)
|
169 |
print(f"Successfully created LLM pipeline for {model_id_or_path}.")
|
170 |
|
171 |
-
#
|
172 |
-
if not get_current_time_in_timezone.__doc__:
|
173 |
-
|
174 |
-
if not
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
# Manually wrap your functions into Tool objects
|
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 |
tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
|
197 |
print(f"Prepared Tool objects with names: {tool_names_for_log} and descriptions.")
|
198 |
|
199 |
# 2. Pass the pre-initialized pipeline object to HfAgent
|
200 |
-
# The first argument (llm_endpoint) of HfAgent can be a pipeline instance.
|
201 |
print(f"Initializing HfAgent with pre-initialized pipeline...")
|
202 |
self.agent = HfAgent(
|
203 |
-
llm_pipeline,
|
204 |
additional_tools=self.actual_tools_for_agent
|
205 |
-
# Note: trust_remote_code=True is NOT passed here directly
|
206 |
)
|
207 |
print(f"HfAgent successfully instantiated with pre-initialized pipeline.")
|
208 |
|
209 |
-
#
|
210 |
-
|
211 |
-
if hasattr(self.agent, 'toolbox'):
|
212 |
-
toolbox_attr = self.agent.toolbox
|
213 |
-
print(f"Type of self.agent.toolbox: {type(toolbox_attr)}")
|
214 |
-
if isinstance(toolbox_attr, dict):
|
215 |
-
print(f"Toolbox is a dict. Keys: {list(toolbox_attr.keys())}")
|
216 |
-
tools_info_from_dict = {}
|
217 |
-
for key, value in list(toolbox_attr.items())[:5]:
|
218 |
-
tool_name_in_dict = key
|
219 |
-
if hasattr(value, 'name'): tool_name_in_dict = value.name
|
220 |
-
elif isinstance(value, dict) and 'name' in value: tool_name_in_dict = value['name']
|
221 |
-
tools_info_from_dict[key] = {'name_attr': tool_name_in_dict, 'type': str(type(value))}
|
222 |
-
print(f"Sample of toolbox dict content: {tools_info_from_dict}")
|
223 |
-
print(f"Names of tools we passed to HfAgent: {[tool.name for tool in self.actual_tools_for_agent]}")
|
224 |
-
elif hasattr(toolbox_attr, 'tools') and callable(toolbox_attr.tools):
|
225 |
-
try:
|
226 |
-
tools_in_toolbox = toolbox_attr.tools()
|
227 |
-
print(f"Toolbox has .tools() method. Tool names: {[tool.name for tool in tools_in_toolbox if hasattr(tool, 'name')]}")
|
228 |
-
except Exception as e_toolbox_call:
|
229 |
-
print(f"Error calling or processing self.agent.toolbox.tools(): {e_toolbox_call}")
|
230 |
-
else:
|
231 |
-
print(f"Toolbox is of type {type(toolbox_attr)} but not a dict or recognized Toolbox object for this debug print.")
|
232 |
-
else:
|
233 |
-
print("self.agent does not have a 'toolbox' attribute (this would be unexpected).")
|
234 |
|
235 |
except Exception as e:
|
236 |
print(f"CRITICAL ERROR: Failed to initialize HfAgent or Pipeline: {e}")
|
@@ -239,7 +209,9 @@ class HfAgentWrapper:
|
|
239 |
traceback.print_exc()
|
240 |
raise RuntimeError(f"HfAgent/Pipeline initialization failed: {e}") from e
|
241 |
|
|
|
242 |
def __call__(self, question: str) -> str:
|
|
|
243 |
print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
|
244 |
try:
|
245 |
answer = self.agent.run(question)
|
@@ -247,7 +219,6 @@ class HfAgentWrapper:
|
|
247 |
return str(answer)
|
248 |
except Exception as e:
|
249 |
print(f"ERROR: HfAgent execution failed for question '{question[:50]}...': {e}")
|
250 |
-
# import traceback # Already imported at the top of your file
|
251 |
print("Full traceback of HfAgent execution error:")
|
252 |
traceback.print_exc()
|
253 |
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 = "google/gemma-1.1-7b-it"
|
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.")
|
184 |
+
if not web_search.__doc__: raise ValueError("Tool 'web_search' is missing a docstring.")
|
185 |
+
if not safe_calculator.__doc__: raise ValueError("Tool 'safe_calculator' is missing a docstring.")
|
186 |
+
|
187 |
+
time_tool_obj = Tool(name=get_current_time_in_timezone.__name__, func=get_current_time_in_timezone, description=get_current_time_in_timezone.__doc__)
|
188 |
+
search_tool_obj = Tool(name=web_search.__name__, func=web_search, description=web_search.__doc__)
|
189 |
+
calculator_tool_obj = Tool(name=safe_calculator.__name__, func=safe_calculator, description=safe_calculator.__doc__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
self.actual_tools_for_agent = [time_tool_obj, search_tool_obj, calculator_tool_obj]
|
191 |
tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
|
192 |
print(f"Prepared Tool objects with names: {tool_names_for_log} and descriptions.")
|
193 |
|
194 |
# 2. Pass the pre-initialized pipeline object to HfAgent
|
|
|
195 |
print(f"Initializing HfAgent with pre-initialized pipeline...")
|
196 |
self.agent = HfAgent(
|
197 |
+
llm_pipeline,
|
198 |
additional_tools=self.actual_tools_for_agent
|
|
|
199 |
)
|
200 |
print(f"HfAgent successfully instantiated with pre-initialized pipeline.")
|
201 |
|
202 |
+
# ... (your robust toolbox inspection code - still keep it) ...
|
203 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
except Exception as e:
|
206 |
print(f"CRITICAL ERROR: Failed to initialize HfAgent or Pipeline: {e}")
|
|
|
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:
|
214 |
+
# ... (your existing __call__ method) ...
|
215 |
print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
|
216 |
try:
|
217 |
answer = self.agent.run(question)
|
|
|
219 |
return str(answer)
|
220 |
except Exception as e:
|
221 |
print(f"ERROR: HfAgent execution failed for question '{question[:50]}...': {e}")
|
|
|
222 |
print("Full traceback of HfAgent execution error:")
|
223 |
traceback.print_exc()
|
224 |
return f"Agent Error: Failed to process the question. Details: {e}"
|