fastapi
pip install fastapi
pip install uvicorn # ASGI server
pip install starlette # lightweight ASGI framework/toolkit
pip install pydantic # Data validation and type annotations
# OR
pip install fastapi uvicorn starlette pydantic
fastapi
pip install fastapi
pip install uvicorn # ASGI server
pip install starlette # lightweight ASGI framework/toolkit
pip install pydantic # Data validation and type annotations
# OR
pip install fastapi uvicorn starlette pydantic
How to create FastApi
from fastapi import FastAPI
import uvicorn
from sklearn.datasets import load_iris
from sklearn.naive_bayes import GaussianNB
from pydantic import BaseModel
# Creating FastAPI instance
app = FastAPI()
# Creating class to define the request body
# and the type hints of each attribute
class request_body(BaseModel):
sepal_length : float
sepal_width : float
petal_length : float
petal_width : float
# Loading Iris Dataset
iris = load_iris()
# Getting our Features and Targets
X = iris.data
Y = iris.target
# Creating and Fitting our Model
clf = GaussianNB()
clf.fit(X,Y)
# Creating an Endpoint to receive the data
# to make prediction on.
@app.post('/predict')
def predict(data : request_body):
# Making the data in a form suitable for prediction
test_data = [[
data.sepal_length,
data.sepal_width,
data.petal_length,
data.petal_width
]]
# Predicting the Class
class_idx = clf.predict(test_data)[0]
# Return the Result
return { 'class' : iris.target_names[class_idx]}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us