Skip to content

supr1yo/spam-detection-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spam Detection API

flask pandas sklearn

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.


Features

  • RESTful API built with Flask
  • Spam detection using Multinomial Naive Bayes and CountVectorizer
  • Language restriction (supports only English)
  • Model training script included
  • Uses .venv for dependency isolation

File Structure

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

Setup Instructions

You can use Docker to build and run this project or follow the steps to setup the project on your machine.

1. Clone the repo

git clone https://github.com/supr1yo/spam-detection-api.git
cd spam-detection-api

2. Set up the virtual environment

python -m venv .venv
source .venv/bin/activate      # On Windows: .venv\Scripts\activate

3. Install required packages

pip install -r requirements.txt

4. Add dataset (if missing)

Download the spam.csv dataset (e.g., from Kaggle) and place it in the data directory if missing.


Train the Model

python train_model.py

This script reads spam.csv, trains the model, and saves model.pkl inside the models/ directory.


Run the API

python run.py

The API will start at http://127.0.0.1:5000/


API Endpoint

POST /predict

Classifies a message as spam or not.

Request

{
    "message": "Free $50 steam points"
}

Response

{
    "label": "spam",
    "message": "Free $50 steam points"
}

Returns 400 Bad Request if:

  • Message is missing from request body

Model Info

  • Algorithm: Multinomial Naive Bayes
  • Feature Extraction: CountVectorizer

License

MIT License. See LICENSE file for details.


Author

Built with 💙 by supr1yo

About

A simple machine learning-based API to detect whether a message is spam or not.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published