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 Bayes
andCountVectorizer
- Language restriction (supports only English)
- Model training script included
- Uses
.venv
for 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-api
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install -r requirements.txt
Download the spam.csv
dataset (e.g., from Kaggle) and place it in the data
directory if missing.
python train_model.py
This script reads spam.csv
, trains the model, and saves model.pkl
inside the models/
directory.
python run.py
The 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