Recent Posts

FastApi


First thing first, Activate your python environment 

Now, Install fastapi and uvicorn 
  
         >> pip install fastapi uvicorn 


You should have an ML model file (.pkl, .model, .hd5 etc)

Let us assume that we have a "classifier.pkl" file


Create a main.py file 

#main.py

import uvicorn

from fastapi import FastAPI

from BankNotes import BankNote

import numpy as np

import pickle

import pandas as pd


app = FastAPI()

pickle_in = open("classifier.pkl","rb")

classifier=pickle.load(pickle_in)


@app.get('/')

def index():

    return {'message': 'Hello, World'}


@app.get('/{name}')

def get_name(name: str):

    return {'Welcome': f'{name}'}


@app.post('/predict')

def predict_banknote(data:BankNote):

    data = data.dict()

    variance=data['variance']

    skewness=data['skewness']

    curtosis=data['curtosis']

    entropy=data['entropy']

   # print(classifier.predict([[variance,skewness,curtosis,entropy]]))

    prediction = classifier.predict([[variance,skewness,curtosis,entropy]])

    if(prediction[0]>0.5):

        prediction="Fake note"

    else:

        prediction="Its a Bank note"

    return {

        'prediction': prediction

    }

#    Will run on http://127.0.0.1:8000

if __name__ == '__main__':

    uvicorn.run(app, host='127.0.0.1', port=8000)


Create a banknote.py file:

from pydantic import BaseModel

# Class which describes Bank Notes measurements

class BankNote(BaseModel):

    variance: float 

    skewness: float 

    curtosis: float 

    entropy: float


Now, In the terminal run the application 

        >>uvicorn main:app --reload

#   This will run on http://127.0.0.1:8000

No comments

If you have any doubts, Please let me know