My Brain Cells

Easiest (and best) learning materials for anyone with a curiosity for machine learning and artificial intelligence, Deep learning, Programming, and other fun life hacks.

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

Anthony

4 thoughts on “FastApi

  1. Hi There,

    I’m Richard. If you are a student then you must know that at college and university level we have to buy a lot of TextBooks according to the subject of the course which are very expensive. This makes it difficult for students who do not have much money to buy these books. There is also a huge gap between supply and demand of TextBooks. Publishers keep changing editions (without any real changes in the books themselves) and force professors to get the new editions. This results in an additional burden on students.

    So considering all these issues, We have launched our website CheapestDigitalBooks.com – We contacted authors and professors directly and received approval and agreement to sell the books digital( ePub/PDF) version directly to university and college students at a very cheap price. You can buy a book from our website at a discount of up to 70% off the price of a book from amazon.

    Our motto is simple: Education should be accessible and affordable for EVERYONE!

    If you are not student please share this website with all the students around you so that they might be benefited.

    Thanks You
    Richard C. Willis
    Marketing Consultant
    CheapestDigitalBooks.com

  2. Did you write the article yourself or you hired someone to do it?
    I was wondering because I am a site owner too and
    struggle with writing new content all the time.
    Someone told me to use AI to do create articles which I am kinda considering because the output
    is almost written by human. Here is the sample content they sent me
    https://sites.google.com/view/best-ai-content-writing-tools/home

    Let me know if you think I should go ahead and use AI.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top