Spaces:
Runtime error
Runtime error
Leonardo
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,7 @@ from scripts.text_web_browser import (
|
|
| 19 |
VisitTool,
|
| 20 |
)
|
| 21 |
from scripts.visual_qa import visualizer
|
|
|
|
| 22 |
|
| 23 |
from smolagents import (
|
| 24 |
CodeAgent,
|
|
@@ -32,7 +33,7 @@ from smolagents import (
|
|
| 32 |
from smolagents.agent_types import AgentText, AgentImage, AgentAudio
|
| 33 |
from smolagents.gradio_ui import pull_messages_from_step, handle_agent_output_types
|
| 34 |
|
| 35 |
-
|
| 36 |
# Constants and configurations
|
| 37 |
AUTHORIZED_IMPORTS = [
|
| 38 |
"requests",
|
|
@@ -60,24 +61,26 @@ AUTHORIZED_IMPORTS = [
|
|
| 60 |
"clean-text",
|
| 61 |
]
|
| 62 |
|
| 63 |
-
|
| 64 |
BROWSER_CONFIG = {
|
| 65 |
"viewport_size": 1024 * 5,
|
| 66 |
"downloads_folder": "downloads_folder",
|
| 67 |
"request_kwargs": {
|
| 68 |
-
"headers": {"User-Agent":
|
| 69 |
"timeout": 300,
|
| 70 |
},
|
| 71 |
"serpapi_key": os.getenv("SERPAPI_API_KEY"),
|
| 72 |
}
|
| 73 |
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
-
# Multimedia file types supported:
|
| 77 |
ALLOWED_FILE_TYPES = [
|
| 78 |
"application/pdf",
|
| 79 |
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
| 80 |
"text/plain",
|
|
|
|
|
|
|
| 81 |
"image/png",
|
| 82 |
"image/webp",
|
| 83 |
"image/jpeg", # Added JPEG support
|
|
@@ -92,14 +95,14 @@ ALLOWED_FILE_TYPES = [
|
|
| 92 |
def setup_environment():
|
| 93 |
"""Initialize environment variables and authentication."""
|
| 94 |
load_dotenv(override=True)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
| 98 |
else:
|
| 99 |
print("HF_TOKEN not found in environment variables.")
|
| 100 |
|
| 101 |
|
| 102 |
-
# ------------------------ Model and Tool Management ------------------------
|
| 103 |
class ModelManager:
|
| 104 |
"""Manages model loading and initialization."""
|
| 105 |
|
|
@@ -110,13 +113,13 @@ class ModelManager:
|
|
| 110 |
if chosen_inference == "hf_api":
|
| 111 |
return HfApiModel(model_id=model_id)
|
| 112 |
|
| 113 |
-
|
| 114 |
return HfApiModel(provider="together")
|
| 115 |
|
| 116 |
-
|
| 117 |
return LiteLLMModel(model_id=model_id)
|
| 118 |
|
| 119 |
-
|
| 120 |
if not key_manager:
|
| 121 |
raise ValueError("Key manager required for OpenAI model")
|
| 122 |
|
|
@@ -124,15 +127,14 @@ class ModelManager:
|
|
| 124 |
model_id=model_id, api_key=key_manager.get_key("openai_api_key")
|
| 125 |
)
|
| 126 |
|
| 127 |
-
|
| 128 |
return TransformersModel(
|
| 129 |
model_id="HuggingFaceTB/SmolLM2-1.7B-Instruct",
|
| 130 |
device_map="auto",
|
| 131 |
max_new_tokens=1000,
|
| 132 |
)
|
| 133 |
|
| 134 |
-
|
| 135 |
-
raise ValueError(f"Invalid inference type: {chosen_inference}")
|
| 136 |
|
| 137 |
except Exception as e:
|
| 138 |
print(f"✗ Couldn't load model: {e}")
|
|
@@ -169,14 +171,22 @@ class ToolRegistry:
|
|
| 169 |
print(f"✗ Couldn't initialize image generation tool: {e}")
|
| 170 |
raise
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
# ------------------------ Agent Creation and Execution ------------------------
|
| 174 |
def create_agent():
|
| 175 |
"""Creates a fresh agent instance with properly configured tools."""
|
| 176 |
# Initialize model
|
| 177 |
model = LiteLLMModel(
|
| 178 |
-
custom_role_conversions=
|
| 179 |
-
model_id="openrouter/
|
| 180 |
) # DEEPSEEK = openrouter/perplexity/r1-1776 <--- boss model
|
| 181 |
|
| 182 |
# Initialize tools
|
|
@@ -186,9 +196,10 @@ def create_agent():
|
|
| 186 |
# Collect all tools in a single list
|
| 187 |
web_tools = ToolRegistry.load_web_tools(model, browser, text_limit)
|
| 188 |
image_generator = ToolRegistry.load_image_generation_tools()
|
|
|
|
| 189 |
|
| 190 |
# Combine all tools into a single list (not a tuple)
|
| 191 |
-
all_tools = [visualizer
|
| 192 |
|
| 193 |
# Validate tools before creating agent
|
| 194 |
for tool in all_tools:
|
|
@@ -200,8 +211,8 @@ def create_agent():
|
|
| 200 |
return CodeAgent(
|
| 201 |
model=model,
|
| 202 |
tools=all_tools, # Pass a single list containing all tools
|
| 203 |
-
max_steps=
|
| 204 |
-
verbosity_level=
|
| 205 |
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
| 206 |
planning_interval=4,
|
| 207 |
)
|
|
@@ -245,7 +256,6 @@ def stream_to_gradio(
|
|
| 245 |
)
|
| 246 |
|
| 247 |
|
| 248 |
-
# ------------------------ Gradio UI Components ------------------------
|
| 249 |
class GradioUI:
|
| 250 |
"""A one-line interface to launch your agent in Gradio."""
|
| 251 |
|
|
@@ -254,8 +264,7 @@ class GradioUI:
|
|
| 254 |
self.file_upload_folder = file_upload_folder
|
| 255 |
|
| 256 |
if self.file_upload_folder is not None:
|
| 257 |
-
|
| 258 |
-
os.mkdir(file_upload_folder)
|
| 259 |
|
| 260 |
def interact_with_agent(self, prompt, messages, session_state):
|
| 261 |
"""Main interaction handler with the agent."""
|
|
@@ -344,7 +353,7 @@ class GradioUI:
|
|
| 344 |
"""Process user message and handle file references."""
|
| 345 |
message = text_input
|
| 346 |
|
| 347 |
-
if
|
| 348 |
message += f"\nYou have been provided with these files, which might be helpful or not: {file_uploads_log}" # Added file list
|
| 349 |
|
| 350 |
return (
|
|
@@ -409,7 +418,7 @@ class GradioUI:
|
|
| 409 |
with gr.Sidebar():
|
| 410 |
gr.Markdown(
|
| 411 |
"""#OpenDeepResearch - 3theSmolagents!
|
| 412 |
-
Model_id:
|
| 413 |
)
|
| 414 |
with gr.Group():
|
| 415 |
gr.Markdown("**What's on your mind mate?**", container=True)
|
|
@@ -580,7 +589,6 @@ class GradioUI:
|
|
| 580 |
)
|
| 581 |
|
| 582 |
|
| 583 |
-
# ------------------------ Execution ------------------------
|
| 584 |
def main():
|
| 585 |
"""Main entry point for the application."""
|
| 586 |
# Initialize environment
|
|
|
|
| 19 |
VisitTool,
|
| 20 |
)
|
| 21 |
from scripts.visual_qa import visualizer
|
| 22 |
+
from scripts.text_cleaner_tool import TextCleanerTool
|
| 23 |
|
| 24 |
from smolagents import (
|
| 25 |
CodeAgent,
|
|
|
|
| 33 |
from smolagents.agent_types import AgentText, AgentImage, AgentAudio
|
| 34 |
from smolagents.gradio_ui import pull_messages_from_step, handle_agent_output_types
|
| 35 |
|
| 36 |
+
|
| 37 |
# Constants and configurations
|
| 38 |
AUTHORIZED_IMPORTS = [
|
| 39 |
"requests",
|
|
|
|
| 61 |
"clean-text",
|
| 62 |
]
|
| 63 |
|
| 64 |
+
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"
|
| 65 |
BROWSER_CONFIG = {
|
| 66 |
"viewport_size": 1024 * 5,
|
| 67 |
"downloads_folder": "downloads_folder",
|
| 68 |
"request_kwargs": {
|
| 69 |
+
"headers": {"User-Agent": USER_AGENT},
|
| 70 |
"timeout": 300,
|
| 71 |
},
|
| 72 |
"serpapi_key": os.getenv("SERPAPI_API_KEY"),
|
| 73 |
}
|
| 74 |
|
| 75 |
+
CUSTOM_ROLE_CONVERSIONS = {"tool-call": "assistant", "tool-response": "user"}
|
| 76 |
+
|
| 77 |
|
|
|
|
| 78 |
ALLOWED_FILE_TYPES = [
|
| 79 |
"application/pdf",
|
| 80 |
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
| 81 |
"text/plain",
|
| 82 |
+
"text/markdown", # Added Markdown support
|
| 83 |
+
"application/json", # Added JSON support
|
| 84 |
"image/png",
|
| 85 |
"image/webp",
|
| 86 |
"image/jpeg", # Added JPEG support
|
|
|
|
| 95 |
def setup_environment():
|
| 96 |
"""Initialize environment variables and authentication."""
|
| 97 |
load_dotenv(override=True)
|
| 98 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 99 |
+
if hf_token: # Check if token is actually set
|
| 100 |
+
login(hf_token)
|
| 101 |
+
print("HF_TOKEN (last 10 characters):", hf_token[-10:])
|
| 102 |
else:
|
| 103 |
print("HF_TOKEN not found in environment variables.")
|
| 104 |
|
| 105 |
|
|
|
|
| 106 |
class ModelManager:
|
| 107 |
"""Manages model loading and initialization."""
|
| 108 |
|
|
|
|
| 113 |
if chosen_inference == "hf_api":
|
| 114 |
return HfApiModel(model_id=model_id)
|
| 115 |
|
| 116 |
+
if chosen_inference == "hf_api_provider":
|
| 117 |
return HfApiModel(provider="together")
|
| 118 |
|
| 119 |
+
if chosen_inference == "litellm":
|
| 120 |
return LiteLLMModel(model_id=model_id)
|
| 121 |
|
| 122 |
+
if chosen_inference == "openai":
|
| 123 |
if not key_manager:
|
| 124 |
raise ValueError("Key manager required for OpenAI model")
|
| 125 |
|
|
|
|
| 127 |
model_id=model_id, api_key=key_manager.get_key("openai_api_key")
|
| 128 |
)
|
| 129 |
|
| 130 |
+
if chosen_inference == "transformers":
|
| 131 |
return TransformersModel(
|
| 132 |
model_id="HuggingFaceTB/SmolLM2-1.7B-Instruct",
|
| 133 |
device_map="auto",
|
| 134 |
max_new_tokens=1000,
|
| 135 |
)
|
| 136 |
|
| 137 |
+
raise ValueError(f"Invalid inference type: {chosen_inference}")
|
|
|
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
print(f"✗ Couldn't load model: {e}")
|
|
|
|
| 171 |
print(f"✗ Couldn't initialize image generation tool: {e}")
|
| 172 |
raise
|
| 173 |
|
| 174 |
+
@staticmethod
|
| 175 |
+
def load_clean_text_tool():
|
| 176 |
+
"""Initialize and return image generation tools."""
|
| 177 |
+
try:
|
| 178 |
+
return TextCleanerTool
|
| 179 |
+
except Exception as e:
|
| 180 |
+
print(f"✗ Couldn't initialize clean text tool: {e}")
|
| 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
|
|
|
|
| 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 = ToolRegistry.load_clean_text_tool()
|
| 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:
|
|
|
|
| 211 |
return CodeAgent(
|
| 212 |
model=model,
|
| 213 |
tools=all_tools, # Pass a single list containing all tools
|
| 214 |
+
max_steps=12,
|
| 215 |
+
verbosity_level=2,
|
| 216 |
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
| 217 |
planning_interval=4,
|
| 218 |
)
|
|
|
|
| 256 |
)
|
| 257 |
|
| 258 |
|
|
|
|
| 259 |
class GradioUI:
|
| 260 |
"""A one-line interface to launch your agent in Gradio."""
|
| 261 |
|
|
|
|
| 264 |
self.file_upload_folder = file_upload_folder
|
| 265 |
|
| 266 |
if self.file_upload_folder is not None:
|
| 267 |
+
os.makedirs(file_upload_folder, exist_ok=True)
|
|
|
|
| 268 |
|
| 269 |
def interact_with_agent(self, prompt, messages, session_state):
|
| 270 |
"""Main interaction handler with the agent."""
|
|
|
|
| 353 |
"""Process user message and handle file references."""
|
| 354 |
message = text_input
|
| 355 |
|
| 356 |
+
if file_uploads_log:
|
| 357 |
message += f"\nYou have been provided with these files, which might be helpful or not: {file_uploads_log}" # Added file list
|
| 358 |
|
| 359 |
return (
|
|
|
|
| 418 |
with gr.Sidebar():
|
| 419 |
gr.Markdown(
|
| 420 |
"""#OpenDeepResearch - 3theSmolagents!
|
| 421 |
+
Model_id: deepseek/deepseek-chat-v3-0324:free"""
|
| 422 |
)
|
| 423 |
with gr.Group():
|
| 424 |
gr.Markdown("**What's on your mind mate?**", container=True)
|
|
|
|
| 589 |
)
|
| 590 |
|
| 591 |
|
|
|
|
| 592 |
def main():
|
| 593 |
"""Main entry point for the application."""
|
| 594 |
# Initialize environment
|