Yago Bolivar
refactor: update tool imports and instantiations for consistency; enhance Gradio UI error handling
17e76f4
raw
history blame
3.2 kB
from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool
import yaml
# Import tool CLASSES from the src directory
from src.final_answer_tool import FinalAnswerTool
from src.web_browsing_tool import WebBrowser
from src.file_processing_tool import FileIdentifier
from src.image_processing_tool import ImageProcessor
from src.markdown_table_parser import MarkdownTableParserTool # Updated
from src.python_tool import CodeExecutionTool
from src.speech_to_text import SpeechToTextTool # Updated
from src.spreadsheet_tool import SpreadsheetTool
from src.text_reversal_tool import TextReversalTool
from src.video_processing_tool import VideoProcessingTool
# Import GradioUI from its location
from First_agent_template.Gradio_UI import GradioUI
# Configure the Language Model
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
custom_role_conversions=None,
)
# Instantiate Tools
final_answer_tool = FinalAnswerTool()
web_browsing_tool = WebBrowser()
file_processing_tool = FileIdentifier()
image_processing_tool = ImageProcessor()
markdown_parser_tool = MarkdownTableParserTool() # Updated
python_tool = CodeExecutionTool()
speech_to_text_tool = SpeechToTextTool() # Updated
spreadsheet_tool = SpreadsheetTool()
text_reversal_tool = TextReversalTool()
video_processing_tool = VideoProcessingTool()
# Load Prompts
try:
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
except FileNotFoundError:
print("Error: prompts.yaml not found. Please ensure it is in the root project directory.")
prompt_templates = {} # Fallback to empty templates
except yaml.YAMLError as e:
print(f"Error parsing prompts.yaml: {e}")
prompt_templates = {}
# Create the Agent
agent_tools = [
final_answer_tool,
web_browsing_tool,
file_processing_tool,
image_processing_tool,
markdown_parser_tool, # Updated
python_tool,
speech_to_text_tool, # Updated
spreadsheet_tool,
text_reversal_tool,
video_processing_tool
]
agent = CodeAgent(
model=model,
tools=agent_tools,
max_steps=15,
verbosity_level=1,
name="ComprehensiveQuestionAgent",
description="An agent equipped with a suite of tools to answer diverse questions from the common_questions.json set.",
prompt_templates=prompt_templates
)
# Launch the Gradio UI
if __name__ == '__main__':
print("Launching Gradio UI...")
try:
ui = GradioUI(agent)
ui.launch()
except Exception as e:
print(f"Failed to launch Gradio UI: {e}")
print("Attempting to run agent with a sample query directly...")
try:
# This is a placeholder for a direct test if Gradio fails.
# You might want to load a question from common_questions.json here.
sample_query = "What is the capital of France and reverse the answer?"
print(f"Testing agent with query: {sample_query}")
response = agent.run(sample_query)
print(f"Agent response: {response}")
except Exception as agent_e:
print(f"Error running agent directly: {agent_e}")