A simple and fast Flask-based REST API that classifies incoming text messages as spam or ham (not spam) using a trained Machine Learning model built with scikit-learn.
- RESTful API built with Flask
- Spam detection using
Multinomial Naive BayesandCountVectorizer - Language restriction (supports only English)
- Model training script included
- Uses
.venvfor dependency isolation
spam-detection-api/
├── app/
│ ├── __init__.py # Flask app
│ ├── model.py # App routes
│ ├── model.py # Trained model function
├── models/
│ └── model.pkl # Saved trained model
├── data/
│ └── spam.csv # Dataset
├── train_model.py # Train the model
├── run.py # Runs the Flask App
├── requirements.txt # Project dependencies
└── README.md # This file
You can use Docker to build and run this project or follow the steps to setup the project on your machine.
git clone https://github.com/supr1yo/spam-detection-api.git
cd spam-detection-apipython -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activatepip install -r requirements.txtDownload the spam.csv dataset (e.g., from Kaggle) and place it in the data directory if missing.
python train_model.pyThis script reads spam.csv, trains the model, and saves model.pkl inside the models/ directory.
python run.pyThe API will start at http://127.0.0.1:5000/
Classifies a message as spam or not.
{
"message": "Free $50 steam points"
}{
"label": "spam",
"message": "Free $50 steam points"
}Returns 400 Bad Request if:
- Message is missing from request body
- Algorithm: Multinomial Naive Bayes
- Feature Extraction: CountVectorizer
MIT License. See LICENSE file for details.
Built with 💙 by supr1yo