Team Ai
Apppublic

Sejal1908/email-classification-api

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
App README

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

  1. 1.Build the Docker image: docker build -t email-classification-api .
  1. 1.Run the Docker container: docker run -p 7860:7860 email-classification-api
  1. 1.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