This project provides a modular Python application that connects to your Gmail account, analyzes your emails using a local Ollama LLM instance, and moves unnecessary emails to a separate label.
- Gmail Integration: Securely connects to your Gmail account using OAuth2
- Smart Email Analysis: Uses local Ollama API to evaluate email content
- Customizable Filters: Define what makes an email necessary or unnecessary
- Label Management: Creates and manages labels in your Gmail account
- Detailed Logging: Comprehensive logging for monitoring and debugging
- Result Tracking: Saves processing results for future reference
- Python 3.7+
- Gmail account
- Google Cloud project with Gmail API enabled
- Ollama installed locally (https://ollama.ai)
-
Clone the repository:
git clone https://github.com/subhashdasyam/ai-gmail-filter.git cd ai-gmail-filter -
Install the required dependencies:
pip install -r requirements.txt -
Create a
.envfile by copying the example:cp .env.example .env -
Edit the
.envfile to configure the application.
-
Create a Google Cloud Project:
- Go to https://console.cloud.google.com/
- Create a new project or select an existing one
- Enable billing for the project (required for API usage)
-
Enable the Gmail API:
- Go to https://console.cloud.google.com/apis/library
- Search for "Gmail API" and select it
- Click "Enable"
-
Create OAuth 2.0 credentials:
- Go to https://console.cloud.google.com/apis/credentials
- Click "Create Credentials" and select "OAuth client ID"
- Select "Desktop app" as the application type
- Name your credentials and click "Create"
- Download the credentials JSON file
- Rename it to "credentials.json" and place it in the project root directory
- Install Ollama from https://ollama.ai
- Start the Ollama service
- Pull the model you want to use:
or any other model you prefer, like:
ollama pull llama2ollama pull mistral - Ensure Ollama is running before starting the application
Run the application:
python main.py
On first run, the script will open a browser window for you to authenticate with your Gmail account. After authentication, the script will:
- Fetch emails from your inbox
- Analyze each email using the local Ollama LLM
- Move unnecessary emails to the "potential-unnecessary" label (or the label name you configured)
- Save processing results to a file
You can customize the behavior of the application by editing the .env file:
MAX_EMAILS: Maximum number of emails to process at onceOLLAMA_MODEL: The LLM model to use for email analysis (must be pulled into Ollama first)LABEL_NAME: Name of the label for unnecessary emailsBODY_PREVIEW_LENGTH: Length of email body to include in analysisLOG_LEVEL: Logging verbosity (DEBUG, INFO, WARNING, ERROR)
Contributions are welcome! Please feel free to submit a Pull Request.