CoolFace
Apppublic

Supreetha15/pii-safe-classifier

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

๐Ÿง  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.py using 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.py requirements.txt Dockerfile README.md sbert_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

  1. 1.Accepts raw email text (email_body)
  2. 2.Detects and masks PII using regex and NER
  3. 3.Classifies the masked email into 1 of 4 categories:
  4. 4.Incident, Request, Problem, Change
  5. 5.Gives the position of the entity like email position,full_name position etc.
  6. 6.Demasks PII for final response
  7. 7.Returns output in strict JSON schema

โš™๏ธ How to work(Using Swagger UI)

  1. 1.Use this url "https://supreetha15-pii-safe-classifier.hf.space/docs"
  2. 2.This will redirect to Swagger UI.
  3. 3.Use POST and the route by default will be /classify.
  4. 4.Check on Try Out to to evaluate the API bu giving a raw email text in email_body
  5. 5.Click execute to run the API and fetch the result.

Other Evaluation methods

  1. 1.Use this url "https://supreetha15-pii-safe-classifier.hf.space/classify"
  2. 2.Go to POSTMAN and set request to POST
  3. 3.In body select raw JSON and provide a raw email text as {"email_body":"string"}
  4. 4.Send the request in order to get the API response
  5. 5.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