⬡ Hub
Skip to content

advanced_deploy_model_fastapi_docker

Advanced - Deploying a Model with FastAPI and Docker

Description

This project demonstrates a complete workflow for deploying a machine learning model in a production-like environment. It covers training a model, exposing it via a REST API using FastAPI, and containerizing the entire application with Docker for portability and scalability.

This is a critical skill in MLOps (Machine Learning Operations), as it bridges the gap between model development and real-world application integration.

Functionality

The main script, advanced_deploy_model_fastapi_docker.py, is a setup script that generates all the necessary files for the project:

  1. Model Training: It trains a simple Logistic Regression classifier on the Iris dataset using scikit-learn and saves the trained model to a file (model.joblib) using joblib.
  2. API Creation: It generates a Python script named app.py, which contains a FastAPI application. This application loads the saved model and defines an API endpoint (/predict) that accepts Iris flower features and returns a species prediction.
  3. Containerization Setup:
    • It creates a Dockerfile that specifies the instructions to build a Docker image containing the Python environment, dependencies, and the FastAPI application.
    • It generates a requirements.txt file listing all the necessary Python packages for the project.

Architecture

The project utilizes a combination of tools and libraries to create a robust deployment setup:

  • scikit-learn: To train the classification model.
  • joblib: For efficient serialization and deserialization of the trained model.
  • FastAPI: A modern, high-performance web framework for building APIs with Python. It's used to create the prediction endpoint.
  • Pydantic: Used by FastAPI for data validation. It ensures that the data sent to the API endpoint has the correct format and types.
  • uvicorn: An ASGI server that runs the FastAPI application.
  • Docker: A platform for developing, shipping, and running applications in containers. It allows us to package our application and its dependencies into a single, isolated unit.

How to Run

Prerequisites

  • Python 3.7+
  • Docker installed and running on your system.

Steps

  1. Generate Project Files: First, run the main Python script to generate the app.py, Dockerfile, requirements.txt, and model.joblib files.

    bash python advanced_deploy_model_fastapi_docker.py

  2. Build the Docker Image: Open your terminal in the project directory (where the Dockerfile is located) and run the following command to build the Docker image. The -t flag tags the image with a name.

    bash docker build -t iris-classifier-api .

  3. Run the Docker Container: Once the image is built, run it as a container. The -p flag maps port 8000 of the container to port 8000 on your local machine.

    bash docker run -p 8000:8000 iris-classifier-api

  4. Access the API: Your API is now running. You can interact with it:

    • API Docs (Swagger UI): Open your web browser and go to http://localhost:8000/docs. You can test the /predict endpoint directly from this interface.
    • Health Check: Visit http://localhost:8000/ to see the health check message.

Concepts Covered

  • Model Deployment: The process of making a machine learning model available for use.
  • REST APIs: Using the API paradigm to serve model predictions over a network.
  • Model Serialization: Saving a trained model to a file.
  • Containerization: Packaging an application and its dependencies into a Docker container.
  • MLOps: The practice of deploying and maintaining machine learning models in production.
  • Data Validation: Ensuring the integrity of input data for predictions using Pydantic.

Files and Subdirectories