Introduction to API Development with Flask (for Model Serving)

This lesson introduces you to the basics of building APIs using Flask, a Python web framework. You'll learn how to create simple API endpoints to serve your machine learning models, allowing you to deploy them and make predictions.

Learning Objectives

  • Understand the basic concepts of APIs and their role in model deployment.
  • Install and configure the Flask framework.
  • Create simple Flask API endpoints for handling requests.
  • Learn how to receive data in the request and return predictions as the response.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to APIs and Model Serving

An API (Application Programming Interface) is like a messenger that takes requests from one application (e.g., a web app, mobile app) and passes them to another (e.g., your machine learning model). When the model returns a prediction, the API sends it back to the requesting application. This process is called Model Serving or Model Deployment. Using an API allows you to keep your model code separate from the user interface, making it easier to update the model without changing the entire application. A common analogy: Think of a waiter in a restaurant. The customer (client application) places an order (request), the waiter (API) takes the order to the kitchen (model), the kitchen prepares the food (prediction), and the waiter brings the food back to the customer (response). We'll use Flask to build our waiter!

Installing Flask

Before we start, we need to install Flask. Open your terminal or command prompt and run the following command:

pip install flask

This command downloads and installs the Flask package and its dependencies. You should ensure that you have Python and pip (the Python package installer) installed correctly before running this command. Verify the installation by running python -m flask --version which should output the Flask version. Also, ensure you have a code editor (like VS Code, Sublime Text, or similar) ready to write Python code.

Your First Flask Application

Let's create a simple Flask application. Create a file named app.py and add the following code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Hello, World!"

if __name__ == "__main__":
    app.run(debug=True)
  • from flask import Flask: Imports the Flask class.
  • app = Flask(__name__): Creates a Flask application instance. __name__ refers to the name of the current module.
  • @app.route("/"): This is a decorator that associates the function hello_world with the root URL ("/"). When someone visits the root URL of your app, this function is executed.
  • def hello_world():: Defines a function that returns the text "Hello, World!".
  • app.run(debug=True): Runs the Flask development server. debug=True provides helpful debugging information, but is not recommended for production environments. In your terminal, navigate to the directory where you saved app.py and run python app.py. Then, open your web browser and go to http://127.0.0.1:5000/. You should see "Hello, World!" displayed.

Handling Requests and Responses

APIs receive requests and send responses. The request contains the data (e.g., user input). The response contains the model's output (e.g., a prediction). We'll now modify our app to accept input via a URL parameter and return a custom greeting. Modify your app.py file to include:

from flask import Flask, request

app = Flask(__name__)

@app.route("/greet")
def greet():
    name = request.args.get("name")  # Get the 'name' parameter from the URL
    if name:
        return f"Hello, {name}!"
    else:
        return "Hello, Guest!"

if __name__ == "__main__":
    app.run(debug=True)
  • from flask import request: We import the request object. This object contains all the request data.
  • request.args.get("name"): This retrieves the value of the 'name' parameter from the URL query string (e.g., http://127.0.0.1:5000/greet?name=YourName).
  • The code now checks if a name was provided. If so, it returns a personalized greeting. Otherwise, it returns a generic greeting.

Restart your app (if it's still running) and go to http://127.0.0.1:5000/greet?name=Alice. You should see "Hello, Alice!".

Serving Model Predictions (Conceptual)

Imagine you have a machine learning model that predicts the price of a house. You can modify the greet() function (or create a new endpoint, e.g., /predict) to accept the relevant features of a house (e.g., size, number of bedrooms) as input. Inside the function, you'd:

  1. Retrieve the input features from the request (e.g., using request.args.get() or a JSON payload – we will cover JSON payloads in the next lesson).
  2. Load your pre-trained model.
  3. Preprocess the input data to match what the model expects.
  4. Make a prediction using the model.
  5. Return the prediction in the response (e.g., a JSON object containing the price). We'll go through the specifics in more detail in subsequent lessons, including how to load a model and perform model inference.
Progress
0%