Spaces:
Sleeping
Sleeping
support python code execution agent
Browse files
app.py
CHANGED
@@ -13,6 +13,7 @@ import requests
|
|
13 |
from requests.exceptions import RequestException
|
14 |
from youtube_transcript_api import YouTubeTranscriptApi
|
15 |
from urllib.parse import urlparse, parse_qs
|
|
|
16 |
|
17 |
from dotenv import load_dotenv
|
18 |
|
@@ -452,30 +453,98 @@ def get_youtube_transcript(video_id: str, language: str = "en") -> str:
|
|
452 |
except Exception as e:
|
453 |
return f"Error retrieving transcript: {str(e)}"
|
454 |
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
# Args:
|
462 |
-
# transcript: The full transcript text of the video
|
463 |
-
# question: The specific question to answer based on the transcript
|
464 |
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
# # This tool leverages the LLM's understanding capability
|
469 |
-
# # The implementation is simple because the LLM will do the analysis
|
470 |
-
# if not transcript or transcript.startswith("Error"):
|
471 |
-
# return f"Unable to analyze transcript: {transcript}"
|
472 |
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
# return "TRANSCRIPT_ANALYSIS_PLACEHOLDER" # This will be replaced by LLM reasoning
|
478 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
479 |
|
480 |
|
481 |
class BasicAgent:
|
@@ -628,6 +697,23 @@ class BasicAgent:
|
|
628 |
""")
|
629 |
)
|
630 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
631 |
|
632 |
# Define the Manager Agent
|
633 |
# This agent manages tasks and delegates to other agents
|
@@ -638,11 +724,13 @@ class BasicAgent:
|
|
638 |
load_xlsx_file_as_dataframe,
|
639 |
load_xlsx_file_as_markdown,
|
640 |
query_wikipedia,
|
|
|
641 |
FinalAnswerTool()],
|
642 |
# Specify the agents this manager oversees
|
643 |
managed_agents=[self.web_search_specialist_agent,
|
644 |
self.reasoning_agent,
|
645 |
-
self.youtube_qa_agent
|
|
|
646 |
name="manager_agent", # Give the manager agent a name
|
647 |
description="Manages tasks by delegating to other agents.", # Describe its role
|
648 |
additional_authorized_imports=[
|
|
|
13 |
from requests.exceptions import RequestException
|
14 |
from youtube_transcript_api import YouTubeTranscriptApi
|
15 |
from urllib.parse import urlparse, parse_qs
|
16 |
+
import json
|
17 |
|
18 |
from dotenv import load_dotenv
|
19 |
|
|
|
453 |
except Exception as e:
|
454 |
return f"Error retrieving transcript: {str(e)}"
|
455 |
|
456 |
+
|
457 |
+
@tool
|
458 |
+
def load_text_file(file_path: str, detect_format: bool = True) -> str:
|
459 |
+
"""
|
460 |
+
Loads a text file and optionally detects and processes its format (plain text, code, JSON, etc.).
|
|
|
|
|
|
|
|
|
461 |
|
462 |
+
Args:
|
463 |
+
file_path: Path to the text file to load
|
464 |
+
detect_format: Whether to automatically detect and process the format (default: True)
|
|
|
|
|
|
|
|
|
465 |
|
466 |
+
Returns:
|
467 |
+
String containing the file content, possibly formatted based on detected type
|
468 |
+
"""
|
|
|
|
|
469 |
|
470 |
+
if not os.path.exists(file_path):
|
471 |
+
return f"Error: File not found at {file_path}"
|
472 |
+
|
473 |
+
try:
|
474 |
+
# Read the file content
|
475 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
476 |
+
content = file.read()
|
477 |
+
|
478 |
+
if not detect_format:
|
479 |
+
return f"File content ({os.path.basename(file_path)}):\n\n{content}"
|
480 |
+
|
481 |
+
# Get file extension
|
482 |
+
_, ext = os.path.splitext(file_path)
|
483 |
+
ext = ext.lower()
|
484 |
+
|
485 |
+
# Handle based on file extension or content detection
|
486 |
+
if ext in ['.json', '.geojson']:
|
487 |
+
# Process JSON
|
488 |
+
try:
|
489 |
+
parsed_json = json.loads(content)
|
490 |
+
formatted_json = json.dumps(parsed_json, indent=2)
|
491 |
+
return f"JSON content ({os.path.basename(file_path)}):\n\n{formatted_json}"
|
492 |
+
except json.JSONDecodeError:
|
493 |
+
return f"Warning: File has JSON extension but content is not valid JSON.\n\n{content}"
|
494 |
+
|
495 |
+
elif ext in ['.py', '.js', '.ts', '.java', '.c', '.cpp', '.cs', '.php', '.rb', '.go', '.rs', '.swift']:
|
496 |
+
# It's a code file, return with appropriate formatting
|
497 |
+
return f"Code file ({os.path.basename(file_path)}, {ext[1:]} language):\n\n{content}"
|
498 |
+
|
499 |
+
elif ext in ['.csv', '.tsv']:
|
500 |
+
# Handle CSV/TSV files with preview
|
501 |
+
lines = content.strip().split('\n')
|
502 |
+
preview_lines = lines[:min(10, len(lines))]
|
503 |
+
preview = '\n'.join(preview_lines)
|
504 |
+
|
505 |
+
if len(lines) > 10:
|
506 |
+
preview += f"\n\n[...and {len(lines) - 10} more lines]"
|
507 |
+
|
508 |
+
return f"Tabular data file ({os.path.basename(file_path)}):\n\n{preview}"
|
509 |
+
|
510 |
+
else:
|
511 |
+
# Try to detect JSON content regardless of extension
|
512 |
+
if content.strip().startswith('{') and content.strip().endswith('}'):
|
513 |
+
try:
|
514 |
+
parsed_json = json.loads(content)
|
515 |
+
formatted_json = json.dumps(parsed_json, indent=2)
|
516 |
+
return f"Detected JSON content ({os.path.basename(file_path)}):\n\n{formatted_json}"
|
517 |
+
except json.JSONDecodeError:
|
518 |
+
pass # Not valid JSON, continue with other detection
|
519 |
+
|
520 |
+
# Try to detect if it might be code
|
521 |
+
code_indicators = [
|
522 |
+
'def ', 'class ', 'function ', 'import ', 'from ', 'var ', 'let ', 'const ',
|
523 |
+
'#include', 'package ', 'using ', 'public class', 'int main'
|
524 |
+
]
|
525 |
+
|
526 |
+
if any(indicator in content for indicator in code_indicators):
|
527 |
+
language = "unknown programming language"
|
528 |
+
return f"Detected code file ({os.path.basename(file_path)}, {language}):\n\n{content}"
|
529 |
+
|
530 |
+
# Check if it's XML/HTML-like
|
531 |
+
if re.search(r'<\w+>.*?</\w+>', content, re.DOTALL) or content.strip().startswith('<?xml'):
|
532 |
+
return f"Markup language file ({os.path.basename(file_path)}):\n\n{content}"
|
533 |
+
|
534 |
+
# Default to plain text
|
535 |
+
return f"Plain text file ({os.path.basename(file_path)}):\n\n{content}"
|
536 |
+
|
537 |
+
except UnicodeDecodeError:
|
538 |
+
# Try different encoding if UTF-8 fails
|
539 |
+
try:
|
540 |
+
with open(file_path, 'r', encoding='latin-1') as file:
|
541 |
+
content = file.read()
|
542 |
+
return f"File content ({os.path.basename(file_path)}, non-UTF-8 encoding):\n\n{content}"
|
543 |
+
except Exception as e:
|
544 |
+
return f"Error reading file (encoding issues): {str(e)}"
|
545 |
+
except Exception as e:
|
546 |
+
return f"Error reading file: {str(e)}"
|
547 |
+
|
548 |
|
549 |
|
550 |
class BasicAgent:
|
|
|
697 |
""")
|
698 |
)
|
699 |
|
700 |
+
self.python_code_executer = CodeAgent(
|
701 |
+
model=reasoning_model,
|
702 |
+
tools=[load_text_file,
|
703 |
+
FinalAnswerTool()],
|
704 |
+
name="python_code_executer",
|
705 |
+
description=textwrap.dedent("""\
|
706 |
+
You are an expert assistant that can execute Python code.
|
707 |
+
|
708 |
+
Execute the Python code and return the final answer using the `final_answer` tool.
|
709 |
+
"""),
|
710 |
+
additional_authorized_imports=["json", "re", "pandas", "numpy", "math", "collections", "itertools", "stat", "statistics", "queue", "unicodedata", "time", "random", "datetime"],
|
711 |
+
verbosity_level=1,
|
712 |
+
max_steps=5,
|
713 |
+
planning_interval=1,
|
714 |
+
#executor_type="e2b",
|
715 |
+
#use_e2b_executor=True
|
716 |
+
)
|
717 |
|
718 |
# Define the Manager Agent
|
719 |
# This agent manages tasks and delegates to other agents
|
|
|
724 |
load_xlsx_file_as_dataframe,
|
725 |
load_xlsx_file_as_markdown,
|
726 |
query_wikipedia,
|
727 |
+
load_text_file,
|
728 |
FinalAnswerTool()],
|
729 |
# Specify the agents this manager oversees
|
730 |
managed_agents=[self.web_search_specialist_agent,
|
731 |
self.reasoning_agent,
|
732 |
+
self.youtube_qa_agent,
|
733 |
+
self.python_code_executer],
|
734 |
name="manager_agent", # Give the manager agent a name
|
735 |
description="Manages tasks by delegating to other agents.", # Describe its role
|
736 |
additional_authorized_imports=[
|