Supreetha15/pii-safe-classifier
๐ง PII-Safe Email Classifier API
A FastAPI backend that classifies support emails using SBERT + SVM, while safely masking and restoring personally identifiable information (PII) using regex and SpaCy NER.
๐ง Tech Stack
- FastAPI โ Lightweight REST API backend
- SBERT (MiniLM-L6-v2) โ Sentence embeddings
- Linear SVM โ Email classification
- Regex + SpaCy โ PII detection & masking
- Joblib โ Model persistence
- Uvicorn โ ASGI server
๐ API Overview
- Framework:
FastAPI - Main file:
api.py - Model:
SBERT encoder + Linear SVM loaded via models.py - PII masking: Done in
utils.pyusing regex & SpaCy - Entry point:
app.py(used for Hugging Face Spaces or Docker)
โ๏ธ Hugging Face Spaces Deployment
- Create a Space and choose Docker as the SDK.
- Upload all project files:
app.py, api.py, models.py, utils.pyrequirements.txtDockerfileREADME.mdsbert_linear_model.joblib - Space will build automatically and give the following link:
https://<space-name>.hf.space/docs- Use Swagger UI or curl to test live requests.
๐ Module Documentation
1.app.py - Hugging Face Spaces entry point; exposes FastAPI app instance from api.py.
2.api.py - FastAPI route /classify for processing incoming emails: masks PII, classifies, and demasks.
3.models.py - Contains SBERTSVMClassifier class: training, saving, loading, and predicting using SBERT + SVM.
4.utils.py - Implements mask_pii() and demask() using advanced regex and SpaCy for secure entity handling.
5.requirements.txt - Python dependencies required to run the application (locally or in Hugging Face Space).
6.Dockerfile - Containerization file to deploy the API using Hugging Face's Docker SDK or locally with Docker.
7.README.md - Complete setup guide, documentation, deployment steps, and test instructions.
8.sbert_linear_model.joblib - Serialized classifier model file (must be pre-trained and included for prediction).
API End Point Details
POST /classify Content-Type: application/json
Body Schema: { "email_body": "string" }
โ๏ธ How It Works
- Accepts raw email text (
email_body) - Detects and masks PII using regex and NER
- Classifies the masked email into 1 of 4 categories:
Incident,Request,Problem,Change- Gives the position of the entity like email position,full_name position etc.
- Demasks PII for final response
- Returns output in strict JSON schema
โ๏ธ How to work(Using Swagger UI)
- Use this url "https://supreetha15-pii-safe-classifier.hf.space/docs"
- This will redirect to Swagger UI.
- Use POST and the route by default will be /classify.
- Check on Try Out to to evaluate the API bu giving a raw email text in
email_body - Click execute to run the API and fetch the result.
Other Evaluation methods
- Use this url
"https://supreetha15-pii-safe-classifier.hf.space/classify" - Go to POSTMAN and set request to
POST - In body select raw JSON and provide a raw email text as
{"email_body":"string"} - Send the request in order to get the API response
- Other Equivalent method can be using curl.
Acknowledgements
Built with using open-source tools like FastAPI, SBERT, and SpaCy. Deployed on Hugging Face Spaces using Docker SDK.
๐ License
This project is licensed under the MIT License โ you're free to use, modify, and distribute.
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
