This project demonstrates a practical application of a fine-tuned Large Language Model (LLM) for corporate data classification, with a strong focus on on-premise deployment and data privacy. It showcases a complete solution, from model fine-tuning to a secure, multi-component application architecture.
Model link : https://huggingface.co/darshandugar/MailClassifier-DistilBERT
- Custom LLM Fine-tuning: A pre-trained
distilbert-base-uncasedmodel is fine-tuned on a custom dataset to categorize corporate emails. - Data Augmentation: Addresses data imbalance issues by generating high-quality synthetic data using Groq's LLM, ensuring robust model performance on all categories.
- On-Premise Deployment: The entire system is designed to run on a local server, guaranteeing that sensitive data never leaves your controlled network.
- Secure Communication: Implements a basic encryption scheme for data packets sent between the application's backend and the model inference server, upholding a chain of data privacy.
- Layered Architecture: The system is composed of a decoupled Python model server, a Spring Boot backend API, and a basic HTML/JS frontend for a clear and maintainable design.
This project utilizes a full fine-tuning approach. This technique is a standard and effective method for adapting a pre-trained model to a specific task.
- Loading a Pre-trained Model: The code loads the
distilbert-base-uncasedmodel. This model has already learned a deep understanding of general language from a massive corpus of text. - Modifying the Output Layer: When the model is loaded with
AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=num_labels), its original output layer is replaced with a new, randomly initialized layer. This new layer is specifically configured to output a prediction for each of the unique labels in our dataset. - Training the Entire Model: The
transformers.Trainerclass then trains the model. Crucially, in full fine-tuning, all of the model's parameters—both in the original pre-trained layers and the new output layer—are updated. This allows the model to deeply adapt its entire learned structure to the specific task of classifying corporate emails.
- Dataset Used: The model is trained on the
infinite-dataset-hub/CorporateMailCategorizationdataset. - Addressing Imbalance: Recognizing that some labels had very few instances, a key step was to augment the training data. A custom script was developed to use a powerful LLM (Groq) to generate additional synthetic examples for these under-represented categories. This balanced the dataset, which is crucial for achieving good and fair performance across all email categories.
The system follows a three-tier architecture:
- Frontend: A simple HTML/JavaScript UI allows users to submit an email query.
- Backend: A Spring Boot application serves as the main API. It handles user requests, creates encrypted data packets, and communicates with the model server.
- Model Server: A Python-based server powered by FastAPI hosts the fine-tuned LLM. It receives encrypted data, makes predictions, and sends the encrypted response back to the backend.
- Clone the repository:
git clone [https://github.com/your-username/Corporate-Mail-Categorizer-POC.git](https://github.com/your-username/Corporate-Mail-Categorizer-POC.git) cd Corporate-Mail-Categorizer-POC - Set up the Python environment:
# Assuming you have a setup.sh script for this ./setup.sh - Copy your fine-tuned model:
After training the model using the provided notebooks, copy the saved model files (the
mail_categorydirectory) intomodel/saved_model/.
- Start the Python Model Server:
cd model source venv/bin/activate python app.py
- Start the Spring Boot Backend:
cd ../spring-boot-backend ./mvnw spring-boot:run - Open the UI:
Open
frontend-ui/index.htmlin your web browser.