An intelligent AI agent that automatically reads, classifies, and drafts replies to incoming corporate emails. Built with Python, LangGraph, and Hugging Face Transformers (BERT & Gemma).
You can find and use the model on the Hugging Face Hub here:
darshandugar/MailClassifier-DistilBERT & darshandugar/Corporate-Email-Response-Generator-gemma-2b
- Automated Email Polling: Continuously monitors a Gmail inbox for new, unread emails.
- Intent Classification: Uses a fine-tuned BERT model to classify the intent of each email (e.g., "Merger Announcement," "Sustainability Initiative").
- Dynamic Workflow: Leverages LangGraph to route tasks based on the classified intent.
- Contextual Reply Generation: Uses a fine-tuned Gemma-2B model (with LoRA adapters) to generate context-aware, professional draft replies.
- End-to-End Automation: Automatically sends the generated reply and marks the original email as read.
This project uses LangGraph to create a stateful graph that mimics a decision-making process.
- The main loop in
recieve_mail.pypolls the Gmail inbox. - When an unread email is found, its content is passed to the
run_workflowfunction frommain_graph.py. - LangGraph Workflow Begins:
- Node 1:
classify_intent: The email content is processed by theIntentClassifier(fine-tuned BERT) to determine its purpose. - Conditional Edge: Based on the intent, the graph routes to a specific handler node (e.g.,
handle_merger). - Node 2:
handle_...: The handler node processes the request, gathers necessary details (e.g., pulling RAG data, creating a ticket ID). - Node 3:
generate_response: The intent and details are passed to theEmailGenerator(fine-tuned Gemma-2B), which drafts a complete, formal reply.
- Node 1:
- The final reply (subject and body) is returned to
recieve_mail.py. - The script uses the Gmail API to send the generated reply and marks the original email as read.
.
├── .gitignore # Tells Git what to ignore
├── config.py # Central config for model paths
├── intent_classify.py # Class for the BERT intent classifier
├── main_graph.py # Defines the core LangGraph workflow
├── recieve_mail.py # Main script: handles Gmail auth, polling, & sending
├── reply_generator.py # Class for the Gemma reply generator
├── requirements.txt # List of Python dependencies
│
├── credentials.json # YOUR Google API credentials (Must add)
├── token.json # Generated by Google Auth (Ignore)
│
├── mail_category/ # YOUR fine-tuned BERT model files
│ ├── config.json
│ ├── pytorch_model.bin
│ └── ...
│
└── fine_tuned_gemma_2b_adapters/ # YOUR fine-tuned Gemma adapter files
├── adapter_config.json
├── adapter_model.safetensors
└── ...
git clone https://github.com/YourUsername/Corporate-Mail-Assistant.git
cd Corporate-Mail-Assistant- Go to the Google Cloud Console.
- Create a new project.
- Enable the Gmail API.
- Create credentials for an "OAuth client ID".
- Select "Desktop app" as the application type.
- Download the JSON credentials file and save it in the project root as
credentials.json.
This project assumes you have already fine-tuned your models.
- BERT Intent Model: Place your fine-tuned BERT model files into the
mail_categoryfolder. - Gemma Adapters: Place your fine-tuned Gemma LoRA adapters into the
fine_tuned_gemma_2b_adaptersfolder.
Install all the required Python packages:
pip install -r requirements.txt-
First-time Authentication:
- Run the main script from your terminal:
python recieve_mail.py
- Your browser will automatically open, asking you to authorize the application to access your Gmail account.
- Grant permissions. The script will create a
token.jsonfile to store your credentials for future runs.
- Run the main script from your terminal:
-
Run the Assistant:
- Once authenticated, the script will start running:
--- Configuration Loaded (Local Paths) --- ... --- LANGGRAPH WORKFLOW COMPILED --- --- LOADING MODELS (This happens once) --- ... --- MODELS LOADED --- Starting mail attender service... Service running. Ignoring emails received before startup. Checking for new mail every 30 seconds... - The assistant is now active. When a new email arrives in the authorized inbox, it will process it automatically.
- Once authenticated, the script will start running:
