Spaces:
Runtime error
Runtime error
Leonardo
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -181,264 +181,6 @@ class ToolRegistry:
|
|
| 181 |
raise
|
| 182 |
|
| 183 |
|
| 184 |
-
def create_agent():
|
| 185 |
-
"""Creates a fresh agent instance with properly configured tools."""
|
| 186 |
-
# Initialize model
|
| 187 |
-
model = LiteLLMModel(
|
| 188 |
-
custom_role_conversions=CUSTOM_ROLE_CONVERSIONS,
|
| 189 |
-
model_id="openrouter/deepseek/deepseek-chat-v3-0324:free", # currently serving:
|
| 190 |
-
) # DEEPSEEK = openrouter/perplexity/r1-1776 <--- boss model
|
| 191 |
-
|
| 192 |
-
# Initialize tools
|
| 193 |
-
text_limit = 30000
|
| 194 |
-
browser = SimpleTextBrowser(**BROWSER_CONFIG)
|
| 195 |
-
|
| 196 |
-
# Collect all tools in a single list
|
| 197 |
-
web_tools = ToolRegistry.load_web_tools(model, browser, text_limit)
|
| 198 |
-
image_generator = ToolRegistry.load_image_generation_tools()
|
| 199 |
-
clean_text = TextCleanerTool() # Instantiate TextCleanerTool
|
| 200 |
-
|
| 201 |
-
# Combine all tools into a single list (not a tuple)
|
| 202 |
-
all_tools = [visualizer] + web_tools + [image_generator] + [clean_text]
|
| 203 |
-
|
| 204 |
-
# Validate tools before creating agent
|
| 205 |
-
for tool in all_tools:
|
| 206 |
-
if not isinstance(tool, Tool):
|
| 207 |
-
raise ValueError(
|
| 208 |
-
"Invalid tool type: "
|
| 209 |
-
f"{type(tool)}. All tools must be instances of Tool class."
|
| 210 |
-
)
|
| 211 |
-
|
| 212 |
-
return CodeAgent(
|
| 213 |
-
model=model,
|
| 214 |
-
tools=all_tools, # Pass a single list containing all tools
|
| 215 |
-
max_steps=12,
|
| 216 |
-
verbosity_level=2,
|
| 217 |
-
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
| 218 |
-
planning_interval=1,
|
| 219 |
-
)
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
def stream_to_gradio(
|
| 223 |
-
agent,
|
| 224 |
-
task: str,
|
| 225 |
-
reset_agent_memory: bool = False,
|
| 226 |
-
additional_args: Optional[dict] = None,
|
| 227 |
-
):
|
| 228 |
-
"""Runs an agent with the given task and streams messages as Gradio ChatMessages."""
|
| 229 |
-
for step_log in agent.run(
|
| 230 |
-
task, stream=True, reset=reset_agent_memory, additional_args=additional_args
|
| 231 |
-
):
|
| 232 |
-
yield from pull_messages_from_step(step_log)
|
| 233 |
-
|
| 234 |
-
# Process final answer : Use a more comprehensive media output
|
| 235 |
-
final_answer = step_log # Last log is the run's final_answer
|
| 236 |
-
final_answer = handle_agent_output_types(final_answer)
|
| 237 |
-
|
| 238 |
-
if isinstance(final_answer, AgentText):
|
| 239 |
-
yield gr.ChatMessage(
|
| 240 |
-
role="assistant",
|
| 241 |
-
content=f"**Final answer:**\n{final_answer.to_string()}\n",
|
| 242 |
-
)
|
| 243 |
-
elif isinstance(final_answer, AgentImage):
|
| 244 |
-
yield gr.ChatMessage(
|
| 245 |
-
role="assistant",
|
| 246 |
-
content={"image": final_answer.to_string(), "type": "file"},
|
| 247 |
-
) # Send as Gradio-compatible file object:
|
| 248 |
-
elif isinstance(final_answer, AgentAudio):
|
| 249 |
-
yield gr.ChatMessage(
|
| 250 |
-
role="assistant",
|
| 251 |
-
content={"audio": final_answer.to_string(), "type": "file"},
|
| 252 |
-
) # Send as Gradio-compatible file object
|
| 253 |
-
else:
|
| 254 |
-
yield gr.ChatMessage(
|
| 255 |
-
role="assistant", content=f"**Final answer:** {str(final_answer)}"
|
| 256 |
-
)
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
"""Main application for the OpenDeepResearch Gradio interface."""
|
| 260 |
-
|
| 261 |
-
import mimetypes
|
| 262 |
-
import os
|
| 263 |
-
import re
|
| 264 |
-
import shutil
|
| 265 |
-
from typing import Optional
|
| 266 |
-
|
| 267 |
-
from dotenv import load_dotenv
|
| 268 |
-
from huggingface_hub import login
|
| 269 |
-
import gradio as gr
|
| 270 |
-
|
| 271 |
-
from scripts.text_inspector_tool import TextInspectorTool
|
| 272 |
-
from scripts.text_web_browser import (
|
| 273 |
-
ArchiveSearchTool,
|
| 274 |
-
FinderTool,
|
| 275 |
-
FindNextTool,
|
| 276 |
-
PageDownTool,
|
| 277 |
-
PageUpTool,
|
| 278 |
-
SimpleTextBrowser,
|
| 279 |
-
VisitTool,
|
| 280 |
-
)
|
| 281 |
-
from scripts.visual_qa import visualizer
|
| 282 |
-
from scripts.text_cleaner_tool import TextCleanerTool
|
| 283 |
-
|
| 284 |
-
from smolagents import (
|
| 285 |
-
CodeAgent,
|
| 286 |
-
HfApiModel,
|
| 287 |
-
LiteLLMModel,
|
| 288 |
-
OpenAIServerModel,
|
| 289 |
-
TransformersModel,
|
| 290 |
-
GoogleSearchTool,
|
| 291 |
-
Tool,
|
| 292 |
-
)
|
| 293 |
-
from smolagents.agent_types import AgentText, AgentImage, AgentAudio
|
| 294 |
-
from smolagents.gradio_ui import pull_messages_from_step, handle_agent_output_types
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
# Constants and configurations
|
| 298 |
-
AUTHORIZED_IMPORTS = [
|
| 299 |
-
"requests",
|
| 300 |
-
"zipfile",
|
| 301 |
-
"pandas",
|
| 302 |
-
"numpy",
|
| 303 |
-
"sympy",
|
| 304 |
-
"json",
|
| 305 |
-
"bs4",
|
| 306 |
-
"pubchempy",
|
| 307 |
-
"xml",
|
| 308 |
-
"yahoo_finance",
|
| 309 |
-
"Bio",
|
| 310 |
-
"sklearn",
|
| 311 |
-
"scipy",
|
| 312 |
-
"pydub",
|
| 313 |
-
"PIL",
|
| 314 |
-
"chess",
|
| 315 |
-
"PyPDF2",
|
| 316 |
-
"pptx",
|
| 317 |
-
"torch",
|
| 318 |
-
"datetime",
|
| 319 |
-
"fractions",
|
| 320 |
-
"csv",
|
| 321 |
-
"clean-text",
|
| 322 |
-
]
|
| 323 |
-
|
| 324 |
-
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
|
| 325 |
-
BROWSER_CONFIG = {
|
| 326 |
-
"viewport_size": 1024 * 5,
|
| 327 |
-
"downloads_folder": "downloads_folder",
|
| 328 |
-
"request_kwargs": {
|
| 329 |
-
"headers": {"User-Agent": USER_AGENT},
|
| 330 |
-
"timeout": 300,
|
| 331 |
-
},
|
| 332 |
-
"serpapi_key": os.getenv("SERPAPI_API_KEY"),
|
| 333 |
-
}
|
| 334 |
-
|
| 335 |
-
CUSTOM_ROLE_CONVERSIONS = {"tool-call": "assistant", "tool-response": "user"}
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
ALLOWED_FILE_TYPES = [
|
| 339 |
-
"application/pdf",
|
| 340 |
-
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
| 341 |
-
"text/plain",
|
| 342 |
-
"text/markdown", # Added Markdown support
|
| 343 |
-
"application/json", # Added JSON support
|
| 344 |
-
"image/png",
|
| 345 |
-
"image/webp",
|
| 346 |
-
"image/jpeg", # Added JPEG support
|
| 347 |
-
"image/gif", # Added GIF support
|
| 348 |
-
"video/mp4",
|
| 349 |
-
"audio/mpeg", # Added MP3 support
|
| 350 |
-
"audio/wav", # Added WAV support
|
| 351 |
-
"audio/ogg", # Added OGG support
|
| 352 |
-
]
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
def setup_environment():
|
| 356 |
-
"""Initialize environment variables and authentication."""
|
| 357 |
-
load_dotenv(override=True)
|
| 358 |
-
hf_token = os.getenv("HF_TOKEN")
|
| 359 |
-
if hf_token: # Check if token is actually set
|
| 360 |
-
login(hf_token)
|
| 361 |
-
print("HF_TOKEN (last 10 characters):", hf_token[-10:])
|
| 362 |
-
else:
|
| 363 |
-
print("HF_TOKEN not found in environment variables.")
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
class ModelManager:
|
| 367 |
-
"""Manages model loading and initialization."""
|
| 368 |
-
|
| 369 |
-
@staticmethod
|
| 370 |
-
def load_model(chosen_inference: str, model_id: str, key_manager=None):
|
| 371 |
-
"""Load the specified model with appropriate configuration."""
|
| 372 |
-
try:
|
| 373 |
-
if chosen_inference == "hf_api":
|
| 374 |
-
return HfApiModel(model_id=model_id)
|
| 375 |
-
|
| 376 |
-
if chosen_inference == "hf_api_provider":
|
| 377 |
-
return HfApiModel(provider="together")
|
| 378 |
-
|
| 379 |
-
if chosen_inference == "litellm":
|
| 380 |
-
return LiteLLMModel(model_id=model_id)
|
| 381 |
-
|
| 382 |
-
if chosen_inference == "openai":
|
| 383 |
-
if not key_manager:
|
| 384 |
-
raise ValueError("Key manager required for OpenAI model")
|
| 385 |
-
return OpenAIServerModel(
|
| 386 |
-
model_id=model_id, api_key=key_manager.get_key("openai_api_key")
|
| 387 |
-
)
|
| 388 |
-
|
| 389 |
-
if chosen_inference == "transformers":
|
| 390 |
-
return TransformersModel(
|
| 391 |
-
model_id="HuggingFaceTB/SmolLM2-1.7B-Instruct",
|
| 392 |
-
device_map="auto",
|
| 393 |
-
max_new_tokens=1000,
|
| 394 |
-
)
|
| 395 |
-
|
| 396 |
-
raise ValueError(f"Invalid inference type: {chosen_inference}")
|
| 397 |
-
except Exception as e:
|
| 398 |
-
print(f"✗ Couldn't load model: {e}")
|
| 399 |
-
raise
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
class ToolRegistry:
|
| 403 |
-
"""Manages tool initialization and organization."""
|
| 404 |
-
|
| 405 |
-
@staticmethod
|
| 406 |
-
def load_web_tools(model, browser, text_limit=20000):
|
| 407 |
-
"""Initialize and return web-related tools."""
|
| 408 |
-
return [
|
| 409 |
-
GoogleSearchTool(provider="serper"),
|
| 410 |
-
VisitTool(browser),
|
| 411 |
-
PageUpTool(browser),
|
| 412 |
-
PageDownTool(browser),
|
| 413 |
-
FinderTool(browser),
|
| 414 |
-
FindNextTool(browser),
|
| 415 |
-
ArchiveSearchTool(browser),
|
| 416 |
-
TextInspectorTool(model, text_limit),
|
| 417 |
-
]
|
| 418 |
-
|
| 419 |
-
@staticmethod
|
| 420 |
-
def load_image_generation_tools():
|
| 421 |
-
"""Initialize and return image generation tools."""
|
| 422 |
-
try:
|
| 423 |
-
return Tool.from_space(
|
| 424 |
-
space_id="xkerser/FLUX.1-dev",
|
| 425 |
-
name="image_generator",
|
| 426 |
-
description="Generates high-quality AgentImage with text prompt (77 token limit).",
|
| 427 |
-
)
|
| 428 |
-
except Exception as e:
|
| 429 |
-
print(f"✗ Couldn't initialize image generation tool: {e}")
|
| 430 |
-
raise
|
| 431 |
-
|
| 432 |
-
@staticmethod
|
| 433 |
-
def load_clean_text_tool():
|
| 434 |
-
"""Initialize and return image generation tools."""
|
| 435 |
-
try:
|
| 436 |
-
return TextCleanerTool
|
| 437 |
-
except Exception as e:
|
| 438 |
-
print(f"✗ Couldn't initialize clean text tool: {e}")
|
| 439 |
-
raise
|
| 440 |
-
|
| 441 |
-
|
| 442 |
def create_agent():
|
| 443 |
"""Creates a fresh agent instance with properly configured tools."""
|
| 444 |
# Initialize model
|
|
@@ -880,4 +622,3 @@ def main():
|
|
| 880 |
|
| 881 |
if __name__ == "__main__":
|
| 882 |
main()
|
| 883 |
-
|
|
|
|
| 181 |
raise
|
| 182 |
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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