Leonardo commited on
Commit
26cad6d
·
verified ·
1 Parent(s): 64228cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -32
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
- """Creates a fresh agent instance with properly configured tools."""
203
- # Initialize model
204
- model = LiteLLMModel(
205
- custom_role_conversions=CUSTOM_ROLE_CONVERSIONS,
206
- model_id="openrouter/google/gemini-2.0-flash-001", # currently serving:
207
- ) # DEEPSEEK = openrouter/perplexity/r1-1776 <--- boss model
208
-
209
- # Initialize tools
210
- text_limit = 30000
211
- browser = SimpleTextBrowser(**BROWSER_CONFIG)
212
-
213
- # Collect all tools in a single list
214
- web_tools = ToolRegistry.load_web_tools(model, browser, text_limit)
215
- image_generator = ToolRegistry.load_image_generation_tools()
216
-
217
- # Combine all tools into a single list (not a tuple)
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
- return CodeAgent(
228
- model=model,
229
- tools=all_tools, # Pass a single list containing all tools
230
- max_steps=10,
231
- verbosity_level=1,
232
- additional_authorized_imports=AUTHORIZED_IMPORTS,
233
- planning_interval=4,
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(