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!
Best Live coding I have never seen
I know this maybe a silly question but at 21:47 how do I know that the "item_id" is going to be "7"?
being able to run code from a .py file interactively in a jupyter notebook is what I've been searching for all this time!
The way Sebastian explains such technical concepts is so awesome.
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'
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]`
Thanks Sebastian from Colombia
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?
Kudos to DeepLearnigAI, great guy Sebastián
Please reply if you see this, I am not able to connect clickhouse database from fastapi
FastAPI made me love backend development again.
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
Check out our GitHub Repo HERE: https://github.com/FourthBrain/FastAPI-for-Machine-Learning-Live-Demo
ipykernel doesn't seem to be enough anymore, tells me that I need the notebook package to run interactive cells
sebastian is so awesome!
Guys, you promised the link to the repository. Please! 🙂
Have you released the code yet?
Does this work on AMD GPUs too?
what theme in vscode is he using?
7:29 Hi @taingolo you should get your laptop battery checked. It jumped from 6% to 37%. 😁