Spaces:
Sleeping
Sleeping
| from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool | |
| import datetime | |
| import requests | |
| import pytz | |
| import yaml | |
| from tools.final_answer import FinalAnswerTool | |
| from Gradio_UI import GradioUI | |
| # --- CUSTOM TOOLS --- | |
| def convert_currency(amount: float, from_currency: str, to_currency: str) -> str: | |
| """A tool that converts an amount from one currency to another using real-time exchange rates. | |
| Args: | |
| amount: The numerical amount to convert. | |
| from_currency: The source currency code (e.g., 'USD', 'INR', 'EUR'). | |
| to_currency: The target currency code (e.g., 'INR', 'GBP', 'JPY'). | |
| """ | |
| try: | |
| # Using Frankfurter API (Free, no API key required) | |
| url = f"https://api.frankfurter.dev/v1/latest?amount={amount}&from={from_currency.upper()}&to={to_currency.upper()}" | |
| response = requests.get(url) | |
| data = response.json() | |
| if response.status_code == 200: | |
| converted_amount = data['rates'][to_currency.upper()] | |
| date = data['date'] | |
| return f"{amount} {from_currency.upper()} is equal to {converted_amount:.2f} {to_currency.upper()} (Rate as of {date})." | |
| else: | |
| return f"Error: Could not convert. Please check if the currency codes are correct (e.g., USD, INR)." | |
| except Exception as e: | |
| return f"An error occurred: {str(e)}" | |
| def get_current_time_in_timezone(timezone: str) -> str: | |
| """A tool that fetches the current local time in a specified timezone. | |
| Args: | |
| timezone: A string representing a valid timezone (e.g., 'America/New_York'). | |
| """ | |
| try: | |
| tz = pytz.timezone(timezone) | |
| local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") | |
| return f"The current local time in {timezone} is: {local_time}" | |
| except Exception as e: | |
| return f"Error fetching time for timezone '{timezone}': {str(e)}" | |
| # --- AGENT SETUP --- | |
| final_answer = FinalAnswerTool() | |
| image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) | |
| model = HfApiModel( | |
| max_tokens=2096, | |
| temperature=0.5, | |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct', | |
| custom_role_conversions=None, | |
| ) | |
| with open("prompts.yaml", 'r') as stream: | |
| prompt_templates = yaml.safe_load(stream) | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[ | |
| final_answer, | |
| get_current_time_in_timezone, | |
| image_generation_tool, | |
| convert_currency # <--- Currency tool added here | |
| ], | |
| max_steps=6, | |
| verbosity_level=1, | |
| prompt_templates=prompt_templates | |
| ) | |
| if __name__ == "__main__": | |
| GradioUI(agent).launch() |