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 | |
| # Use updated API from duckduckgo_search: import DDGS instead of ddg | |
| try: | |
| from duckduckgo_search import DDGS | |
| except ImportError: | |
| raise ImportError("Please install duckduckgo_search using 'pip install duckduckgo_search'.") | |
| def DuckDuckGoSearchTool(arg1: str, arg2: int) -> str: | |
| """ | |
| A tool that lets you search the web using DuckDuckGo. | |
| Args: | |
| arg1: The search query. | |
| arg2: The maximum number of results to return. | |
| Returns: | |
| A formatted string containing the search results. | |
| """ | |
| with DDGS() as ddgs: | |
| results = ddgs.text(arg1, max_results=arg2) | |
| if not results: | |
| return "No results found." | |
| formatted_results = [] | |
| for idx, result in enumerate(results, start=1): | |
| title = result.get("title", "No title") | |
| href = result.get("href", "No URL") | |
| snippet = result.get("body", "No description available") | |
| formatted_results.append( | |
| f"Result {idx}:\n" | |
| f"🔍 Title: {title}\n" | |
| f"🌐 URL: {href}\n" | |
| f"📝 Snippet: {snippet}\n" | |
| ) | |
| return "\n".join(formatted_results) | |
| 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'). | |
| Returns: | |
| A string indicating the current local time in the given 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)}" | |
| def get_weather_info(location: str) -> str: | |
| """ | |
| A tool that fetches current weather information for a given location using wttr.in. | |
| Args: | |
| location: The name of the location (e.g., 'New York'). | |
| Returns: | |
| A formatted string with the current weather details. | |
| """ | |
| try: | |
| url = f"http://wttr.in/{location}?format=j1" | |
| response = requests.get(url) | |
| data = response.json() | |
| current_condition = data.get("current_condition", [{}])[0] | |
| temp_c = current_condition.get("temp_C", "N/A") | |
| weather_desc = current_condition.get("weatherDesc", [{"value": "N/A"}])[0]["value"] | |
| humidity = current_condition.get("humidity", "N/A") | |
| return ( | |
| f"Current weather in {location}:\n" | |
| f"🌡 Temperature: {temp_c}°C\n" | |
| f"🌤 Weather: {weather_desc}\n" | |
| f"💧 Humidity: {humidity}%" | |
| ) | |
| except Exception as e: | |
| return f"Error fetching weather for {location}: {str(e)}" | |
| # Initialize the final answer tool. | |
| final_answer = FinalAnswerTool() | |
| # Setup the language model. | |
| model = HfApiModel( | |
| max_tokens=2096, | |
| temperature=0.5, | |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct', | |
| custom_role_conversions=None, | |
| ) | |
| # Load an additional image generation tool from the Hub. | |
| image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) | |
| # Load prompt templates from a YAML configuration file. | |
| with open("prompts.yaml", 'r') as stream: | |
| prompt_templates = yaml.safe_load(stream) | |
| # Create the CodeAgent with the specified model and tools. | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[final_answer, DuckDuckGoSearchTool, get_current_time_in_timezone, get_weather_info], | |
| max_steps=6, | |
| verbosity_level=1, | |
| grammar=None, | |
| planning_interval=None, | |
| name=None, | |
| description=None, | |
| prompt_templates=prompt_templates | |
| ) | |
| # Launch the Gradio UI for interactive use. | |
| GradioUI(agent).launch() | |