The diagram below displays the containerized model serving architecture. The Streamlit UI communicates with the FastAPI endpoint, which loads a trained model to generate predictions. The entire stack runs inside a Docker container.
2. Part 1: Step-by-Step Action Items & Key Execution Steps
STEP 1
Activate Python Virtual Environment
Point the terminal execution environment to the course conda sandbox environment.
$ conda activate ds_ai_ml
This targets active python libraries to the isolated virtual sandbox.
STEP 2
Create Project Folders inside Linux VM
Create a dedicated folder for the project files inside your guest VM home folder directory.
$ mkdir -p ~/Projects/model_deployment && cd ~/Projects/model_deployment
This sets up the working directory layout for the deployment code.
STEP 3
Install FastAPI, Streamlit, and joblib via Pip
Install the required deployment packages inside the active conda session.
This builds the Docker image and runs it as a background container on ports 8000 and 8501.
3. Deployment Pipeline Flow
The flowchart below outlines the model deployment pipeline. It details the steps from model loading and API routing to building container images and serving ports.
4. Part 2: Complete Deliverable Assets & Production Templates
To run the serving container, we need the FastAPI main.py, Streamlit app.py, a mock model script, and the Dockerfile. Below are the consolidated templates.
Production templates
1. Save Mock Model Script (Save as ~/Projects/model_deployment/generate_mock_model.py):
import joblib
from sklearn.ensemble import RandomForestClassifier
import numpy as np
# Train a simple classifier on mock data
X = np.array([[10, 50], [5, 95], [20, 30], [2, 110]])
y = np.array([0, 1, 0, 1])
clf = RandomForestClassifier(random_state=42)
clf.fit(X, y)
# Save the trained model to disk
joblib.dump(clf, "churn_rf.joblib")
print("Mock model generated and saved as churn_rf.joblib")
2. FastAPI Application (Save as ~/Projects/model_deployment/main.py):
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import joblib
import os
app = FastAPI(title="Customer Churn Prediction API")
# Define request data schemaclassPredictRequest(BaseModel):
Tenure: float
MonthlyCharges: float
# Load model on startup
MODEL_PATH = "churn_rf.joblib"
if os.path.exists(MODEL_PATH):
model = joblib.load(MODEL_PATH)
else:
model = None
@app.get("/")
defread_root():
return {"status": "running", "model_loaded": model is not None}
@app.post("/predict")
defpredict(request: PredictRequest):
if model is None:
raise HTTPException(status_code=500, detail="Model artifact missing")
# Run predictions
features = [[request.Tenure, request.MonthlyCharges]]
pred = int(model.predict(features)[0])
prob = float(model.predict_proba(features)[0][1])
return {"prediction": pred, "churn_probability": prob}
3. Streamlit Application (Save as ~/Projects/model_deployment/app.py):
import streamlit as st
import requests
st.title("Customer Churn Prediction Dashboard")
st.write("Input parameters below to estimate customer churn risk.")
# Input controls
tenure = st.slider("Tenure (Months)", min_value=1, max_value=72, value=12)
monthly_charges = st.number_input("Monthly Charges ($)", min_value=10.0, max_value=200.0, value=65.0)
if st.button("Predict Churn Risk"):
# Post request to FastAPI endpoint
payload = {"Tenure": tenure, "MonthlyCharges": monthly_charges}
try:
res = requests.post("http://localhost:8000/predict", json=payload)
data = res.json()
if data["prediction"] == 1:
st.error(f"High risk detected. Churn probability: {data['churn_probability']:.2%}")
else:
st.success(f"Low risk. Churn probability: {data['churn_probability']:.2%}")
except Exception as e:
st.error(f"Error calling prediction API: {e}")
4. Dockerfile (Save as ~/Projects/model_deployment/Dockerfile):
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
# Expose ports for FastAPI (8000) and Streamlit (8501)
EXPOSE 8000
EXPOSE 8501
# Start both services using a shell script
CMD uvicorn main:app --host 0.0.0.0 --port 8000 & streamlit run app.py --server.port 8501 --server.address 0.0.0.0
5. Requirements File (Save as ~/Projects/model_deployment/requirements.txt):