Skip to content

hpanwar08/rest-api-ml-model

Repository files navigation

Build Status codecov

Rest API for deploying ML models ⚡

A microservice built on Flask to deploy ML models as rest api.
This code can be used as a boilerplate for your next machine learning project deployment. Replace project/sentiment/ml_models/model.py with your custom model architecture file.

Steps to run

  • Create virtual or conda environment and activate it Windows
    virtualenv restenv
    restenv\Scripts\activate
    Linux
    python3 -m venv restenv
    source restenv/bin/activate
  • Install dependencies
    Edit line 15-17 in requirements.txt according to your OS
    pip install -r requirements.txt
  • Install spacy model
    python -m spacy download en_core_web_sm
  • To start the microservice in dev environment
    python manage.py start --env=dev
  • To start the microservice in production environment
    python manage.py start
  • To access swagger api, open http://localhost:5000/api/v1/
  • To run tests
    python manage.py test
  • Code coverage
    python manage.py cov

Input JSON and output JSON

Sample input json body

URL: http://localhost:5000/api/v1/sentiment

{
	"text": "i am so happy"
}

Sample output json body

{
    "status": "success",
    "message": {
        "text": "i am so happy",
        "sentiment": "positive",
        "confidence": 0.9986
    }
}

Input json body (bulk predict)

URL: http://localhost:5000/api/v1/sentiments

{
    "texts": [
        {
            "text": "i am so happy"
        },
        {
            "text": "I am so SAD"
        },
        {
            "text": "today is a Good day"
        }
    ]
}

Output json body

{
    "status": "success",
    "message": [
        {
            "status": "success",
            "text": "i am so happy",
            "sentiment": "positive",
            "confidence": 0.9986
        },
        {
            "status": "success",
            "text": "I am so SAD",
            "sentiment": "negative",
            "confidence": 0.9993
        },
        {
            "status": "success",
            "text": "today is a Good day",
            "sentiment": "positive",
            "confidence": 0.9989
        }
    ]
}

Swagger API

URL: http://localhost:5000/api/v1/

Swagger API

Sentiment Prediction using /sentiment endpoint

Sentiment

Work in progress...

TODOs ⏰

  • add sentiment analysis model
  • update api
  • add tests
  • add logging
  • add load testing
  • add database to store predictions
  • dockerize the app
  • add authentication