Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,14 @@ import datetime
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
|
|
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
|
|
|
|
|
|
| 9 |
@tool
|
| 10 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 11 |
"""A tool that does nothing yet
|
|
@@ -28,20 +33,64 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 28 |
except Exception as e:
|
| 29 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
class CustomFinalAnswer(FinalAnswerTool):
|
| 33 |
def __call__(self, answer: str) -> str:
|
| 34 |
return f"Phoom Said: {super().__call__(answer)}"
|
| 35 |
|
| 36 |
final_answer = CustomFinalAnswer()
|
| 37 |
|
| 38 |
-
# Load
|
|
|
|
| 39 |
with open("prompts.yaml", 'r') as stream:
|
| 40 |
prompt_templates = yaml.safe_load(stream)
|
| 41 |
|
| 42 |
prompt_templates['system'] = prompt_templates.get('system', '') + \
|
| 43 |
"\n\nIMPORTANT: Always begin your final answer with 'Phoom Said:' followed by the response."
|
| 44 |
|
|
|
|
|
|
|
| 45 |
model = HfApiModel(
|
| 46 |
max_tokens=2096,
|
| 47 |
temperature=0.5,
|
|
@@ -53,7 +102,13 @@ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_co
|
|
| 53 |
|
| 54 |
agent = CodeAgent(
|
| 55 |
model=model,
|
| 56 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
max_steps=6,
|
| 58 |
verbosity_level=1,
|
| 59 |
grammar=None,
|
|
@@ -63,4 +118,6 @@ agent = CodeAgent(
|
|
| 63 |
prompt_templates=prompt_templates
|
| 64 |
)
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import smtplib
|
| 7 |
+
from email.mime.multipart import MIMEMultipart
|
| 8 |
+
from email.mime.text import MIMEText
|
| 9 |
from tools.final_answer import FinalAnswerTool
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
| 12 |
+
# -- Your Custom Tools --
|
| 13 |
+
|
| 14 |
@tool
|
| 15 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 16 |
"""A tool that does nothing yet
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
+
@tool
|
| 37 |
+
def send_html_email(
|
| 38 |
+
smtp_server: str,
|
| 39 |
+
smtp_port: int,
|
| 40 |
+
sender_email: str,
|
| 41 |
+
sender_password: str,
|
| 42 |
+
recipient_email: str,
|
| 43 |
+
subject: str,
|
| 44 |
+
html_content: str
|
| 45 |
+
) -> str:
|
| 46 |
+
"""
|
| 47 |
+
A tool to send an HTML email.
|
| 48 |
+
Args:
|
| 49 |
+
smtp_server: The SMTP server address (e.g., 'smtp.gmail.com')
|
| 50 |
+
smtp_port: The SMTP server port (e.g., 587 for TLS)
|
| 51 |
+
sender_email: The sender's email address
|
| 52 |
+
sender_password: The sender's email password or app-specific password
|
| 53 |
+
recipient_email: The recipient's email address
|
| 54 |
+
subject: The subject of the email
|
| 55 |
+
html_content: The HTML content of the email
|
| 56 |
+
Returns:
|
| 57 |
+
A status message indicating success or failure.
|
| 58 |
+
"""
|
| 59 |
+
try:
|
| 60 |
+
message = MIMEMultipart()
|
| 61 |
+
message['From'] = sender_email
|
| 62 |
+
message['To'] = recipient_email
|
| 63 |
+
message['Subject'] = subject
|
| 64 |
+
message.attach(MIMEText(html_content, 'html'))
|
| 65 |
+
|
| 66 |
+
session = smtplib.SMTP(smtp_server, smtp_port)
|
| 67 |
+
session.starttls()
|
| 68 |
+
session.login(sender_email, sender_password)
|
| 69 |
+
session.sendmail(sender_email, recipient_email, message.as_string())
|
| 70 |
+
session.quit()
|
| 71 |
+
|
| 72 |
+
return "Email sent successfully!"
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return f"Failed to send email: {str(e)}"
|
| 75 |
+
|
| 76 |
+
# -- Custom Final Answer Tool --
|
| 77 |
+
|
| 78 |
class CustomFinalAnswer(FinalAnswerTool):
|
| 79 |
def __call__(self, answer: str) -> str:
|
| 80 |
return f"Phoom Said: {super().__call__(answer)}"
|
| 81 |
|
| 82 |
final_answer = CustomFinalAnswer()
|
| 83 |
|
| 84 |
+
# -- Load Prompts --
|
| 85 |
+
|
| 86 |
with open("prompts.yaml", 'r') as stream:
|
| 87 |
prompt_templates = yaml.safe_load(stream)
|
| 88 |
|
| 89 |
prompt_templates['system'] = prompt_templates.get('system', '') + \
|
| 90 |
"\n\nIMPORTANT: Always begin your final answer with 'Phoom Said:' followed by the response."
|
| 91 |
|
| 92 |
+
# -- Load Model and Tools --
|
| 93 |
+
|
| 94 |
model = HfApiModel(
|
| 95 |
max_tokens=2096,
|
| 96 |
temperature=0.5,
|
|
|
|
| 102 |
|
| 103 |
agent = CodeAgent(
|
| 104 |
model=model,
|
| 105 |
+
tools=[
|
| 106 |
+
final_answer,
|
| 107 |
+
get_current_time_in_timezone,
|
| 108 |
+
image_generation_tool,
|
| 109 |
+
my_custom_tool,
|
| 110 |
+
send_html_email # <<<<< ADDED EMAIL TOOL
|
| 111 |
+
],
|
| 112 |
max_steps=6,
|
| 113 |
verbosity_level=1,
|
| 114 |
grammar=None,
|
|
|
|
| 118 |
prompt_templates=prompt_templates
|
| 119 |
)
|
| 120 |
|
| 121 |
+
# -- Launch UI --
|
| 122 |
+
|
| 123 |
+
GradioUI(agent).launch()
|