Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
|
|
|
|
|
|
|
|
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -18,6 +21,75 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -56,7 +128,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 56 |
|
| 57 |
agent = CodeAgent(
|
| 58 |
model=model,
|
| 59 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 60 |
max_steps=6,
|
| 61 |
verbosity_level=1,
|
| 62 |
grammar=None,
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
+
import os
|
| 3 |
+
from google.oauth2.credentials import Credentials
|
| 4 |
+
from googleapiclient.discovery import build
|
| 5 |
import datetime
|
| 6 |
import requests
|
| 7 |
import pytz
|
|
|
|
| 21 |
"""
|
| 22 |
return "What magic will you build ?"
|
| 23 |
|
| 24 |
+
def get_gmail_service():
|
| 25 |
+
creds = Credentials(
|
| 26 |
+
token=None,
|
| 27 |
+
refresh_token=os.environ["GMAIL_REFRESH_TOKEN"],
|
| 28 |
+
client_id=os.environ["GMAIL_CLIENT_ID"],
|
| 29 |
+
client_secret=os.environ["GMAIL_CLIENT_SECRET"],
|
| 30 |
+
token_uri="https://oauth2.googleapis.com/token",
|
| 31 |
+
)
|
| 32 |
+
return build("gmail", "v1", credentials=creds)
|
| 33 |
+
|
| 34 |
+
@tool
|
| 35 |
+
def read_latest_emails(max_results: int = 5) -> str:
|
| 36 |
+
"""Reads the latest emails from Gmail inbox.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
max_results: Number of emails to fetch (default 5)
|
| 40 |
+
|
| 41 |
+
Returns:
|
| 42 |
+
A summary of recent emails
|
| 43 |
+
"""
|
| 44 |
+
service = get_gmail_service()
|
| 45 |
+
results = service.users().messages().list(
|
| 46 |
+
userId="me", maxResults=max_results, labelIds=["INBOX"]
|
| 47 |
+
).execute()
|
| 48 |
+
|
| 49 |
+
messages = results.get("messages", [])
|
| 50 |
+
summaries = []
|
| 51 |
+
|
| 52 |
+
for msg in messages:
|
| 53 |
+
detail = service.users().messages().get(
|
| 54 |
+
userId="me", id=msg["id"], format="metadata",
|
| 55 |
+
metadataHeaders=["From", "Subject", "Date"]
|
| 56 |
+
).execute()
|
| 57 |
+
headers = {h["name"]: h["value"] for h in detail["payload"]["headers"]}
|
| 58 |
+
summaries.append(
|
| 59 |
+
f"From: {headers.get('From')}\n"
|
| 60 |
+
f"Subject: {headers.get('Subject')}\n"
|
| 61 |
+
f"Date: {headers.get('Date')}"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
return "\n\n".join(summaries)
|
| 65 |
+
|
| 66 |
+
@tool
|
| 67 |
+
def send_email(to: str, subject: str, body: str) -> str:
|
| 68 |
+
"""Sends an email via Gmail.
|
| 69 |
+
|
| 70 |
+
Args:
|
| 71 |
+
to: Recipient email address
|
| 72 |
+
subject: Email subject
|
| 73 |
+
body: Email body text
|
| 74 |
+
|
| 75 |
+
Returns:
|
| 76 |
+
Success or error message
|
| 77 |
+
"""
|
| 78 |
+
import base64
|
| 79 |
+
from email.mime.text import MIMEText
|
| 80 |
+
|
| 81 |
+
service = get_gmail_service()
|
| 82 |
+
message = MIMEText(body)
|
| 83 |
+
message["to"] = to
|
| 84 |
+
message["subject"] = subject
|
| 85 |
+
|
| 86 |
+
raw = base64.urlsafe_b64encode(message.as_bytes()).decode()
|
| 87 |
+
service.users().messages().send(
|
| 88 |
+
userId="me", body={"raw": raw}
|
| 89 |
+
).execute()
|
| 90 |
+
|
| 91 |
+
return f"Email sent to {to} successfully!"
|
| 92 |
+
|
| 93 |
@tool
|
| 94 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 95 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 128 |
|
| 129 |
agent = CodeAgent(
|
| 130 |
model=model,
|
| 131 |
+
tools=[final_answer],[read_latest_emails,send_email], ## add your tools here (don't remove final answer)
|
| 132 |
max_steps=6,
|
| 133 |
verbosity_level=1,
|
| 134 |
grammar=None,
|