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 | |
| # Tool: Fetch current time in a given timezone | |
| def get_current_time_in_timezone(timezone: str) -> str: | |
| """ | |
| Fetches the current local time in a specified timezone. | |
| Args: | |
| timezone (str): The timezone string (e.g., 'America/New_York'). | |
| Returns: | |
| str: The current local time in the specified timezone. | |
| """ | |
| 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)}" | |
| # Tool: Fetch real-time weather for a given location | |
| def get_weather(city: str) -> str: | |
| """ | |
| Fetches the current weather for a given city. | |
| Args: | |
| city (str): The name of the city. | |
| Returns: | |
| str: Weather description and temperature. | |
| """ | |
| api_url = f"https://wttr.in/{city}?format=%C+%t" | |
| response = requests.get(api_url) | |
| if response.status_code == 200: | |
| return response.text | |
| return "Unable to retrieve weather information." | |
| # Tool: Fetch currency exchange rate | |
| def get_exchange_rate(base_currency: str, target_currency: str) -> str: | |
| """ | |
| Fetches the exchange rate between two currencies. | |
| Args: | |
| base_currency (str): The currency to convert from. | |
| target_currency (str): The currency to convert to. | |
| Returns: | |
| str: Exchange rate information. | |
| """ | |
| 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, "N/A") | |
| return f"1 {base_currency} = {rate} {target_currency}" | |
| return "Exchange rate information not available." | |
| # Tool: Fetch Wikipedia summary | |
| def get_wikipedia_summary(query: str) -> str: | |
| """ | |
| Fetches a short summary from Wikipedia. | |
| Args: | |
| query (str): The topic to search on Wikipedia. | |
| Returns: | |
| str: Summary of the topic. | |
| """ | |
| url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{query}" | |
| response = requests.get(url) | |
| if response.status_code == 200: | |
| return response.json().get("extract", "No summary available.") | |
| return "Wikipedia summary not found." | |
| # Tool: Fetch latest news headlines | |
| def get_latest_news() -> str: | |
| """ | |
| Fetches the latest news headlines. | |
| Returns: | |
| str: Top 5 latest news headlines. | |
| """ | |
| url = "https://newsapi.org/v2/top-headlines?country=us&apiKey=YOUR_NEWSAPI_KEY" | |
| response = requests.get(url) | |
| if response.status_code == 200: | |
| articles = response.json().get("articles", [])[:5] | |
| return "\n".join([f"{i+1}. {article['title']}" for i, article in enumerate(articles)]) | |
| return "Unable to retrieve news." | |
| # Tool: Fetch stock price for a given company symbol | |
| def get_stock_price(symbol: str) -> str: | |
| """ | |
| Fetches the latest stock price for a given company symbol. | |
| Args: | |
| symbol (str): The stock ticker symbol. | |
| Returns: | |
| str: The latest stock price. | |
| """ | |
| url = f"https://finnhub.io/api/v1/quote?symbol={symbol}&token=YOUR_FINNHUB_API_KEY" | |
| response = requests.get(url) | |
| if response.status_code == 200: | |
| data = response.json() | |
| return f"Stock price of {symbol}: ${data.get('c', 'N/A')}" | |
| return "Stock information not available." | |
| final_answer = FinalAnswerTool() | |
| # Hugging Face model setup | |
| model = HfApiModel( | |
| max_tokens=2096, | |
| temperature=0.5, | |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct', | |
| custom_role_conversions=None, | |
| ) | |
| # Load image generation tool from Hugging Face | |
| image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) | |
| with open("prompts.yaml", 'r') as stream: | |
| prompt_templates = yaml.safe_load(stream) | |
| # Creating the AI agent | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[ | |
| final_answer, | |
| get_current_time_in_timezone, | |
| get_weather, | |
| get_exchange_rate, | |
| get_wikipedia_summary, | |
| get_latest_news, | |
| get_stock_price, | |
| image_generation_tool, | |
| ], | |
| max_steps=6, | |
| verbosity_level=1, | |
| grammar=None, | |
| planning_interval=None, | |
| name=None, | |
| description=None, | |
| prompt_templates=prompt_templates | |
| ) | |
| # Launch Gradio UI for interaction | |
| GradioUI(agent).launch() | |