Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,6 +14,7 @@ print(f"--- Python version: {sys.version} ---")
|
|
14 |
# print(f"--- Python sys.path (module search paths): {sys.path} ---") # Optional now
|
15 |
|
16 |
import transformers
|
|
|
17 |
print(f"--- Expected Transformers Version: 4.36.0 ---")
|
18 |
print(f"--- Actual Transformers Version: {transformers.__version__} ---")
|
19 |
# print(f"--- Transformers module loaded from: {transformers.__file__} ---") # Optional now
|
@@ -146,7 +147,6 @@ def safe_calculator(expression: str) -> str:
|
|
146 |
|
147 |
# --- Basic Agent Definition ---
|
148 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
149 |
-
# --- Agent Definition using HfAgent ---
|
150 |
class HfAgentWrapper:
|
151 |
def __init__(self):
|
152 |
print("Initializing HfAgentWrapper...")
|
@@ -154,15 +154,38 @@ class HfAgentWrapper:
|
|
154 |
|
155 |
print(f"Loading HfAgent with model: {model_repo}")
|
156 |
try:
|
157 |
-
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
print(f"HfAgent loaded successfully with model {model_repo}.")
|
160 |
-
print(f"HfAgent initialized with tools: {
|
|
|
161 |
except Exception as e:
|
162 |
print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
|
|
|
|
|
|
|
|
|
163 |
raise RuntimeError(f"HfAgent initialization failed: {e}") from e
|
164 |
|
165 |
def __call__(self, question: str) -> str:
|
|
|
166 |
print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
|
167 |
try:
|
168 |
answer = self.agent.run(question)
|
|
|
14 |
# print(f"--- Python sys.path (module search paths): {sys.path} ---") # Optional now
|
15 |
|
16 |
import transformers
|
17 |
+
from transformers.tools import Tool
|
18 |
print(f"--- Expected Transformers Version: 4.36.0 ---")
|
19 |
print(f"--- Actual Transformers Version: {transformers.__version__} ---")
|
20 |
# print(f"--- Transformers module loaded from: {transformers.__file__} ---") # Optional now
|
|
|
147 |
|
148 |
# --- Basic Agent Definition ---
|
149 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
150 |
class HfAgentWrapper:
|
151 |
def __init__(self):
|
152 |
print("Initializing HfAgentWrapper...")
|
|
|
154 |
|
155 |
print(f"Loading HfAgent with model: {model_repo}")
|
156 |
try:
|
157 |
+
# Your original tool functions
|
158 |
+
# get_current_time_in_timezone, web_search, safe_calculator
|
159 |
+
|
160 |
+
# Manually wrap your functions into Tool objects
|
161 |
+
# Tool.from_function(func) uses func.__name__ for the tool name
|
162 |
+
# and func.__doc__ for the tool description.
|
163 |
+
time_tool_obj = Tool.from_function(get_current_time_in_timezone)
|
164 |
+
search_tool_obj = Tool.from_function(web_search)
|
165 |
+
calculator_tool_obj = Tool.from_function(safe_calculator)
|
166 |
+
|
167 |
+
# This is the list of Tool objects that HfAgent will receive
|
168 |
+
self.actual_tools_for_agent = [time_tool_obj, search_tool_obj, calculator_tool_obj]
|
169 |
+
|
170 |
+
# For printing/logging purposes, let's see their names
|
171 |
+
tool_names_for_log = [tool.name for tool in self.actual_tools_for_agent]
|
172 |
+
print(f"Prepared Tool objects with names: {tool_names_for_log}")
|
173 |
+
|
174 |
+
self.agent = HfAgent(model_repo, additional_tools=self.actual_tools_for_agent)
|
175 |
+
|
176 |
print(f"HfAgent loaded successfully with model {model_repo}.")
|
177 |
+
print(f"HfAgent initialized with tools: {tool_names_for_log}") # Use the names from the Tool objects
|
178 |
+
|
179 |
except Exception as e:
|
180 |
print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
|
181 |
+
# Ensure the full traceback of the original error 'e' is visible in logs
|
182 |
+
import traceback
|
183 |
+
print("Full traceback of HfAgent initialization error:")
|
184 |
+
traceback.print_exc()
|
185 |
raise RuntimeError(f"HfAgent initialization failed: {e}") from e
|
186 |
|
187 |
def __call__(self, question: str) -> str:
|
188 |
+
# ... (your __call__ method remains the same) ...
|
189 |
print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
|
190 |
try:
|
191 |
answer = self.agent.run(question)
|