Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Comment

fastapi

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}
Comment

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]}
Comment

fastapi

Best framework
Comment

how to use fastapi

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
async def read_item(item_id: int, q: Union[str, None] = None):
    return {"item_id": item_id, "q": q}
Comment

PREVIOUS NEXT
Code Example
Python :: pandas df tail 
Python :: how to unimport a file python 
Python :: boto 3 list EMR 
Python :: pandas find fifth caracter in field and change cell based on that number 
Python :: fix the debug_mode = false django 
Python :: get date only from datetimefiel django 
Python :: how to for loop in python stackoverflow 
Python :: round to decimal places python 
Python :: python Using for loop and list comprehension 
Python :: convert 2 lists into dictionary 
Python :: char list python 
Python :: label binarizer 
Python :: python MAX_INT 
Python :: qr code scanner using opencv 
Python :: convert series to dataframe pandas 
Python :: model checkpoint 
Python :: plot dataframe 
Python :: docstring in python 
Python :: split a column in pandas 
Python :: Async-Sync 
Python :: how to find greatest number in python 
Python :: loops in python 
Python :: numpy.sort 
Python :: loading bar in python 
Python :: include app in django project 
Python :: time conversion 
Python :: when iterating through a pandas dataframe using index, is the index +1 able to be compared 
Python :: how to index lists in python 
Python :: pandas df number of columns 
Python :: list dataframe to numpy array 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =