saimohit/Country_information_Agent
0
1from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool2import datetime3import os4import pytz5import yaml6 7import pandas8 9from typing import List10 11from dotenv import load_dotenv12from tools.final_answer import FinalAnswerTool13from Gradio_UI import GradioUI14 15 16load_dotenv()17 18 19HF_TOKEN = os.getenv("HF_U1ACAPP_TOKEN")20 21 22@tool23def format_country_info(24 population: int, languages: List[str], religions: List[str]25) -> str:26 """A tool that takes as argument the most recent info about a country and most specifically27 the population number, the officially languages (accompanied and sorted by their respective percentage) used28 as well as the common religions (accompanied and sorted by their respective percentage) found in the country.29 It returns the same info formatted in markdown.30 For any info field that is not available, its value is considered as N/A31 Args:32 population: the country's population number according to most recent info33 languages: the languages used in the country34 religions: the religions found in the country35 36 """37 summary_info = {38 "Population": population if population > 0 else "N/A",39 "Languages": "<br>".join(40 [f"- {lang}" for lang in languages] if len(languages) > 1 else languages41 )42 or "N/A",43 "Religions": "<br>".join(44 [f"- {rel}" for rel in religions] if len(religions) > 1 else religions45 )46 or "N/A",47 }48 49 df = pandas.DataFrame(summary_info, index=[1])50 return df.to_markdown(index=False)51 52 53@tool54def get_current_time_in_timezone(timezone: str) -> str:55 """A tool that fetches the current local time in a specified timezone.56 Args:57 timezone: A string representing a valid timezone (e.g., 'America/New_York').58 """59 try:60 # Create timezone object61 tz = pytz.timezone(timezone)62 # Get current time in that timezone63 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")64 return f"The current local time in {timezone} is: {local_time}"65 except Exception as e:66 return f"Error fetching time for timezone '{timezone}': {str(e)}"67 68 69final_answer = FinalAnswerTool()70model = HfApiModel(71 max_tokens=2096,72 temperature=0.5,73 #model_id="Qwen/Qwen2.5-Coder-32B-Instruct",74 model_id = "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",75 custom_role_conversions=None,76 token=HF_TOKEN,77)78 79 80# Import tool from Hub81image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)82 83with open("prompts.yaml", "r") as stream:84 prompt_templates = yaml.safe_load(stream)85 86agent = CodeAgent(87 model=model,88 tools=[89 final_answer,90 DuckDuckGoSearchTool(),91 get_current_time_in_timezone,92 format_country_info,93 ], ## add your tools here (don't remove final answer)94 max_steps=6,95 verbosity_level=1,96 grammar=None,97 planning_interval=None,98 name=None,99 description=None,100 prompt_templates=prompt_templates,101)102 103 104GradioUI(agent).launch()105 