Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,9 @@ import pytz
|
|
8 |
import math
|
9 |
import re
|
10 |
import requests
|
|
|
|
|
|
|
11 |
import traceback
|
12 |
|
13 |
import sys
|
@@ -152,10 +155,19 @@ def safe_calculator(expression: str) -> str:
|
|
152 |
class HfAgentWrapper:
|
153 |
def __init__(self):
|
154 |
print("Initializing HfAgentWrapper...")
|
155 |
-
|
156 |
-
print(f"Loading HfAgent with model: {model_repo}")
|
157 |
|
158 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
# Ensure your tool functions have docstrings.
|
160 |
if not get_current_time_in_timezone.__doc__:
|
161 |
raise ValueError("Tool 'get_current_time_in_timezone' is missing a docstring.")
|
@@ -180,26 +192,25 @@ class HfAgentWrapper:
|
|
180 |
func=safe_calculator,
|
181 |
description=safe_calculator.__doc__
|
182 |
)
|
183 |
-
|
184 |
self.actual_tools_for_agent = [time_tool_obj, search_tool_obj, calculator_tool_obj]
|
185 |
-
|
186 |
tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
|
187 |
print(f"Prepared Tool objects with names: {tool_names_for_log} and descriptions.")
|
188 |
|
189 |
-
#
|
|
|
|
|
190 |
self.agent = HfAgent(
|
191 |
-
|
192 |
-
additional_tools=self.actual_tools_for_agent
|
193 |
-
trust_remote_code=True
|
194 |
)
|
195 |
-
print(f"HfAgent successfully instantiated with
|
196 |
|
197 |
-
# --- Robust toolbox inspection ---
|
198 |
print(f"Inspecting self.agent.toolbox...")
|
199 |
if hasattr(self.agent, 'toolbox'):
|
200 |
toolbox_attr = self.agent.toolbox
|
201 |
print(f"Type of self.agent.toolbox: {type(toolbox_attr)}")
|
202 |
-
|
203 |
if isinstance(toolbox_attr, dict):
|
204 |
print(f"Toolbox is a dict. Keys: {list(toolbox_attr.keys())}")
|
205 |
tools_info_from_dict = {}
|
@@ -210,7 +221,6 @@ class HfAgentWrapper:
|
|
210 |
tools_info_from_dict[key] = {'name_attr': tool_name_in_dict, 'type': str(type(value))}
|
211 |
print(f"Sample of toolbox dict content: {tools_info_from_dict}")
|
212 |
print(f"Names of tools we passed to HfAgent: {[tool.name for tool in self.actual_tools_for_agent]}")
|
213 |
-
|
214 |
elif hasattr(toolbox_attr, 'tools') and callable(toolbox_attr.tools):
|
215 |
try:
|
216 |
tools_in_toolbox = toolbox_attr.tools()
|
@@ -221,14 +231,13 @@ class HfAgentWrapper:
|
|
221 |
print(f"Toolbox is of type {type(toolbox_attr)} but not a dict or recognized Toolbox object for this debug print.")
|
222 |
else:
|
223 |
print("self.agent does not have a 'toolbox' attribute (this would be unexpected).")
|
224 |
-
# --- End of robust toolbox inspection ---
|
225 |
|
226 |
-
except Exception as e:
|
227 |
-
print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
|
228 |
# import traceback # Already imported at the top of your file
|
229 |
-
print("Full traceback of HfAgent initialization error:")
|
230 |
traceback.print_exc()
|
231 |
-
raise RuntimeError(f"HfAgent initialization failed: {e}") from e
|
232 |
|
233 |
def __call__(self, question: str) -> str:
|
234 |
print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
|
|
|
8 |
import math
|
9 |
import re
|
10 |
import requests
|
11 |
+
from transformers import HfAgent # Your successful import
|
12 |
+
from transformers.tools import Tool # Your successful import
|
13 |
+
from transformers import pipeline # <<< --- MAKE SURE THIS IMPORT IS ADDED / PRESENT
|
14 |
import traceback
|
15 |
|
16 |
import sys
|
|
|
155 |
class HfAgentWrapper:
|
156 |
def __init__(self):
|
157 |
print("Initializing HfAgentWrapper...")
|
158 |
+
model_id_or_path = "google/gemma-1.1-7b-it" # Correct Hugging Face Hub ID
|
|
|
159 |
|
160 |
try:
|
161 |
+
print(f"Attempting to create LLM pipeline for model: {model_id_or_path} with trust_remote_code=True")
|
162 |
+
# 1. Create the pipeline first, with trust_remote_code=True
|
163 |
+
llm_pipeline = pipeline(
|
164 |
+
task="text-generation",
|
165 |
+
model=model_id_or_path,
|
166 |
+
trust_remote_code=True
|
167 |
+
# Optional: device_map="auto" if accelerate is properly set up and you have >1 GPU or want specific mapping
|
168 |
+
)
|
169 |
+
print(f"Successfully created LLM pipeline for {model_id_or_path}.")
|
170 |
+
|
171 |
# Ensure your tool functions have docstrings.
|
172 |
if not get_current_time_in_timezone.__doc__:
|
173 |
raise ValueError("Tool 'get_current_time_in_timezone' is missing a docstring.")
|
|
|
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, # <<< --- PASS THE PIPELINE OBJECT HERE
|
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 |
+
# --- Robust toolbox inspection (still useful) ---
|
210 |
print(f"Inspecting self.agent.toolbox...")
|
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 = {}
|
|
|
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()
|
|
|
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}")
|
237 |
# import traceback # Already imported at the top of your file
|
238 |
+
print("Full traceback of HfAgent/Pipeline initialization error:")
|
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]}... ---")
|