Live demonstration of building an ML web application with FastAPI

Posted by


In this tutorial, we will walk through the process of creating a Machine Learning (ML) web application using FastAPI. FastAPI is a modern web framework for building APIs with Python that is fast and easy to use. By the end of this tutorial, you will have a basic web application that can receive input data, run it through an ML model, and return the prediction to the user.

To get started, make sure you have Python installed on your machine. You can install FastAPI and other required libraries by running the following command in your terminal:

pip install fastapi uvicorn numpy pandas scikit-learn

Next, create a new Python file called app.py and import the necessary libraries:

from fastapi import FastAPI
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from pydantic import BaseModel

Now, create an instance of the FastAPI class:

app = FastAPI()

Next, let’s define a class called InputData which will act as the input data model for our ML model:

class InputData(BaseModel):
    feature1: float
    feature2: float
    feature3: float

Now, let’s define a dummy ML model that we will use for demonstration purposes. In a real-world scenario, you would train your model on actual data.

# Define a dummy ML model
model = RandomForestClassifier()
X = np.random.rand(100, 3)
y = np.random.randint(0, 2, 100)

model.fit(X, y)

Next, let’s create a route that will accept input data, make a prediction using the ML model, and return the prediction to the user:

@app.post("/predict")
def predict(data: InputData):
    features = [data.feature1, data.feature2, data.feature3]
    prediction = model.predict([features])[0]
    return {"prediction": prediction}

Finally, we need to run the FastAPI application using Uvicorn. You can do this by running the following command in your terminal:

uvicorn app:app --reload

You should see output indicating that the FastAPI application is running. You can now navigate to http://localhost:8000/docs in your browser to access the Swagger UI. Here, you can test your ML model by providing input data and seeing the prediction returned by the API.

Congratulations! You have successfully created a Machine Learning web application using FastAPI. Feel free to further customize your application by adding more routes, implementing more complex ML models, or integrating with other technologies. Happy coding!

0 0 votes
Article Rating
26 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@lahadmbacke2054
1 month ago

Best Live coding I have never seen

@user-sh6jm9pz9s
1 month ago

I know this maybe a silly question but at 21:47 how do I know that the "item_id" is going to be "7"?

@anaareias2065
1 month ago

being able to run code from a .py file interactively in a jupyter notebook is what I've been searching for all this time!

@shanyachaubey1909
1 month ago

The way Sebastian explains such technical concepts is so awesome.

@mohammadirfandsa6804
1 month ago

Hi All, I am getting below error
—————————————————————————

TypeError Traceback (most recent call last)

Cell In[11], line 1

—-> 1 pipe = StableDiffusionPipeline.from_pretrained(

2 "CompVis/stable-diffusion-v1-4",

3 revision="fp16",

4 torch_dtype=torch.float16,

5 use_auth_token=token,

6 )

452 if issubclass(class_obj, class_candidate):

453 load_method_name = importable_classes[class_name][1]

–> 455 load_method = getattr(class_obj, load_method_name)

457 loading_kwargs = {}

458 if issubclass(class_obj, torch.nn.Module):

TypeError: attribute name must be string, not 'NoneType'

@WordsThroughTheSky
1 month ago

FYI, at 15:08, if you're having trouble with `image = pipe(prompt)['sample'][0]` as of 2024-01-03 it's `image = pipe(prompt)['images'][0]`

@haroldmunoz1775
1 month ago

Thanks Sebastian from Colombia

@mytube1000javed
1 month ago

Is there any sanity or logic in using this fast API? If you are using stable diffusion? Why one will not use stable diffusion directly?

@surajpratapsinghsayar7282
1 month ago

Kudos to DeepLearnigAI, great guy Sebastián

@santhoshkumarmatlapudi2851
1 month ago

Please reply if you see this, I am not able to connect clickhouse database from fastapi

@thisaintmyrealname1
1 month ago

FastAPI made me love backend development again.

@Ting-ChiangYi
1 month ago

I get an error message on the line
pipe.to("cuda")

The message is
AssertionError: Torch not compiled with CUDA enabled

Is it possible to run this demo without a CUDA enabled GPU?

I have been googling and trying various things for several hours but couldn't get it to work.
thank you

@Deeplearningai
1 month ago
@theflag2328
1 month ago

ipykernel doesn't seem to be enough anymore, tells me that I need the notebook package to run interactive cells

@anirbanc88
1 month ago

sebastian is so awesome!

@so_you_can_go
1 month ago

Guys, you promised the link to the repository. Please! 🙂

@RZRRR1337
1 month ago

Have you released the code yet?

@RZRRR1337
1 month ago

Does this work on AMD GPUs too?

@joaomaues
1 month ago

what theme in vscode is he using?

@zaheeruddinfaiz7064
1 month ago

7:29 Hi @taingolo you should get your laptop battery checked. It jumped from 6% to 37%. 😁