In the real world, once we have a machine learning pipeline setup, and ready to go. The last thing to do is to create a web app for the customer/web dev team to use. This post focuses on how to do build a ML web app. We’ll talk about all of the different libraries that companies use as of today, so once hired you can “hit the ground running”, or just come back to this post on a future date.
Table of Contents
REST APIs
FastAPI
Streamlit
Flask
1 - REST APIs
In the world of Machine Learning Operations (MLOps), deploying models as web applications often involves building a REST API. This allows for seamless interaction between the model and the user interface. Understanding APIs, especially REST APIs, is key to making your ML models accessible and usable in practical scenarios.
An Application Programming Interface (API) is a set of rules and definitions that allows different software applications to communicate with each other. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, and the conventions to follow.
1.1 Understanding REST APIs
REST, or Representational State Transfer, is an architectural style for providing standards between computer systems on the web, making it easier for systems to communicate with each other. RESTful systems are characterized by how they are stateless and separate the concerns of client and server.
Key Characteristics of REST APIs:
Stateless: Each request from a client to a server must contain all the information needed to understand and complete the request. The server should not store any state about the client session.
Uniform Interface: A uniform interface is maintained so that the same set of operations is used throughout the API. This simplifies and decouples the architecture, which enables each part to evolve independently.
Client-Server Architecture: The client and the server are independent of each other, allowing each to be developed and replaced independently.
1.2 GET and POST requests
A GET request is used to request data from a specified resource. In the context of an ML web app, a GET request could be used to retrieve information about a model or fetch predictions based on predefined parameters.
For example, a simple GET request in Python using the requests
library might look like:
import requests
response = requests.get('http://example.com/api/CREDENTIALs')
print(response.json())
A POST request is used to send data to a server to create or update a resource. In ML web apps, POST requests are commonly used to send input data to a model and receive predictions.
Example of a POST request in Python:
import requests
data = {'input': [feature_values]}
response = requests.post('http://example.com/api/predict', json=data)
print(response.json())
Here, data is sent to the server, which processes it through an ML model and returns a prediction. Here is a visual you can look at as well
1.3 Postman
Postman is a popular tool for testing APIs. It provides a user-friendly interface for sending requests to APIs, viewing responses, and helps in debugging. It supports various types of HTTP requests such as GET, POST, PUT, DELETE and includes features for setting headers, body, parameters, etc.
Using Postman with REST APIs:
Create a New Request: Open Postman and click on 'New' to create a new request.
Set the Request Type and URL: Choose the type of request (GET, POST, etc.) and enter the URL of the API endpoint.
Configure the Request: Depending on your needs, you may add headers, query parameters, or body data. For instance, for a POST request, you might add input data as JSON in the body.
Send and Analyze the Response: Send the request and view the response. Postman provides a detailed view of the response data, headers, and status code, which can be invaluable for debugging and understanding the API's behavior.
Here is what Postman looks like if you’ve never used it before.
2 - FastAPI
In the dynamic field of Machine Learning Operations (MLOps), deploying machine learning models as web applications rapidly and efficiently is crucial. FastAPI, a modern, fast web framework for building APIs with Python, has become increasingly popular for this purpose. This section explores what FastAPI is, its advantages, and how to build a simple ML-based web app using it.
FastAPI is a Python framework that makes it easy to build APIs. It is built on standard Python type hints, which means that the code is not only easy to write but also robust and scalable. FastAPI integrates seamlessly with Python data science stack, making it an excellent choice for deploying machine learning models.
Key Features:
Fast to Run: Performance is on par with NodeJS and Go, thanks to Starlette for the web parts and Pydantic for the data parts.
Fast to Code: Features like automatic request validation and serialization reduce development time.
Interactive API Documentation: Automatic interactive API documentation using Swagger UI and ReDoc.
2.2 Why FastAPI over Django?
Keep reading with a 7-day free trial
Subscribe to Data Science & Machine Learning 101 to keep reading this post and get 7 days of free access to the full post archives.