This Python application uses the Gmail API and OpenAI API to summarize the last 10 emails in your Gmail account.
- ๐ Fetches the most recent 10 emails from your Gmail account.
- ๐ฉ Retrieves and cleans the email content (text and HTML).
- ๐ค Summarizes the email content using OpenAI's GPT-based API.
mkdir gmail-ai-agent
cd gmail-ai-agent
python -m venv ./venv
source ./venv/bin/activate
pip install google-auth-oauthlib google-auth-httplib2 google-api-python-client beautifulsoup4 openai- ๐ Go to Google Cloud Console and create a new project.
- โ Enable the Gmail API for your project.
- ๐ Configure the OAuth Consent Screen:
- Fill in the required details.
- Add the scope
https://www.googleapis.com/auth/gmail.readonly.
- ๐ Create credentials for the OAuth client:
- Choose Desktop App as the application type.
- Download the credentials file and rename it to
credentials.json.
- โ๏ธ Create an account on OpenAI.
- ๐ Generate an API key from the OpenAI API Portal.
- ๐ง Export your OpenAI API key as an environment variable:
export OPENAI_API_KEY='your-openai-api-key'โถ๏ธ Run the application:
python app.py- Authenticate with Gmail API:
- Handles OAuth2 authentication.
- Stores and refreshes credentials using
token.pickle.
- Fetch Recent Emails:
- Retrieves up to 10 recent emails from your Gmail inbox.
- Extracts subject, sender, date, and content.
- Clean and Summarize Content:
- Parses and cleans email content using BeautifulSoup.
- Summarizes email content using OpenAI's GPT model.
- Output Summaries:
- Displays a concise summary of each email.
- ๐ Ensure that
credentials.jsonis in the same directory as the Python script. - ๐ The
token.picklefile will be created during the first run to store authentication tokens. - ๐ Ensure your OpenAI API key is kept secure and not hardcoded in the script.
This project is licensed under the MIT License. Feel free to modify and distribute it as needed.