ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Introduction to Machine Learning Deployment
Chapter 1: Getting Started with Model Deployment
What is Machine Learning Deployment?
Why Deploy Machine Learning Models?
The Machine Learning Workflow Overview
Types of Deployment Strategies (Introduction)
Challenges in Model Deployment
Quiz for Chapter 1
Chapter 2: Preparing Your Model for Deployment
Saving Trained Models
Introduction to Model Serialization
Using Pickle for Model Persistence
Using Joblib for Model Persistence
Handling Model Dependencies
Saving Preprocessing Steps
Hands-on Practical: Saving and Loading a Simple Model
Quiz for Chapter 2
Chapter 3: Creating a Prediction Service with Flask
What is an API?
Introduction to Web Frameworks
Setting Up Flask
Creating a Basic Flask Application
Loading Your Saved Model in Flask
Defining a Prediction Endpoint
Handling Input Data (JSON)
Returning Predictions
Testing Your API Locally
Hands-on Practical: Building a Simple Flask Prediction API
Quiz for Chapter 3
Chapter 4: Introduction to Containerization with Docker
What is Containerization?
Introduction to Docker
Docker Concepts: Images and Containers
Installing Docker
Writing a Simple Dockerfile
Building a Docker Image for the Flask App
Running the Application in a Docker Container
Hands-on Practical: Containerizing the Prediction Service
Quiz for Chapter 4

What is an API?

After training and saving a machine learning model, the next challenge is often how an external application—such as a web app or another service—can actually use that model to obtain predictions. Direct access to the file system where a model.pkl or model.joblib file is stored is generally not a viable approach. Instead, a defined method for different software components to communicate is required. This is precisely the problem that Application Programming Interfaces, or APIs, are designed to solve.

Think of an API as a contract or a set of rules that allows one piece of software to request services or data from another. It defines the kinds of requests that can be made, how to make them, the data formats that should be used, and what responses to expect.

The Waiter Analogy

Imagine you're at a restaurant. You (the client application) want food (a prediction). You don't go directly into the kitchen (the model logic) and start cooking. Instead, you interact with a waiter (the API).

  1. The Menu (API Definition): The menu tells you what dishes (operations/predictions) are available and what information the kitchen needs to prepare them (input data format).
  2. Placing an Order (Making a Request): You tell the waiter what you want from the menu ("I'd like the pasta with input data X"). You provide the necessary details in the expected format.
  3. The Kitchen (Server/Model): The waiter takes your order to the kitchen. The kitchen staff (the server running your model code) prepares your dish (generates the prediction based on input X).
  4. Serving the Dish (Returning a Response): The waiter brings the prepared dish (the prediction result) back to your table.

In this analogy:

  • You are the Client: The application needing a prediction.
  • The Waiter is the API: The intermediary defining how to interact.
  • The Menu is the API Specification: It documents the available functions and required inputs/outputs.
  • The Kitchen is the Server: The system hosting the model and logic.
  • Your Order is the Request: Sending data to the API endpoint.
  • The Prepared Dish is the Response: Receiving the prediction from the API.

APIs in Web Development and Model Serving

In the context of web services, APIs often use the HyperText Transfer Protocol (HTTP), the same protocol your web browser uses to fetch web pages. When we build a prediction service, we typically create a web API. This means our model will listen for incoming requests over the network at specific URLs (often called endpoints).

A client application sends an HTTP request to a specific endpoint. This request usually includes:

  • An HTTP Method: Like POST (commonly used for sending data to create or update something, suitable for sending input features for prediction) or GET (typically for retrieving data).
  • The Endpoint URL: The specific address for the prediction function (e.g., http://yourserver.com/predict).
  • Headers: Containing metadata about the request (e.g., the format of the data being sent).
  • A Body: Often containing the actual input data for the model, frequently formatted as JSON (JavaScript Object Notation), which is a human-readable text format for data exchange.

The server hosting the API receives this request, processes the input data, potentially loads the saved model, feeds the data to the model to get a prediction, and then sends an HTTP response back to the client. This response usually contains the prediction result, again often formatted as JSON.

A simple interaction flow: A client sends input data via an HTTP request to the API server, which uses the saved model to generate a prediction and sends it back in an HTTP response.

Why is this useful for machine learning deployment?

  1. Decoupling: The application needing the prediction doesn't need to know how the model works internally or even where the model file is stored. It just needs to know the API's address and how to format the request.
  2. Accessibility: Your model becomes accessible over a network, allowing various applications (web frontends, mobile apps, other backend services) to use it.
  3. Scalability: You can scale the API server independently of the client applications. If prediction requests increase, you can add more resources to the server handling the API.
  4. Technology Agnostic: A client application written in Java, JavaScript, or any other language can interact with your Python-based prediction API as long as they both "speak" HTTP and agree on the data format (like JSON).

In this chapter, we'll focus on building such a web API using Flask, a popular and lightweight Python web framework. You'll learn how to create endpoints, handle incoming data, load your previously saved model, generate predictions, and send those predictions back as responses. This API will serve as the bridge between your trained model and the applications that need its intelligence.

  • RESTful Web APIs, Leonard Richardson, Mike Amundsen, Sam Ruby, 2013 (O'Reilly Media) - A foundational guide to understanding the REST architectural style, web API design, and HTTP interactions.
  • Flask Documentation, Pallets, 2024 - The official and current documentation for the Flask web framework, detailing its features and API development.
  • The JSON Data Interchange Format, T. Bray, Ed., 2017 IETF RFC 8259 (Internet Engineering Task Force (IETF)) - The formal specification for JSON (JavaScript Object Notation), which is a common data format for web APIs.
  • Designing Machine Learning Systems: An Iterative Process for Production-Ready ML Applications, Chip Huyen, 2022 (O'Reilly Media) - Covers the process of deploying machine learning models and the role of APIs in serving predictions to applications.

© 2026 ApX Machine Learning