CoolFace
Apppublic

HSinghHuggingFace/My_First_AI_Agent

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py73 linesDownload Raw Back to root
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7from forex_python.converter import CurrencyRates8from smolagents import tool9 10from Gradio_UI import GradioUI11 12@tool13def currency_converter(amount: int, from_currency: str, to_currency: str) -> float:14    """A tool that converts an amount from one currency to another using real-time exchange rates.15    Args:16        amount: The amount of money to convert (integer value).17        from_currency: The currency code of the original currency (e.g., 'USD').18        to_currency: The currency code of the target currency (e.g., 'INR').19    Returns:20        Converted amount as per the current exchange rate.21    """22    try:23        c = CurrencyRates()24        converted_amount = c.convert(from_currency, to_currency, amount)25        return converted_amount26    except Exception as e:27        return f"Error in currency conversion: {str(e)}"28 29@tool30def get_current_time_in_timezone(timezone: str) -> str:31    """A tool that fetches the current local time in a specified timezone.32    Args:33        timezone: A string representing a valid timezone (e.g., 'America/New_York').34    """35    try:36        # Create timezone object37        tz = pytz.timezone(timezone)38        # Get current time in that timezone39        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")40        return f"The current local time in {timezone} is: {local_time}"41    except Exception as e:42        return f"Error fetching time for timezone '{timezone}': {str(e)}"43 44 45final_answer = FinalAnswerTool()46model = HfApiModel(47max_tokens=2096,48temperature=0.5,49model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded50custom_role_conversions=None,51)52 53 54# Import tool from Hub55image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)56 57with open("prompts.yaml", 'r') as stream:58    prompt_templates = yaml.safe_load(stream)59    60agent = CodeAgent(61    model=model,62    tools=[currency_converter,get_current_time_in_timezone,final_answer], ## add your tools here (don't remove final answer)63    max_steps=6,64    verbosity_level=1,65    grammar=None,66    planning_interval=None,67    name=None,68    description=None,69    prompt_templates=prompt_templates70)71 72 73GradioUI(agent).launch()