ML backend service for SMS spam detection
This Python Flask service provides the machine learning capabilities for SMS spam detection. It serves a trained decision tree model through a REST API that can classify SMS messages as "spam" or "ham" (not spam).
- REST API for SMS spam classification
- Pre-trained decision tree model
- Text preprocessing pipeline
- Model training scripts included
- Swagger/OpenAPI documentation
- Python 3.12+ (tested with 3.12.9)
- Dependencies listed in
requirements.txt
The service includes scripts to train the model from scratch using the SMS Spam Collection dataset.
To train the model, you have two options. Either you create a local environment...
$ python -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
... or you train in a Docker container (recommended):
$ docker run -it --rm -v ./:/root/sms/ python:3.12.9-slim bash
... (container startup)
$ cd /root/sms/
$ pip install -r requirements.txt
Once all dependencies have been installed, the data can be preprocessed and the model trained by creating the output folder and invoking three commands:
$ mkdir output
$ python src/read_data.py
Total number of messages:5574
...
$ python src/text_preprocessing.py
[nltk_data] Downloading package stopwords to /root/nltk_data...
[nltk_data] Unzipping corpora/stopwords.zip.
...
$ python src/text_classification.py
The resulting model files will be placed as .joblib files in the output/ folder.
To make the models accessible, you need to start the microservice by running the src/serve_model.py script from within the virtual environment that you created before, or in a fresh Docker container (recommended):
$ docker run -it --rm -p 8081:8081 -v ./:/root/sms/ python:3.12.9-slim bash
... (container startup)
$ cd /root/sms/
$ pip install -r requirements.txt
$ python src/serve_model.py
The server will start on port 8081.
Once its startup has finished, you can either access localhost:8081/apidocs in your browser to interact with the service, or you send POST requests to request predictions, for example with curl:
$ curl -X POST "http://localhost:8081/predict" -H "Content-Type: application/json" -d '{"sms": "test ..."}'
{
"classifier": "decision tree",
"result": "ham",
"sms": "test ..."
}