Practice Project 12

Big Data Concepts Brief & Deployed ML Model

Deploy a machine learning model behind a FastAPI endpoint and Streamlit UI, build a Docker container, and verify the deployment inside your Linux VM.

Domain / Environment
Model Serving / Conda VM
Difficulty
Advanced (4/5)
Course Module
Model Deployment
Deliverables
FastAPI + Streamlit files, Dockerfile & scaling brief
1. System Architecture & Process Workflow

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.

Docker Container Boundary (Port 8000 & 8501) Streamlit UI Input parameters Click Predict Display Prediction POST /predict FastAPI Backend Receive JSON Call model.predict() Return JSON payload Trained Model churn_rf.joblib
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.

$ pip install fastapi uvicorn streamlit requests joblib scikit-learn
This command downloads FastAPI, Streamlit, and joblib libraries to build and serve the model.
STEP 4

Create FastAPI main.py in VS Code

Launch VS Code and create the FastAPI backend script file.

Launch VS Code via terminal "code ." -> New File -> Type: main.py -> Paste FastAPI code -> Save file
This registers the backend API endpoints in `main.py` to handle prediction requests.
STEP 5

Create Streamlit app.py in VS Code

Create the front-end user interface script file.

Right-click in explorer tree -> click New File -> Type: app.py -> Paste Streamlit code -> Save file
This registers the front-end dashboard file `app.py` to accept inputs and display predictions.
STEP 6

Create Dockerfile in VS Code

Create the configuration file to build and containerize the application.

Right-click in explorer tree -> click New File -> Type: Dockerfile -> Paste Docker configuration -> Save file
This registers the container build directives in a `Dockerfile`.
STEP 7

Build and Run the Docker Container

Build the container image and run it locally to verify the deployment.

$ docker build -t churn-app . && docker run -d -p 8000:8000 -p 8501:8501 churn-app
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.

1. Save Model Export trained model artifact to disk joblib.dump() 2. FastAPI Backend Map predict route and load model @app.post("/predict") 3. Streamlit UI Build UI inputs and call API st.button() 4. Dockerize Build image and expose API ports docker build 5. Serve Ports Confirm app runs inside container Container serving
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 schema class PredictRequest(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("/") def read_root(): return {"status": "running", "model_loaded": model is not None} @app.post("/predict") def predict(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):

fastapi uvicorn streamlit requests joblib scikit-learn numpy
5. Deliverables Summary

Verify that the following configurations and outputs exist inside your project workspace.

Created Files / Templates

  • ~/Projects/model_deployment/main.py - FastAPI backend script.
  • ~/Projects/model_deployment/app.py - Streamlit frontend dashboard.
  • ~/Projects/model_deployment/Dockerfile - Containerization configuration file.
  • ~/Projects/model_deployment/requirements.txt - Python packages manifest.

Verification Artifacts / Execution Proof

  • FASTAPI server responding to health checks on port 8000.
  • Streamlit UI active and processing inputs on port 8501.
  • Docker containers running both apps successfully.
6. Closing Explanation: Why We Did This & What It Accomplishes

Architectural Intent & Operational Impact

Why We Did This

What This Accomplishes