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 import geocoder # For location lookup from io import BytesIO from PIL import Image import base64 @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: 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 def get_location_image(timezone: str) -> str: """A tool that fetches an image of a location based on the provided timezone. Args: timezone: A string representing a valid timezone (e.g., 'America/New_York'). """ try: # 1. Get Location (City/Region) based on timezone try: # More robust timezone to location mapping. Handles cases where a timezone # might cover multiple cities. g = geocoder.timezone_at(lat=None, lng=None, tz=timezone) # No lat/lng needed location_name = g.city # or g.region, or a combination if location_name is None: location_name = g.state # try state if city is not found if location_name is None: return f"Could not determine location for timezone: {timezone}" except Exception as e: return f"Error getting location: {e}" # 2. Search for Image (DuckDuckGo or other image search) try: search_term = f"{location_name} city" # Be specific image_search = DuckDuckGoSearchTool() image_urls = image_search(search_term, image_type="photo") #photos work best if not image_urls: return f"No images found for {location_name}" image_url = image_urls[0] # Take the first result except Exception as e: return f"Error searching for image: {e}" # 3. Download and Encode Image try: response = requests.get(image_url, stream=True) response.raise_for_status() # Check for HTTP errors image = Image.open(BytesIO(response.content)) buffered = BytesIO() image.save(buffered, format="JPEG") # Or PNG, depending on source img_str = base64.b64encode(buffered.getvalue()).decode() return f'{location_name}' # Return as HTML image tag except Exception as e: return f"Error processing image: {e}" except Exception as e: return f"An unexpected error occurred: {e}" 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) 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_location_image], # Add your new tool max_steps=6, verbosity_level=1, grammar=None, planning_interval=None, name=None, description=None, prompt_templates=prompt_templates, ) GradioUI(agent).launch()