eazaran/First_agent_template
0
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7 8from Gradio_UI import GradioUI9 10@tool11def fetch_latest_news(topic: str) -> str:12 """Fetches the latest news articles related to a given topic.13 Args:14 topic: The topic to search for news articles (e.g., 'AI technology').15 """16 try:17 # Create DuckDuckGo Search object18 search_tool = DuckDuckGoSearchTool()19 # Get latest news about that provided subject20 results = search_tool.run(f"{topic} latest news")21 return f"Here are some recent news articles about {topic}:\n\n{results}"22 except Exception as e:23 return f"Error fetching news for '{topic}': {str(e)}"24 25@tool26def get_current_time_in_timezone(timezone: str) -> str:27 """A tool that fetches the current local time in a specified timezone.28 Args:29 timezone: A string representing a valid timezone (e.g., 'America/New_York').30 """31 try:32 # Create timezone object33 tz = pytz.timezone(timezone)34 # Get current time in that timezone35 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")36 return f"The current local time in {timezone} is: {local_time}"37 except Exception as e:38 return f"Error fetching time for timezone '{timezone}': {str(e)}"39 40 41final_answer = FinalAnswerTool()42model = HfApiModel(43max_tokens=2096,44temperature=0.5,45model_id='Qwen/Qwen2.5-Coder-32B-Instruct',46custom_role_conversions=None,47)48 49 50# Import tool from Hub51image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)52 53with open("prompts.yaml", 'r') as stream:54 prompt_templates = yaml.safe_load(stream)55 56agent = CodeAgent(57 model=model,58 tools=[final_answer, fetch_latest_news],59 max_steps=6,60 verbosity_level=1,61 grammar=None,62 planning_interval=None,63 name=None,64 description=None,65 prompt_templates=prompt_templates66)67 68 69GradioUI(agent).launch()