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 # Initialize DuckDuckGo search tool search_tool = DuckDuckGoSearchTool() @tool def find_open_restaurants_by_location(location: str, cuisine: str = None, rating: float = None, number_of_options: int = 5 )-> str: """A tool that searches for restaurants according to location, cuisine, rating and local time. Args: location: A string representing the location area with the optional inclusion of a zip code to search for open restaurants (e.g. Paris, Paris 75005, London, London NW1 4RY). cuisine: An optional string representing the cuisine type (e.g. Asian, Indian, Italian, breakfast, lunch, etc.) rating: An optional float representing the average customer rating of the restaurant from Google, TripAdvisor, or Yelp. number_of_options: An integer representing the number of return options, default set to 5. """ try: # Build query dynamically based on provided inputs query_parts = [ f"restaurants open now in {location}", f"serving {cuisine} cuisine" if cuisine else "", f"rated {rating} or higher" if rating else "", ] query = " ".join(filter(None, query_parts)) search_results = search_tool(query) if not search_results: return f"❌ No matching restaurants found in {location} at {local_time} fulfilling desired requests." # Ensure search_results is a list before iterating if isinstance(search_results, list) and len(search_results) > 0: results_str = "\n".join( [f"{result.get('title', 'Unknown')} - {result.get('url', 'No URL')}" for result in search_results[:number_of_options]] ) else: results_str = str(search_results) # Convert to string if it's not a list return f"🍽️ Open restaurants in {location}:\n\n{results_str}" except Exception as e: return f"❌ Error finding restaurants: {str(e)}" @tool 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: # Create timezone object tz = pytz.timezone(timezone) # Get current time in that 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)}" final_answer = FinalAnswerTool() # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder: model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' model = HfApiModel( max_tokens=2096, temperature=0.0, model_id= model_id,# it is possible that this model may be overloaded custom_role_conversions=None, ) # Import tool from Hub 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) agent = CodeAgent( model=model, tools=[final_answer, get_current_time_in_timezone, find_open_restaurants_by_location], ## add your tools here (don't remove final answer) max_steps=6, verbosity_level=1, grammar=None, planning_interval=None, name=None, description=None, prompt_templates=prompt_templates ) GradioUI(agent).launch()