From Model to Microservice: Deploying a Lead Conversion Predictor with FastAPI and Docker
Blog Post Outline: Building and Deploying a Lead Conversion Prediction Service
Introduction:
Briefly introduce the problem: Predicting lead conversion is crucial for businesses.
Mention the goal: Build a web service to serve a pre-trained lead conversion model.
Highlight the technologies used: FastAPI for the web service, Docker for containerization.
Section 1: The Machine Learning Model (Briefly)
Explain that a pre-trained model is used (mention it's a pipeline with a vectorizer and logistic regression).
Show the code for loading the model and making a prediction on a sample record (like the one in Question 3).
State the prediction result for the sample record.
Section 2: Building the Web Service with FastAPI
Explain why FastAPI is a good choice for building APIs (fast, easy to use, automatic documentation).
Show the code for the FastAPI application (
main.py):Import necessary libraries (
FastAPI,pickle,BaseModel).Define the
LeadPydantic model for request validation.Load the pre-trained model.
Create the FastAPI app instance.
Define the health check endpoint (
/).Define the prediction endpoint (
/predict), showing how it takes theLeadobject, makes a prediction, and returns the probability.
Show the code for testing the FastAPI application locally using the
requestslibrary (like in the notebook).Show the output of the local test.
Section 3: Containerizing with Docker
Explain the benefits of containerization (consistency, portability, isolation).
Introduce Docker and the base image used (
agrigorev/zoomcamp-model:2025).Show the
Dockerfilecontent, explaining each step:FROM: Using the base image.COPY: Copying the model file, the FastAPI code, and the requirements file.WORKDIR: Setting the working directory.RUN: Installing dependencies usinguv pip install.EXPOSE: Exposing the port the FastAPI application runs on.CMD: Specifying the command to run the application withuvicorn.
Acknowledge the issue with running Docker in the Colab environment if that was the case in your notebook run. Explain that in a typical environment, you would build the image using
docker buildand show the command.Similarly, explain how you would run the container using
docker runin a typical environment and show the command.
Section 4: Testing the Dockerized Service
Explain how to test the service running in a Docker container.
Show the code for testing the containerized service using the
requestslibrary (like in the notebook).Show the output of the test request to the Dockerized service.
State the prediction result for the client record from Question 6.
Conclusion:
Summarize what was achieved: successfully built and containerized a machine learning web service.
Discuss potential next steps (e.g., deploying to a cloud platform, adding more features, improving the model).
Possible additions:
Add screenshots of the notebook cells or the output.
Include a link to the GitHub repository with the code.
Discuss any challenges you faced and how you overcame them.
Summary:
Q&A
Build a web service with FastAPI: The FastAPI application was successfully built, defining endpoints for a health check (
/) and predictions (/predict). It loads a pre-trained model and uses a Pydantic model for request validation.Containerize it with Docker: A
Dockerfilewas created to containerize the application usingagrigorev/zoomcamp-model:2025as the base image. However, building and running the Docker image/container failed due to thedockercommand not being found in the execution environment.Test the containerized service: The Python script to test the service was written and executed. Although the Docker container could not be built or run in the provided environment, the test script itself was successfully executed and demonstrated how to interact with the
/predictendpoint.
Data Analysis Key Findings
The FastAPI application (
main.py) was successfully written, including endpoint definitions, model loading, and prediction logic.A
requirements.txt.lockfile was generated, listing the dependencies installed, includingfastapianduvicorn.The script to test the FastAPI application locally was successful, demonstrating the ability to send a request and receive a prediction probability when the service is running.
A
Dockerfilewas correctly written to package the application and its dependencies into a Docker image.Attempts to download the Docker base image, build the Docker image, and run the Docker container failed because the
dockercommand was not available in the execution environment.The script designed to test the containerized service was successfully executed, although it couldn't connect to a running Docker container due to the aforementioned Docker environment issue.
Insights or Next Steps
The core components of the web service (FastAPI app, Dockerfile, testing script) were successfully developed.
The primary roadblock was the lack of a functional Docker environment; the next step should be to ensure Docker is installed and accessible to build and run the containerized application for end-to-end testing.