Spaces:
Runtime error
Runtime error
Leonardo
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,11 +74,11 @@ AUTHORIZED_IMPORTS = [
|
|
| 74 |
"time", # Measuring time
|
| 75 |
"tempfile", # Creating temporary files and directories
|
| 76 |
# Data Visualization (if needed) - Consider security implications carefully
|
| 77 |
-
"matplotlib", # Plotting library (basic charts)
|
| 78 |
-
"seaborn", # Statistical data visualization (more advanced)
|
| 79 |
-
# Web Scraping (more specific/controlled) - Consider ethical implications
|
| 80 |
"lxml", # Faster XML/HTML processing (alternative to bs4)
|
| 81 |
-
"selenium", # Automated browser control (for dynamic websites)
|
| 82 |
# Database interaction (if needed) - Handle credentials securely!
|
| 83 |
"sqlite3", # SQLite database access
|
| 84 |
# "psycopg2", # PostgreSQL adapter if needed
|
|
@@ -89,7 +89,10 @@ AUTHORIZED_IMPORTS = [
|
|
| 89 |
]
|
| 90 |
|
| 91 |
|
| 92 |
-
USER_AGENT =
|
|
|
|
|
|
|
|
|
|
| 93 |
BROWSER_CONFIG = {
|
| 94 |
"viewport_size": 1024 * 5,
|
| 95 |
"downloads_folder": "downloads_folder",
|
|
@@ -215,7 +218,8 @@ def create_agent():
|
|
| 215 |
# Initialize model
|
| 216 |
model = LiteLLMModel(
|
| 217 |
custom_role_conversions=CUSTOM_ROLE_CONVERSIONS,
|
| 218 |
-
|
|
|
|
| 219 |
) # DEEPSEEK = openrouter/perplexity/r1-1776 <--- boss model
|
| 220 |
|
| 221 |
# Initialize tools
|
|
@@ -234,7 +238,8 @@ def create_agent():
|
|
| 234 |
for tool in all_tools:
|
| 235 |
if not isinstance(tool, Tool):
|
| 236 |
raise ValueError(
|
| 237 |
-
f"Invalid tool type: {type(tool)}.
|
|
|
|
| 238 |
)
|
| 239 |
|
| 240 |
return CodeAgent(
|
|
@@ -264,9 +269,9 @@ def stream_to_gradio(
|
|
| 264 |
for step_log in agent.run(
|
| 265 |
task, stream=True, reset=reset_agent_memory, additional_args=additional_args
|
| 266 |
):
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
for message in
|
| 270 |
if not first_message_yielded:
|
| 271 |
# Replace the initial "Processing" message
|
| 272 |
first_message_yielded = True
|
|
@@ -274,10 +279,14 @@ def stream_to_gradio(
|
|
| 274 |
"⏳ Processing your request...", ""
|
| 275 |
)
|
| 276 |
|
| 277 |
-
#
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
message.content = f"📄 **Document Analysis:** {message.content}"
|
| 280 |
-
elif "
|
| 281 |
message.content = f"🔍 **Search:** {message.content}"
|
| 282 |
|
| 283 |
yield message
|
|
@@ -298,7 +307,10 @@ def stream_to_gradio(
|
|
| 298 |
except Exception as e:
|
| 299 |
yield gr.ChatMessage(
|
| 300 |
role="assistant",
|
| 301 |
-
content=
|
|
|
|
|
|
|
|
|
|
| 302 |
)
|
| 303 |
|
| 304 |
|
|
@@ -367,9 +379,8 @@ class GradioUI:
|
|
| 367 |
|
| 368 |
# Sanitize file name
|
| 369 |
original_name = os.path.basename(file.name)
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
) # Replace invalid chars with underscores
|
| 373 |
|
| 374 |
# Ensure the extension correlates to the mime type
|
| 375 |
type_to_ext = {}
|
|
@@ -419,12 +430,16 @@ class GradioUI:
|
|
| 419 |
no_currency_symbols=False,
|
| 420 |
no_punct=False,
|
| 421 |
lang="en",
|
| 422 |
-
) # Can change default behaviour
|
| 423 |
|
| 424 |
message = cleaned_message # Use the cleaned message
|
| 425 |
|
| 426 |
if file_uploads_log:
|
| 427 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
|
| 429 |
return (
|
| 430 |
message,
|
|
@@ -495,7 +510,10 @@ class GradioUI:
|
|
| 495 |
lines=3,
|
| 496 |
label="Your request",
|
| 497 |
container=False,
|
| 498 |
-
placeholder=
|
|
|
|
|
|
|
|
|
|
| 499 |
)
|
| 500 |
launch_research_btn = gr.Button("Run", variant="primary")
|
| 501 |
|
|
@@ -516,9 +534,11 @@ class GradioUI:
|
|
| 516 |
with gr.Row():
|
| 517 |
gr.HTML(
|
| 518 |
"""
|
| 519 |
-
<div style="display: flex; align-items: center; gap: 8px;
|
|
|
|
| 520 |
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png"
|
| 521 |
-
style="width: 32px; height: 32px; object-fit: contain;"
|
|
|
|
| 522 |
<a target="_blank" href="https://github.com/huggingface/smolagents">
|
| 523 |
<b>huggingface/smolagents</b>
|
| 524 |
</a>
|
|
@@ -527,7 +547,8 @@ class GradioUI:
|
|
| 527 |
)
|
| 528 |
|
| 529 |
# Add session state to store session-specific data
|
| 530 |
-
|
|
|
|
| 531 |
stored_messages = gr.State([])
|
| 532 |
if "file_uploads_log" not in locals():
|
| 533 |
file_uploads_log = gr.State([])
|
|
|
|
| 74 |
"time", # Measuring time
|
| 75 |
"tempfile", # Creating temporary files and directories
|
| 76 |
# Data Visualization (if needed) - Consider security implications carefully
|
| 77 |
+
"matplotlib", # Plotting library (basic charts)
|
| 78 |
+
"seaborn", # Statistical data visualization (more advanced)
|
| 79 |
+
# Web Scraping (more specific/controlled) - Consider ethical implications
|
| 80 |
"lxml", # Faster XML/HTML processing (alternative to bs4)
|
| 81 |
+
"selenium", # Automated browser control (for dynamic websites)
|
| 82 |
# Database interaction (if needed) - Handle credentials securely!
|
| 83 |
"sqlite3", # SQLite database access
|
| 84 |
# "psycopg2", # PostgreSQL adapter if needed
|
|
|
|
| 89 |
]
|
| 90 |
|
| 91 |
|
| 92 |
+
USER_AGENT = (
|
| 93 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
| 94 |
+
"(KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
|
| 95 |
+
)
|
| 96 |
BROWSER_CONFIG = {
|
| 97 |
"viewport_size": 1024 * 5,
|
| 98 |
"downloads_folder": "downloads_folder",
|
|
|
|
| 218 |
# Initialize model
|
| 219 |
model = LiteLLMModel(
|
| 220 |
custom_role_conversions=CUSTOM_ROLE_CONVERSIONS,
|
| 221 |
+
# Currently serving:
|
| 222 |
+
model_id="openrouter/deepseek/deepseek-chat-v3-0324:free",
|
| 223 |
) # DEEPSEEK = openrouter/perplexity/r1-1776 <--- boss model
|
| 224 |
|
| 225 |
# Initialize tools
|
|
|
|
| 238 |
for tool in all_tools:
|
| 239 |
if not isinstance(tool, Tool):
|
| 240 |
raise ValueError(
|
| 241 |
+
f"Invalid tool type: {type(tool)}. "
|
| 242 |
+
f"All tools must be instances of Tool class."
|
| 243 |
)
|
| 244 |
|
| 245 |
return CodeAgent(
|
|
|
|
| 269 |
for step_log in agent.run(
|
| 270 |
task, stream=True, reset=reset_agent_memory, additional_args=additional_args
|
| 271 |
):
|
| 272 |
+
# pull_messages_from_step is a generator function that yields messages
|
| 273 |
+
# We need to iterate through each yielded message
|
| 274 |
+
for message in pull_messages_from_step(step_log):
|
| 275 |
if not first_message_yielded:
|
| 276 |
# Replace the initial "Processing" message
|
| 277 |
first_message_yielded = True
|
|
|
|
| 279 |
"⏳ Processing your request...", ""
|
| 280 |
)
|
| 281 |
|
| 282 |
+
# Check message content for document analysis or search references
|
| 283 |
+
content_lower = (
|
| 284 |
+
message.content.lower() if hasattr(message, "content") else ""
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
if "document analysis" in content_lower:
|
| 288 |
message.content = f"📄 **Document Analysis:** {message.content}"
|
| 289 |
+
elif "search" in content_lower:
|
| 290 |
message.content = f"🔍 **Search:** {message.content}"
|
| 291 |
|
| 292 |
yield message
|
|
|
|
| 307 |
except Exception as e:
|
| 308 |
yield gr.ChatMessage(
|
| 309 |
role="assistant",
|
| 310 |
+
content=(
|
| 311 |
+
f"❌ **Error:** {str(e)}\n\n"
|
| 312 |
+
f"Please try again with a different query."
|
| 313 |
+
),
|
| 314 |
)
|
| 315 |
|
| 316 |
|
|
|
|
| 379 |
|
| 380 |
# Sanitize file name
|
| 381 |
original_name = os.path.basename(file.name)
|
| 382 |
+
# Replace invalid chars with underscores
|
| 383 |
+
sanitized_name = re.sub(r"[^\w\-.]", "_", original_name)
|
|
|
|
| 384 |
|
| 385 |
# Ensure the extension correlates to the mime type
|
| 386 |
type_to_ext = {}
|
|
|
|
| 430 |
no_currency_symbols=False,
|
| 431 |
no_punct=False,
|
| 432 |
lang="en",
|
| 433 |
+
) # Can change default behaviour with TextCleanerTool
|
| 434 |
|
| 435 |
message = cleaned_message # Use the cleaned message
|
| 436 |
|
| 437 |
if file_uploads_log:
|
| 438 |
+
# Added file list to message
|
| 439 |
+
message += (
|
| 440 |
+
f"\nYou have been provided with these files, which might be "
|
| 441 |
+
f"helpful or not: {file_uploads_log}"
|
| 442 |
+
)
|
| 443 |
|
| 444 |
return (
|
| 445 |
message,
|
|
|
|
| 510 |
lines=3,
|
| 511 |
label="Your request",
|
| 512 |
container=False,
|
| 513 |
+
placeholder=(
|
| 514 |
+
"Enter your prompt here and press Shift+Enter or "
|
| 515 |
+
"press the button"
|
| 516 |
+
),
|
| 517 |
)
|
| 518 |
launch_research_btn = gr.Button("Run", variant="primary")
|
| 519 |
|
|
|
|
| 534 |
with gr.Row():
|
| 535 |
gr.HTML(
|
| 536 |
"""
|
| 537 |
+
<div style="display: flex; align-items: center; gap: 8px;
|
| 538 |
+
font-family: system-ui, -apple-system, sans-serif;">
|
| 539 |
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png"
|
| 540 |
+
style="width: 32px; height: 32px; object-fit: contain;"
|
| 541 |
+
alt="logo">
|
| 542 |
<a target="_blank" href="https://github.com/huggingface/smolagents">
|
| 543 |
<b>huggingface/smolagents</b>
|
| 544 |
</a>
|
|
|
|
| 547 |
)
|
| 548 |
|
| 549 |
# Add session state to store session-specific data
|
| 550 |
+
# Initialize empty state for each session
|
| 551 |
+
session_state = gr.State({})
|
| 552 |
stored_messages = gr.State([])
|
| 553 |
if "file_uploads_log" not in locals():
|
| 554 |
file_uploads_log = gr.State([])
|