Spaces:
Runtime error
Runtime error
| 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 | |
| def my_custom_tool(arg1:str, arg2:int) -> str: | |
| """A tool that does nothing yet | |
| Args: | |
| arg1: the first argument | |
| arg2: the second argument | |
| """ | |
| return "What magic will you build?" | |
| def get_current_time_in_timezone(timezone: str) -> str: | |
| """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)}" | |
| def get_wikipedia_summary(query: str) -> str: | |
| """Fetches a summary from Wikipedia. | |
| Args: | |
| query: The search term. | |
| """ | |
| response = requests.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{query}") | |
| if response.status_code == 200: | |
| return response.json().get("extract", "No summary found.") | |
| return "Failed to fetch Wikipedia summary." | |
| def get_weather(city: str) -> str: | |
| """Fetches weather data for a given city. | |
| Args: | |
| city: Name of the city. | |
| """ | |
| API_KEY = "your_weather_api_key_here" | |
| url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric" | |
| response = requests.get(url) | |
| if response.status_code == 200: | |
| data = response.json() | |
| return f"Weather in {city}: {data['weather'][0]['description']}, Temp: {data['main']['temp']}°C" | |
| return "Failed to fetch weather data." | |
| def get_exchange_rate(base_currency: str, target_currency: str) -> str: | |
| """Fetches exchange rate between two currencies. | |
| Args: | |
| base_currency: The currency to convert from. | |
| target_currency: The currency to convert to. | |
| """ | |
| url = f"https://api.exchangerate-api.com/v4/latest/{base_currency}" | |
| response = requests.get(url) | |
| if response.status_code == 200: | |
| data = response.json() | |
| rate = data['rates'].get(target_currency, "Currency not found") | |
| return f"Exchange rate from {base_currency} to {target_currency}: {rate}" | |
| return "Failed to fetch exchange rate." | |
| def generate_text_summary(text: str) -> str: | |
| """Generates a concise summary of the provided text. | |
| Args: | |
| text: The text to summarize. | |
| """ | |
| return "This is a summary of the given text. (Replace with actual implementation)" | |
| def translate_text(text: str, target_language: str) -> str: | |
| """Translates text into the specified target language. | |
| Args: | |
| text: The text to translate. | |
| target_language: The target language code (e.g., 'fr' for French). | |
| """ | |
| return f"Translated text to {target_language}: (Replace with actual implementation)" | |
| final_answer = FinalAnswerTool() | |
| model = HfApiModel( | |
| max_tokens=2096, | |
| temperature=0.5, | |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct', | |
| custom_role_conversions=None, | |
| ) | |
| image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) | |
| text_summarization_tool = load_tool("agents-course/text-summarization", trust_remote_code=True) | |
| translation_tool = load_tool("agents-course/text-translation", trust_remote_code=True) | |
| 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, get_wikipedia_summary, get_weather, get_exchange_rate, image_generation_tool, text_summarization_tool, translation_tool], | |
| max_steps=6, | |
| verbosity_level=1, | |
| grammar=None, | |
| planning_interval=None, | |
| name="Multi-Tool AI Assistant", | |
| description="An AI-powered assistant with multiple tools including weather, exchange rates, text summarization, and image generation.", | |
| prompt_templates=prompt_templates | |
| ) | |
| GradioUI(agent).launch() |