Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,36 +4,137 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""
|
|
|
|
| 24 |
Args:
|
| 25 |
-
timezone: A
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"Error
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
# 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:
|
|
@@ -55,7 +156,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
import smtplib
|
| 8 |
+
import os
|
| 9 |
+
import base64
|
| 10 |
+
from email.mime.text import MIMEText
|
| 11 |
+
from email.mime.multipart import MIMEMultipart
|
| 12 |
+
from email.mime.image import MIMEImage
|
| 13 |
|
| 14 |
from Gradio_UI import GradioUI
|
| 15 |
|
| 16 |
+
|
| 17 |
+
# Aux function to help tools with timezone
|
| 18 |
+
def _get_now_in_timezone(timezone: str) -> datetime.datetime:
|
| 19 |
+
"""Helper: return actual datetime in timezone."""
|
| 20 |
+
tz = pytz.timezone(timezone)
|
| 21 |
+
return datetime.datetime.now(tz)
|
| 22 |
+
|
| 23 |
+
# Tool 1
|
| 24 |
@tool
|
| 25 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 26 |
+
"""Fetches the current local time in a specified timezone.
|
|
|
|
| 27 |
Args:
|
| 28 |
+
timezone: A valid timezone string (e.g., 'Europe/Madrid', 'America/New_York').
|
|
|
|
| 29 |
"""
|
| 30 |
+
try:
|
| 31 |
+
now = _get_now_in_timezone(timezone)
|
| 32 |
+
return f"The current local time in {timezone} is: {now.strftime('%Y-%m-%d %H:%M:%S')}"
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
+
# tool 2
|
| 37 |
@tool
|
| 38 |
+
def get_day_of_year(timezone: str) -> str:
|
| 39 |
+
"""Returns what day of the year it currently is in the given timezone,
|
| 40 |
+
and whether it is New Year's Day (January 1st at midnight).
|
| 41 |
Args:
|
| 42 |
+
timezone: A valid timezone string (e.g., 'Europe/Madrid', 'America/New_York').
|
| 43 |
"""
|
| 44 |
try:
|
| 45 |
+
now = _get_now_in_timezone(timezone) # 👈 reutiliza la lógica
|
| 46 |
+
day_of_year = now.timetuple().tm_yday
|
| 47 |
+
is_leap = now.year % 4 == 0 and (now.year % 100 != 0 or now.year % 400 == 0)
|
| 48 |
+
total_days = 366 if is_leap else 365
|
| 49 |
+
is_new_year_midnight = (now.month == 1 and now.day == 1 and now.hour == 0)
|
| 50 |
+
|
| 51 |
+
return (
|
| 52 |
+
f"In {timezone}: day {day_of_year} of {total_days} "
|
| 53 |
+
f"({now.strftime('%B %d, %Y %H:%M:%S')}). "
|
| 54 |
+
f"Is New Year midnight: {is_new_year_midnight}."
|
| 55 |
+
)
|
| 56 |
except Exception as e:
|
| 57 |
+
return f"Error: {str(e)}"
|
| 58 |
+
|
| 59 |
|
| 60 |
+
# tool 3
|
| 61 |
+
@tool
|
| 62 |
+
def send_new_year_greeting(
|
| 63 |
+
recipient_name: str,
|
| 64 |
+
recipient_email: str,
|
| 65 |
+
personalized_message: str,
|
| 66 |
+
timezone: str,
|
| 67 |
+
image_base64: str = "",
|
| 68 |
+
) -> str:
|
| 69 |
+
"""Sends a personalized New Year's greeting email with an optional AI-generated image.
|
| 70 |
+
ONLY sends if it is currently January 1st between 00:00 and 00:59 in the given timezone.
|
| 71 |
+
The agent should first use get_day_of_year to verify the date before calling this.
|
| 72 |
+
|
| 73 |
+
Args:
|
| 74 |
+
recipient_name: Full name of the recipient.
|
| 75 |
+
recipient_email: Email address of the recipient.
|
| 76 |
+
personalized_message: Warm, personalized message crafted by the agent for this person.
|
| 77 |
+
timezone: Timezone to check for New Year's midnight (e.g., 'Europe/Madrid').
|
| 78 |
+
image_base64: Optional base64-encoded image generated by image_generation_tool to attach.
|
| 79 |
+
"""
|
| 80 |
+
try:
|
| 81 |
+
# helper reused to get timezone
|
| 82 |
+
now = _get_now_in_timezone(timezone)
|
| 83 |
+
is_new_year_midnight = (now.month == 1 and now.day == 1 and now.hour == 0)
|
| 84 |
+
|
| 85 |
+
if not is_new_year_midnight:
|
| 86 |
+
return (
|
| 87 |
+
f"NOT sent. In {timezone} it is {now.strftime('%B %d, %Y %H:%M')} — "
|
| 88 |
+
f"greetings only go out on January 1st at midnight. "
|
| 89 |
+
f"Use get_day_of_year to check first!"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# email envs vars
|
| 93 |
+
sender_email = os.environ.get("GREETING_SENDER_EMAIL")
|
| 94 |
+
sender_password = os.environ.get("GREETING_SENDER_PASS")
|
| 95 |
+
|
| 96 |
+
if not sender_email or not sender_password:
|
| 97 |
+
return "Missing env vars: GREETING_SENDER_EMAIL and GREETING_SENDER_PASS"
|
| 98 |
+
|
| 99 |
+
msg = MIMEMultipart("related")
|
| 100 |
+
msg["Subject"] = f"Happy New Year {now.year}, {recipient_name}!"
|
| 101 |
+
msg["From"] = sender_email
|
| 102 |
+
msg["To"] = recipient_email
|
| 103 |
+
|
| 104 |
+
html_body = f"""
|
| 105 |
+
<html><body style="font-family: Arial, sans-serif; max-width: 600px; margin: auto; padding: 20px;">
|
| 106 |
+
<h1 style="color: #FFD700; text-align: center;">🎉 Happy New Year {now.year}!</h1>
|
| 107 |
+
{"<img src='cid:new_year_image' style='width:100%; border-radius:12px; margin-bottom:20px;'/>" if image_base64 else ""}
|
| 108 |
+
<p>Dear <strong>{recipient_name}</strong>,</p>
|
| 109 |
+
<p style="font-size: 16px; line-height: 1.6;">{personalized_message}</p>
|
| 110 |
+
<p style="color: #888; font-size: 12px; text-align:center;">
|
| 111 |
+
Sent with love <3 · {timezone}
|
| 112 |
+
</p>
|
| 113 |
+
</body></html>
|
| 114 |
+
"""
|
| 115 |
+
msg.attach(MIMEText(html_body, "html"))
|
| 116 |
+
|
| 117 |
+
# image if agent generate it
|
| 118 |
+
if image_base64:
|
| 119 |
+
img_data = base64.b64decode(image_base64)
|
| 120 |
+
img = MIMEImage(img_data)
|
| 121 |
+
img.add_header("Content-ID", "<new_year_image>")
|
| 122 |
+
msg.attach(img)
|
| 123 |
+
|
| 124 |
+
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
|
| 125 |
+
server.login(sender_email, sender_password)
|
| 126 |
+
server.sendmail(sender_email, recipient_email, msg.as_string())
|
| 127 |
+
|
| 128 |
+
return (
|
| 129 |
+
f"Greeting sent to {recipient_name} ({recipient_email}) "
|
| 130 |
+
f"at {now.strftime('%H:%M:%S')} in {timezone}!"
|
| 131 |
+
f"{' Includes AI-generated image ' if image_base64 else ''}"
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
except Exception as e:
|
| 135 |
+
return f"Failed: {str(e)}"
|
| 136 |
|
| 137 |
+
|
| 138 |
final_answer = FinalAnswerTool()
|
| 139 |
|
| 140 |
# 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:
|
|
|
|
| 156 |
|
| 157 |
agent = CodeAgent(
|
| 158 |
model=model,
|
| 159 |
+
tools=[final_answer, get_current_time_in_timezone, get_day_of_year, send_new_year_greeting, image_generation_tool], ## add your tools here (don't remove final answer)
|
| 160 |
max_steps=6,
|
| 161 |
verbosity_level=1,
|
| 162 |
grammar=None,
|