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:
- Model Training: It trains a simple Logistic Regression classifier on the Iris dataset using
scikit-learnand saves the trained model to a file (model.joblib) usingjoblib. - API Creation: It generates a Python script named
app.py, which contains aFastAPIapplication. This application loads the saved model and defines an API endpoint (/predict) that accepts Iris flower features and returns a species prediction. - Containerization Setup:
- It creates a
Dockerfilethat specifies the instructions to build a Docker image containing the Python environment, dependencies, and the FastAPI application. - It generates a
requirements.txtfile listing all the necessary Python packages for the project.
- It creates a
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
-
Generate Project Files: First, run the main Python script to generate the
app.py,Dockerfile,requirements.txt, andmodel.joblibfiles.bash python advanced_deploy_model_fastapi_docker.py -
Build the Docker Image: Open your terminal in the project directory (where the
Dockerfileis located) and run the following command to build the Docker image. The-tflag tags the image with a name.bash docker build -t iris-classifier-api . -
Run the Docker Container: Once the image is built, run it as a container. The
-pflag maps port 8000 of the container to port 8000 on your local machine.bash docker run -p 8000:8000 iris-classifier-api -
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/predictendpoint directly from this interface. - Health Check: Visit
http://localhost:8000/to see the health check message.
- API Docs (Swagger UI): Open your web browser and go to
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.