Spaces:
Runtime error
Runtime error
Leonardo
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,8 @@ from scripts.text_web_browser import (
|
|
| 19 |
VisitTool,
|
| 20 |
)
|
| 21 |
from scripts.visual_qa import visualizer
|
|
|
|
|
|
|
| 22 |
|
| 23 |
from smolagents import (
|
| 24 |
CodeAgent,
|
|
@@ -183,6 +185,19 @@ class ToolRegistry:
|
|
| 183 |
TextInspectorTool(model, text_limit),
|
| 184 |
]
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
@staticmethod
|
| 187 |
def load_image_generation_tools():
|
| 188 |
"""Initialize and return image generation tools."""
|
|
@@ -199,39 +214,54 @@ class ToolRegistry:
|
|
| 199 |
|
| 200 |
# ------------------------ Agent Creation and Execution ------------------------
|
| 201 |
def create_agent():
|
| 202 |
-
"""
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
all_tools = [visualizer] + web_tools + [image_generator]
|
| 219 |
-
|
| 220 |
-
# Validate tools before creating agent
|
| 221 |
-
for tool in all_tools:
|
| 222 |
-
if not isinstance(tool, Tool):
|
| 223 |
-
raise ValueError(
|
| 224 |
-
f"Invalid tool type: {type(tool)}. All tools must be instances of Tool class."
|
| 225 |
-
)
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
|
| 237 |
def stream_to_gradio(
|
|
|
|
| 19 |
VisitTool,
|
| 20 |
)
|
| 21 |
from scripts.visual_qa import visualizer
|
| 22 |
+
from scripts.frontmatter_tool import FrontmatterGeneratorTool
|
| 23 |
+
from scripts.text_cleaner_tool import TextCleanerTool
|
| 24 |
|
| 25 |
from smolagents import (
|
| 26 |
CodeAgent,
|
|
|
|
| 185 |
TextInspectorTool(model, text_limit),
|
| 186 |
]
|
| 187 |
|
| 188 |
+
@staticmethod
|
| 189 |
+
def load_document_tools():
|
| 190 |
+
"""
|
| 191 |
+
Initialize and return document processing, i.e. sanitisation and indexing, tools.
|
| 192 |
+
|
| 193 |
+
Returns:
|
| 194 |
+
List of document tools
|
| 195 |
+
"""
|
| 196 |
+
return [
|
| 197 |
+
FrontmatterGeneratorTool(),
|
| 198 |
+
TextCleanerTool(),
|
| 199 |
+
]
|
| 200 |
+
|
| 201 |
@staticmethod
|
| 202 |
def load_image_generation_tools():
|
| 203 |
"""Initialize and return image generation tools."""
|
|
|
|
| 214 |
|
| 215 |
# ------------------------ Agent Creation and Execution ------------------------
|
| 216 |
def create_agent():
|
| 217 |
+
"""
|
| 218 |
+
Creates a fresh agent instance with properly configured tools.
|
| 219 |
+
|
| 220 |
+
Returns:
|
| 221 |
+
CodeAgent: Configured agent ready for use
|
| 222 |
+
|
| 223 |
+
Raises:
|
| 224 |
+
ValueError: If tool validation fails
|
| 225 |
+
RuntimeError: If agent creation fails
|
| 226 |
+
"""
|
| 227 |
+
try:
|
| 228 |
+
# Initialize model
|
| 229 |
+
model = LiteLLMModel(
|
| 230 |
+
custom_role_conversions=CUSTOM_ROLE_CONVERSIONS,
|
| 231 |
+
model_id="openrouter/deepseek/deepseek-chat-v3-0324:free",
|
| 232 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
+
# Initialize tools
|
| 235 |
+
text_limit = 30000
|
| 236 |
+
browser = SimpleTextBrowser(**BROWSER_CONFIG)
|
| 237 |
+
|
| 238 |
+
# Collect all tools in a single list
|
| 239 |
+
web_tools = ToolRegistry.load_web_tools(model, browser, text_limit)
|
| 240 |
+
doc_tools = ToolRegistry.load_document_tools() # New document tools
|
| 241 |
+
image_generator = ToolRegistry.load_image_generation_tools()
|
| 242 |
+
|
| 243 |
+
# Combine all tools into a single list
|
| 244 |
+
all_tools = [visualizer] + web_tools + doc_tools + [image_generator]
|
| 245 |
+
|
| 246 |
+
# Validate tools before creating agent
|
| 247 |
+
for tool in all_tools:
|
| 248 |
+
if not isinstance(tool, Tool):
|
| 249 |
+
raise ValueError(
|
| 250 |
+
f"Invalid tool type: {type(tool)}. "
|
| 251 |
+
f"All tools must be instances of Tool class."
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
return CodeAgent(
|
| 255 |
+
model=model,
|
| 256 |
+
tools=all_tools,
|
| 257 |
+
max_steps=12,
|
| 258 |
+
verbosity_level=2,
|
| 259 |
+
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
| 260 |
+
planning_interval=4,
|
| 261 |
+
)
|
| 262 |
+
except (ValueError, RuntimeError) as e:
|
| 263 |
+
print(f"Failed to create agent: {e}")
|
| 264 |
+
raise RuntimeError(f"Agent creation failed: {e}")
|
| 265 |
|
| 266 |
|
| 267 |
def stream_to_gradio(
|