from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool import datetime import requests import pytz import yaml from tools.final_answer import FinalAnswerTool import smtplib import os import base64 from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage from Gradio_UI import GradioUI # Aux function to help tools with timezone def _get_now_in_timezone(timezone: str) -> datetime.datetime: """Helper: return actual datetime in timezone.""" tz = pytz.timezone(timezone) return datetime.datetime.now(tz) # Tool 1 @tool def get_current_time_in_timezone(timezone: str) -> str: """Fetches the current local time in a specified timezone. Args: timezone: A valid timezone string (e.g., 'Europe/Madrid', 'America/New_York'). """ try: now = _get_now_in_timezone(timezone) return f"The current local time in {timezone} is: {now.strftime('%Y-%m-%d %H:%M:%S')}" except Exception as e: return f"Error fetching time for timezone '{timezone}': {str(e)}" # tool 2 @tool def get_day_of_year(timezone: str) -> str: """Returns what day of the year it currently is in the given timezone, and whether it is New Year's Day (January 1st at midnight). Args: timezone: A valid timezone string (e.g., 'Europe/Madrid', 'America/New_York'). """ try: now = _get_now_in_timezone(timezone) # 👈 reutiliza la lógica day_of_year = now.timetuple().tm_yday is_leap = now.year % 4 == 0 and (now.year % 100 != 0 or now.year % 400 == 0) total_days = 366 if is_leap else 365 is_new_year_midnight = (now.month == 1 and now.day == 1 and now.hour == 0) return ( f"In {timezone}: day {day_of_year} of {total_days} " f"({now.strftime('%B %d, %Y %H:%M:%S')}). " f"Is New Year midnight: {is_new_year_midnight}." ) except Exception as e: return f"Error: {str(e)}" # tool 3 @tool def send_new_year_greeting( recipient_name: str, recipient_email: str, personalized_message: str, timezone: str, image_base64: str = "", ) -> str: """Sends a personalized New Year's greeting email with an optional AI-generated image. ONLY sends if it is currently January 1st between 00:00 and 00:59 in the given timezone. The agent should first use get_day_of_year to verify the date before calling this. Args: recipient_name: Full name of the recipient. recipient_email: Email address of the recipient. personalized_message: Warm, personalized message crafted by the agent for this person. timezone: Timezone to check for New Year's midnight (e.g., 'Europe/Madrid'). image_base64: Optional base64-encoded image generated by image_generation_tool to attach. """ try: # helper reused to get timezone now = _get_now_in_timezone(timezone) is_new_year_midnight = (now.month == 1 and now.day == 1 and now.hour == 0) if not is_new_year_midnight: return ( f"NOT sent. In {timezone} it is {now.strftime('%B %d, %Y %H:%M')} — " f"greetings only go out on January 1st at midnight. " f"Use get_day_of_year to check first!" ) # email envs vars sender_email = os.environ.get("GREETING_SENDER_EMAIL") sender_password = os.environ.get("GREETING_SENDER_PASS") if not sender_email or not sender_password: return "Missing env vars: GREETING_SENDER_EMAIL and GREETING_SENDER_PASS" msg = MIMEMultipart("related") msg["Subject"] = f"Happy New Year {now.year}, {recipient_name}!" msg["From"] = sender_email msg["To"] = recipient_email html_body = f"""
Dear {recipient_name},
{personalized_message}
Sent with love <3 · {timezone}
""" msg.attach(MIMEText(html_body, "html")) # image if agent generate it if image_base64: img_data = base64.b64decode(image_base64) img = MIMEImage(img_data) img.add_header("Content-ID", "