Sejal1908/email-classification-api
emailClassificationsupport_Sejal
A Python-based Flask API that detects and masks Personally Identifiable Information (PII) in email content and classifies the email into relevant categories using a machine learning model. This project is deployed via Hugging Face Spaces.
๐ Demo Paste your email content into the API via a POST request, and get back a masked version with all sensitive entities hidden, alongside the predicted email category.
๐ Features โ Regex and NER-based PII Masking (name, email, phone number, DOB, etc.)
๐ค ML-based Email Classification (e.g., General, Finance, Promotions)
๐ก REST API built using Flask
๐งช Ready-to-test JSON response format
โ๏ธ Hosted on Hugging Face Spaces with GitHub integration
๐ Project Structure
. โโโ app.py # Main Flask app entrypoint โโโ api.py # API routes and logic โโโ data/ โโโ emails.csv # the Data set โโโ models.py # Classification logic and ML model โโโ utils.py # PII masking functions โโโ trainmodel.py # Script to train and save ML model โโโ requirements.txt # Required Python dependencies โโโ README.md # You're here! โโโ space.yaml # Hugging Face deployment file โโโ testinput.json # Sample input for testing โโโ model/ โโโ email_classifier.pkl # Pretrained ML model
๐ง Technologies Used Python 3.10
Flask
Scikit-learn
Regex + SpaCy (for PII detection)
Docker + Hugging Face Spaces
GitHub (CI/CD)
๐ง Installation (Run Locally)
# Clone the repository git clone https://github.com/Sejal1908/email-classification-api.git cd email-classification-api
Create virtual environment (optional but recommended)
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
Install dependencies
pip install -r requirements.txt
Run the app
python app.py
๐งช API Usage
I have used postman for this project
โถ๏ธ Endpoint POST /classify_email
โถ๏ธ Request Headers pgsql Copy code Content-Type: application/json
โถ๏ธ Request Body json Copy code { "email": "Contact Alice Wonderland at alice@wonder.land or +91 98765 43210. DOB: 01/01/1980." }
โ Successful Response Format json Copy code { "inputemailbody": "Contact Alice Wonderland at alice@wonder.land or +91 98765 43210. DOB: 01/01/1980.", "listofmaskedentities": [ { "position": [0, 24], "classification": "fullname", "entity": "Contact Alice Wonderland" }, { "position": [28, 45], "classification": "email", "entity": "alice@wonder.land" }, { "position": [49, 64], "classification": "phonenumber", "entity": "+91 98765 43210" }, { "position": [71, 81], "classification": "dob", "entity": "01/01/1980" } ], "maskedemail": "[fullname] at [email] or [phonenumber]. DOB: [dob].", "categoryofthe_email": "General" }
โ๏ธ Hugging Face Deployment
Go to https://huggingface.co/spaces and click "Create Space"
Choose SDK as Docker
Select "Link to GitHub Repository"
Ensure you include a space.yaml or README.md for Hugging Face to build correctly
Push your code to GitHub โ it auto-deploys!
Example space.yaml
This file tells Hugging Face Spaces how to run your app
sdk: docker python_version: "3.10" --------------------------------------------------------------------------------------- ๐ง Training the Classifier
You can train and save your ML model using the provided train_model.py script.
bash Copy code python train_model.py This will generate a classifier.pkl saved in the /model folder which your app will load at runtime.
DockerFile
Use official Python image
FROM python:3.10
Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1
Set working directory
WORKDIR /code
Install dependencies
COPY requirements.txt . RUN pip install --upgrade pip RUN pip install -r requirements.txt
Copy all project files
COPY . .
Expose port
EXPOSE 7860
Run the app
CMD ["python", "app.py"]
Docker Setup
- Build the Docker image: docker build -t email-classification-api .
- Run the Docker container: docker run -p 7860:7860 email-classification-api
- Access the application on http://localhost:7860. -------------------------------------------------------------------------------------------- ๐ License
This project is licensed under the MIT License --------------------------------------------------------------------------------------------
๐ฉโ๐ป Author Sejal Vhankade ๐ Github ๐ LinkedIn โ๏ธ sejal.vhankade@gmail.com
