One aspect of the scenario is creating a machine learning model. It must be usable in the real world and available to consumers and developers.
The simplest and most popular way to deploy machine learning models is to enclose them in a REST API.
With a popular library called FastAPI, that’s exactly what we’ll accomplish today.
But, what is FastAPI?
The FastAPI Python web framework was created from the ground up to take advantage of contemporary Python capabilities.
For asynchronous, concurrent communication with clients, it adheres to the ASGI standard, while it is also capable of using WSGI.
Endpoints and routes can both employ async functions. Additionally, FastAPI enables the productive creation of web apps in type-hinted, clean, contemporary Python code.
FastAPI’s main use case is, as the name suggests, creating API endpoints.
Using the OpenAPI standard, which includes an interactive Swagger UI, or providing Python dictionary data as JSON are both simple ways to achieve this. However, FastAPI is not only for APIs.
It can be used to offer standard web pages using the Jinja2 template engine and to serve apps utilizing WebSockets, in addition to pretty much everything else a web framework can do.
In this article, we’ll develop a straightforward machine learning model and then use FastAPI to deploy it. Let’s begin.
Installation of FastAPI and creation of the first API
Installing the library and an ASGI server is required first; either Uvuicorn or Hypercorn will work. It works by entering the following command into the Terminal:
Now that the API has been created, you can use your preferred code editor and browse through it. Create a Python script called ml_model.py to get started. You are welcome to give yours a different name, but for the sake of this post, I will refer to this file as ml_model.py.
To create a straightforward API with two endpoints, you must complete the following tasks:
- Import the FastAPI and Uvicorn libraries.
- Set up a FastAPI class instance.
- Declare the first route, which, on the index page, produces a straightforward JSON object.
- Declare the second route, which provides a straightforward JSON object with a customized message. The name parameter is taken straight from the URL (for instance, https://127.0.0.1:8000/Jay).
- Utilize Uvicorn to run the API.
Implementing these five stages is shown in the following bit of code ie. creating a simple API
All done! Let’s launch our API immediately. Open a Terminal window next to the ml model.py file to accomplish this. Next, enter the following:
the Enter key. Before moving on, let’s debunk this assertion. The first app makes use of the Python file name alone, without the extension. The second app must have the same name as your FastAPI instance.
By using -reload, you tell the API that you want it to automatically reload when you save the file rather than beginning from scratch.
Now launch a browser and navigate to https://127.0.0.1:8000; the outcome should appear as follows:
You now understand how to create a simple API using FastAPI.
Building and training the Machine Learning model
Without collecting or analyzing any data, we will just train a simple model. These are unrelated to the deployment of models and are not essential to the topic at hand.
A model based on the Iris dataset can be installed using the same neural network installation method.
And we’ll do just that: download the Iris dataset and train the model. That won’t be simple. To begin, make a file named jaysmlmodel.py.
In it, you’ll do the following:
- Imports — You’ll need pandas, scikit-RandomForecastClassifier, learn’s pydantic’s BaseModel (you’ll discover why in the following step), and joblib for storing and loading models.
- Declare an IrisSpecies class that inherits from the base model. This class only contains fields needed to forecast a single flower species (more on that in the next section)
- Create a class. IrisModel is a model training and prediction tool.
- Declare a method named _train model within IrisModel. It is used to train models using the Random Forests technique. The trained model is returned by the procedure.
- Declare a predicted species function inside IrisModel. It is utilized to forecast based on 4 input factors (flower measurements). Both the forecast (flower species) and the prediction probability are returned by the algorithm.
- Change the constructor in IrisModel so that it loads the Iris dataset and trains the model if it’s missing from the folder. This solves the problem of repeatedly training new models. The joblib library is utilized for model loading and saving.
Here is the whole code:
I hope the above list and the comments made it easy to grasp even though this was a sizable amount of code to create. Now that this model has been developed, let’s publish its prediction capabilities over a REST API.
Constructing a full REST API
Return to the ml_model.py file and purge all of the data. The boilerplate will be essentially the same as what you had before, but we should start over with a blank file.
You will only define one endpoint this time, which is the one utilized to determine the type of flower. IrisModel.predict species(), which was declared in the preceding section, is called by this endpoint to carry out the prediction.
The request type is the other big change. In order to transmit parameters in JSON rather than URL, it is recommended that you use POST when using machine learning APIs.
The above sentence may have sounded like gibberish if you are a data scientist, but that’s alright. To design and deploy models, one does not necessarily need to be an expert on HTTP requests and REST APIs.
The tasks for ml model.py are few and straightforward:
- You must import the following from the previously created jaymlmodel.py file: uvicorn, FastAPI, IrisModel, and IrisSpecies.
- Create instances of FastAPI and IrisModel.
- Declare a function at https://127.0.0.1:8000/predict to make predictions.
- The IrisModel.predict species() method receives an object of type IrisSpecies, transforms it to a dictionary, and then returns it. Returns are the expected class and predicted probability.
- Use uvicorn to execute the API.
Yet again, here is the entire file’s code together with its comments:
That is all you need to do. In the next step, let’s test the API.
Testing the API
Re-enter the following line into the Terminal to execute the API: uvicorn ml_model:app –reload
This is how the documentation page appears:
So that’s it for today. In the part after this, let’s conclude.
Conclusion
Today, you learned what FastAPI is and how to use it, using both a simple API example and a simple machine learning example. You’ve also learned how to create and view API documentation, as well as how to test it.
That’s a lot for a single piece, so don’t be surprised if it takes a few readings to properly understand.
Happy coding.
Leave a Reply