Skip to content

Code for my Medium article: "How you can quickly deploy your ML models with FastAPI"

Notifications You must be signed in to change notification settings

diana-corona/FastAPISpamDetection

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 

Repository files navigation

FastAPISpamDetection

Spam Text Message Classification Data

Obtained from

https://www.kaggle.com/team-ai/spam-text-message-classification

Using the model on Jupyter notebooks

A Jupiter notebook demo with the models bb is available in FastAPISpamDetection_demo.ipynb

Deploying the App on Docker

The docker file:

  1. Pulls the FastAPI docker image.
  2. Copies the app directory to the docker image.
  3. Set the app directory as the working directory.
  4. Install requirements listed in requirements.txt
  5. Train the models

To build the docker image run (this will train the model)

docker build -t spam-detection .

To run the docker image run (this will deploy the API)

docker run -d --name spam-detection -p 80:80 spam-detection

API Automatically Generated Documentation

The API documentation is generated automatically by FASTAPI, To access the API documentation go to

http://localhost/docs

Query Parameters

Query parameters are part of the URL string and are prefixed by a “?”

For example:

http://localhost/spam_detection_query/?message=WINNER

Path Variables

In Path Variables, the input data is passed to the API as a path in the URL

For example:

http://localhost/spam_detection_path/Hi

Data preprocesing

The data was preprocessed by

  1. Removing HTML markup tags
  2. Removing punctuation marks
  3. Removing capital letters

Models accuracy

The data was randomly separed in using sklearn train_test_split. 80% of the data was used to train 20% of the data was used to test the model and get the accuracy

  1. MLPClassifier has an accuracy of 98.29%
  2. KNeighbors has an accuracy of 93.36%
  3. DecisionTreeClassifier has an accuracy of 95.96%
  4. RandomForestClassifier has an accuracy of 97.04%