Skip to main content

Chapter 40: Class-Based and Home-Based Projects

MODULE 6 | MLOps​

Chapter 40: Class-Based and Home-Based Projects​

So far we learned one tool in each chapter: FastAPI , Docker , MLflow , CI/CD , Logging . In this chapter they all join in one real project : the AI Medical Assistant . The user chooses their symptoms , and the app shows the top 3 possible illnesses and their likelihood. It has a FastAPI backend , a nice web UI , tests , Docker and CI , all together.

ETHICS / RESPONSIBILITY​

This project is only for learning. It is not a substitute for a doctor or a diagnosis . We used synthetic data , which is not from real patients. Real medical AI needs a proper dataset, guidance from doctors, very strict testing and legal approval . Showing a disclaimer with every answer of the app is a part of this project, not a final nicety. And never give anyone a diagnosis or medicine advice relying on this app.

TOPIC CHECKLIST (tick them off as you go)​

  • ☐ The project's goal, scope and architecture

  • ☐ Class-Based Project: model, FastAPI backend, UI, tests, Docker

  • ☐ Running the backend and testing with curl and /docs

  • ☐ Deliverables and rubric of the Class Project

  • Home-Based Project A: MLflow tracking and registry

  • ☐

  • ☐ Home-Based Project B: the /predict/image endpoint of a skin image model

  • ☐ Home-Based Projects C and D: monitoring and your own model

  • ☐ Summary of Module 7 and the next step

40.1 The Project's Goal and Scope​

PROBLEM STATEMENT​

Many people search the internet first for symptoms of a minor illness. We build a learning symptom checker : the user chooses symptoms, and the API returns the top 3 possible conditions + likelihood (probability) . Along with it, it is clearly written that this is not a doctor's advice.

Input: a list of symptoms (for example fever, headache). Output: top 3 conditions + disclaimer. Model: Random Forest. Backend: FastAPI. UI: one HTML page.

DIAGRAM​
Browser (UI) ----> FastAPI ----> model.joblib
static/index.html /predict (Random Forest)
| |
| logging (har request)
v v
Docker container <- CI (GitHub Actions: test + build)

The project folder will look like this:

FOLDER MAP​
medical-assistant/
train.py <- make data, train and save the model
train_mlflow.py <- (Home Project A) training with MLflow
requirements.txt <- libraries for the API (with versions)
requirements-dev.txt <- libraries for testing
Dockerfile
.dockerignore
.gitignore
app/
__init__.py <- empty file
main.py <- FastAPI backend
static/
index.html <- UI
model/ <- train.py puts model.joblib and symptoms.json here
tests/
test_api.py
.github/workflows/
ci.yml <- (the file from Chapter 38)

40.2 Class-Based Project: Step 1, Data and Model​

We do not have a real medical dataset (and if we did, we would need permission), so train.py creates a synthetic dataset of 9 common illnesses and 18 symptoms : for each illness it states how likely each symptom is (for example fever is 0.90 in Flu). Then we randomly create 300 samples of each illness and train a Random Forest .

train.py

import json
import os
import joblib
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
SYMPTOMS = [ "fever", "cough", "sore_throat", "runny_nose", "sneezing", "headache", "body_ache", "fatigue", "nausea", "vomiting", "diarrhea", "stomach_pain", "rash", "chills", "joint_pain", "light_sensitivity", "itchy_eyes", "loss_of_appetite", ] # For each illness:
symptom -> probability of it occurring (0 to 1)
PROFILES = { "Common Cold": {"runny_nose": .9, "sneezing": .8, "sore_throat": .6, "cough": .5, "headache": .3, "fatigue": .3, "fever": .15}, "Flu": {"fever": .9, "body_ache": .85, "chills": .7, "cough": .7, "fatigue": .8, "headache": .6, "sore_throat": .4}, "Dengue": {"fever": .95, "headache": .8, "joint_pain": .8, "body_ache": .8, "rash": .5, "nausea": .5, "fatigue": .7, "chills": .4, "loss_of_appetite": .5}, "Typhoid": {"fever": .9, "stomach_pain": .7, "headache": .6, "loss_of_appetite": .7, "fatigue": .7, "nausea": .4, "diarrhea": .4}, "Malaria": {"fever": .9, "chills": .9, "headache": .6, "nausea": .5, "vomiting": .4, "body_ache": .5, "fatigue": .6}, "Migraine": {"headache": .98, "light_sensitivity": .85, "nausea": .6, "vomiting": .3, "fatigue": .3}, "Food Poisoning": {"nausea": .85, "vomiting": .85, "diarrhea": .85, "stomach_pain": .8, "fever": .3, "fatigue": .4}, "Allergy": {"sneezing": .85, "itchy_eyes": .85, "runny_nose": .7, "rash": .3, "cough": .3}, "Gastritis": {"stomach_pain": .9, "nausea": .6, "vomiting": .3, "loss_of_appetite": .6}, }
def make_dataset(per_class=300, seed=42):
rng = np.random.default_rng(seed)
rows, labels = [], []
for disease, profile in PROFILES.items():
for _ in range(per_class):
row = [int(rng.random() < profile.get(s, 0.03)) for s in SYMPTOMS]
rows.append(row)
labels.append(disease)
return pd.DataFrame(rows, columns=SYMPTOMS), pd.Series(labels, name="disease")
if __name__ == "__main__":
X, y = make_dataset()
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
acc = accuracy_score(y_test, model.predict(X_test))
print(f"Rows: {len(X)} | Symptoms: {len(SYMPTOMS)} | Diseases: {y.nunique()}")
print(f"Test accuracy: {acc:.2%}")
os.makedirs("model", exist_ok=True)
joblib.dump(model, "model/model.joblib")
with open("model/symptoms.json", "w") as f:
json.dump(SYMPTOMS, f)
print("Saved: model/model.joblib, model/symptoms.json")
TERMINAL​
python train.py
OUTPUT​
Rows: 2700 | Symptoms: 18 | Diseases: 9
Test accuracy: 88.89%
Saved: model/model.joblib, model/symptoms.json
PartWhat it does
SYMPTOMSAnordered listof 18 symptoms; the model's columns are in this order
PROFILESThelikelihoodof a symptom for each illness; a symptom not in the list has a likelihood of
0.03(occasionally, for ordinary reasons)
make_dataset()Makesalways the samedata with default_rng(42) (seed), so that the result can be
reproduced
stratify=yAn equal share of every illnessin both train and test
joblib.dumpThe model file in model/model.joblib, and the order of symptoms in symptoms.json

40.3 Step 2: The FastAPI Backend​

Now in app/main.py we put everything we learned in Chapters 35 and 39: the model loaded once , input checked with Pydantic , logging , and a disclaimer with every answer:

app/main.py

import json
import logging
import time
from pathlib import Path
import joblib
import pandas as pd
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from pydantic import BaseModel, Field
BASE_DIR = Path(__file__).resolve().parent.parent logging.basicConfig( level=logging.INFO, format="%(asctime)s | %(levelname)s | %(name)s | %(message)s", )
logger = logging.getLogger("medical-assistant")
model = joblib.load(BASE_DIR / "model" / "model.joblib")
SYMPTOMS = json.loads((BASE_DIR / "model" / "symptoms.json").read_text())
DISCLAIMER = "This is for learning only. This is not a doctor's advice."
app = FastAPI(title="AI Medical Assistant", version="1.0.0")
class PredictRequest(BaseModel):
symptoms:
list[str] = Field(min_length=1, description="Names of symptoms")
class Prediction(BaseModel):
disease:
str probability:
float
class PredictResponse(BaseModel):
predictions:
list[Prediction] disclaimer:
str @app.get("/")
def home():
return FileResponse(BASE_DIR / "static" / "index.html") @app.get("/health")
def health():
return {"status": "ok"} @app.get("/symptoms")
def list_symptoms():
return {"symptoms": SYMPTOMS}
@app.post("/predict", response_model=PredictResponse)
def predict(req: PredictRequest):
start = time.perf_counter()
unknown = [s for s in req.symptoms if s not in SYMPTOMS]
if unknown:
logger.warning("Unknown symptoms: %s", unknown)
raise HTTPException(status_code=400,
detail=f"These symptoms were not found: {unknown}")
values = [1 if s in req.symptoms else 0 for s in SYMPTOMS]
row = pd.DataFrame([values], columns=SYMPTOMS)
probs = model.predict_proba(row)[0]
top3 = sorted(zip(model.classes_, probs), key=lambda p: p[1], reverse=True)[:3]
ms = (time.perf_counter() - start) * 1000
logger.info("predict | symptoms=%d | top=%s | %.1f ms",
len(req.symptoms), top3[0][0], ms)
return PredictResponse(
predictions=[Prediction(disease=d, probability=round(float(p), 4))
for d, p in top3],
disclaimer=DISCLAIMER,
)
EndpointMethodPurpose
/GETShows theUI page(static/index.html)
/healthGETA check that it isalive(for Docker, CI and monitoring)
/symptomsGETThe list of the18 symptomsthe model knows (the UI makes chips from this)
/predictPOSTSend symptoms, get thetop 3 conditions + probability + disclaimer
Key part of the codeWhy it is there
min_length=1 (PredictRequest)Anempty listautomatically gives422(at least one symptom is required)
unknown = [...] and HTTPException(400)For a symptom the model does not know at all, aclear 400response and a
WARNING log
pd.DataFrame([values], columns=SYMPTOMS)Give the model the datawith column names(training had names too)
predict_proba + sorted(...)[:3]Take thelikelihoodof every illness and pick the top3
logger.info(...)Thecount, top result and timeof every prediction (not the private
symptoms)
DISCLAIMERA warning inevery response, so that the UI or any client cannot skip it

40.4 Step 3: The Web UI​

Now the prettiest part: a web page where the user taps symptoms like chips and presses Check . It is a single HTML file (static/index.html), with no framework. The colours are the same as this book's ( pink and lavender ):

static/index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>AI Medical Assistant</title> <style> body { font-family: Arial, sans-serif; background: #FDEBF2; color: #2b2b33; margin: 0; }

.wrap { max-width: 640px; margin: 0 auto; padding: 24px 16px; }
h1 { color: #A4457A; margin-bottom: 4px; }
.card { background: #fff; border: 1px solid #F4C6D7; border-radius: 12px;
padding: 16px; margin-top: 16px; }
.chip { display: inline-block; padding: 6px 12px; margin: 4px;
border: 1px solid #F4C6D7;
border-radius: 20px; cursor: pointer; background: #fff; }
.chip.on { background: #A4457A; color: #fff; border-color: #A4457A; }
button { background: #A4457A; color: #fff; border: 0; padding: 10px 20px;
border-radius: 8px;
font-size: 16px; cursor: pointer; margin-top: 12px; }
.bar { background: #F4C6D7; border-radius: 6px; height: 10px; margin-top: 4px; }
.fill { background: #A4457A; height: 10px; border-radius: 6px; }
.warn { font-size: 13px; color: #6b6b75; margin-top: 12px; }
</style>
</head>
<body>
<div class="wrap">
<h1>AI Medical Assistant</h1>
<div>Choose your symptoms, then press Check.</div>
<div class="card">
<div id="chips">Loading...</div>
<button onclick="check()">Check</button>
</div>
<div class="card" id="result" style="display:none"></div>
<div class="warn" id="disclaimer"></div>
</div>
<script>
const selected = new Set();
async function loadSymptoms() {
const res = await fetch("/symptoms");
const data = await res.json();
const box = document.getElementById("chips");
box.innerHTML = "";
data.symptoms.forEach(s => {
const el = document.createElement("span");
el.className = "chip";
el.textContent = s.replaceAll("_", " ");
el.onclick = () => {
selected.has(s) ? selected.delete(s) : selected.add(s);
el.classList.toggle("on");
};
box.appendChild(el);
});
}
async function check() {
const result = document.getElementById("result");
if (selected.size === 0) {
result.style.display = "block";
result.textContent = "First choose at least one symptom.";
return;
}
const res = await fetch("/predict", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ symptoms: Array.from(selected) })
});
const data = await res.json();
if (!res.ok) {
result.style.display = "block";
result.textContent = data.detail;
return;
}
result.style.display = "block";
result.innerHTML = "<b>Top 3 possibilities</b>" + data.predictions.map(p =>
`<div style="margin-top:10px">${p.disease}: ${(p.probability * 100).toFixed(1)}%
<div class="bar">
<div class="fill" style="width:${p.probability * 100}%"></div>
</div></div>`
).join("");
document.getElementById("disclaimer").textContent = data.disclaimer;
}
loadSymptoms();
</script>
</body>
</html>
Part of the UIWhat it does
loadSymptoms()As soon as the page opens, it asks**/symptomsfor the list and makes achip**for each
symptom
selected (Set)A record ofwhich chipsthe user pressed
el.onclickSelects / deselectswhen a chip is pressed; the on class changes the colour
check()Does**POST /predict, and shows the top 3 withbars**in the response
Error handlingIf nothing was chosen or the server gives400, it shows aclear message
disclaimerThewarningis always shown below the response
PRACTICAL TIP​

The UI's fetch("/predict") works without writing the full address because the UI and API come from the same server (the / route serves the UI). So there is no CORS problem. If you put the UI in a different place (for example Vercel), you will have to add CORS to the API.

40.5 Step 4: Running and Testing​

TERMINAL​
python train.py
uvicorn app.main:app --reload

Open http://127.0.0.1:8000 in the browser: you will see the UI. On /docs there is the Swagger testing page. Try curl too (symptoms: fever, headache, joint_pain, body_ache):

TERMINAL​
curl -X POST http://127.0.0.1:8000/predict -H "Content-Type: application/json" \
-d '{"symptoms":["fever","headache","joint_pain","body_ache"]}'

OUTPUT (the real response comes on a single line; it is broken up for reading)

{"predictions":[{"disease":"Dengue","probability":0.9967},
{"disease":"Malaria","probability":0.0033},
{"disease":"Allergy","probability":0.0}],
"disclaimer":"This is for learning only. This is not a doctor's advice."}

See too that the app stops wrong input properly :

OUTPUT (wrong symptom and empty list)​
{"detail":"These symptoms were not found: ['xyz']"} <- status 400
{"detail":[{"type":"too_short","loc":["body","symptoms"], ... }]} <- status 422 (empty
list)

Now we write tests (tests/test_api.py). These are the TestClient + pytest that we learned in Chapters 35 and 38:

tests/test_api.py

from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_health():
res = client.get("/health")
assert res.status_code == 200
assert res.json() == {"status": "ok"}
def test_symptoms_list():
res = client.get("/symptoms")
assert res.status_code == 200
assert "fever" in res.json()["symptoms"]
def test_predict_returns_top3():
res = client.post("/predict", json={"symptoms": ["nausea", "vomiting", "diarrhea"]})
assert res.status_code == 200
preds = res.json()["predictions"]
assert len(preds) == 3
assert preds[0]["disease"] == "Food Poisoning"
def test_predict_unknown_symptom():
res = client.post("/predict", json={"symptoms": ["xyz"]})
assert res.status_code == 400
def test_predict_empty_list():
res = client.post("/predict", json={"symptoms": []})
assert res.status_code == 422

TERMINAL pytest -v OUTPUT tests/test_api.py::test_health PASSED [ 20%] tests/test_api.py::test_symptoms_list PASSED [ 40%] tests/test_api.py::test_predict_returns_top3 PASSED [ 60%] tests/test_api.py::test_predict_unknown_symptom PASSED [ 80%] tests/test_api.py::test_predict_empty_list PASSED [100%]

========================= 5 passed, 1 warning in 1.58s =========================

40.6 Step 5: Docker and CI​

Now we pack the project in a box (Chapter 36). First requirements.txt with versions (write the versions from your own pip freeze; these are the ones the book's code was tested on):

requirements.txt​

fastapi==0.159.1 uvicorn==0.54.0 scikit-learn==1.8.0 joblib==1.5.3 pandas==3.0.2 numpy==2.4.4 requirements-dev.txt pytest==9.1.1 httpx==0.28.1 Dockerfile FROM python:3.12-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app/ app/
COPY static/ static/
COPY model/ model/
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
TERMINAL​

docker build -t medical-assistant . docker run -d -p 8000:8000 --name medical-container medical-assistant docker ps EXAMPLE OUTPUT (the ID and time will differ) CONTAINER ID IMAGE STATUS PORTS NAMES b7c3d9e1f2a4 medical-assistant Up 8 seconds 0.0.0.0:8000->8000/tcp medical-container

The same UI will run from inside the container at http://localhost:8000. Because the model is small ( about 9 MB ), it goes straight into the image with COPY model/ model/ and can be pushed to GitHub too.

Then CI (Chapter 38): .github/workflows/ci.yml is the same file that was given in Chapter 38. Push the project to GitHub, see the green tick for both the test and docker-build jobs in the Actions tab, and add the CI badge to the README.

40.7 Class-Based Project: Deliverables and Rubric​

Goal: In class, run all the steps above yourself and submit a working AI Medical Assistant .

Deliverables​

  • A working project ( GitHub repo ) containing train.py, app/, static/, tests/, Dockerfile, requirements.txt and ci.yml.

  • README.md : what the project does, how to run it (3 to 4 commands), screenshots (UI and /docs), limitations and a disclaimer .

  • Screenshots : the result on the UI, Swagger /docs, the "5 passed" of pytest, and the green tick of GitHub Actions.

  • A screenshot of the container running from Docker (docker ps and the UI).

  • Demo: a 2 to 3 minute screen recording.

RubricMarks
The model trains and the API gives correct predictions (/predict, /health, /symptoms)25
The UI works (chips, Check, top 3 bars, error message)20
Tests are written and pass (at least 5, including one for wrong input)15
The Docker image builds and the container runs15
CI (GitHub Actions) is green, the README and screenshots are clear15
Ethics: disclaimer,no private data in logs, limitations written honestly10
Total100

40.8 Home-Based Project A: MLflow Tracking and Model Registry​

Goal: keep the record of training in MLflow (Chapter 37) and make the best model the champion in the registry. train_mlflow.py imports make_dataset() from train.py and runs three settings:

train_mlflow.py

import mlflow
import mlflow.sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from train import make_dataset
mlflow.set_tracking_uri("sqlite:///mlflow.db")
mlflow.set_experiment("medical-assistant")
X, y = make_dataset()
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42)
for n_trees, depth in [(25, 8), (50, 12), (100, None)]:
with mlflow.start_run(run_name=f"rf_{n_trees}_{depth}"):
model = RandomForestClassifier(n_estimators=n_trees, max_depth=depth,
random_state=42)
model.fit(X_train, y_train)
acc = accuracy_score(y_test, model.predict(X_test))
mlflow.log_params({"n_estimators": n_trees, "max_depth": depth})
mlflow.log_metric("accuracy", acc)
mlflow.sklearn.log_model(model, name="model",
serialization_format="cloudpickle")
print(f"n_estimators={n_trees:>3}, max_depth={str(depth):>4} "
f"-> accuracy={acc:.4f}")

TERMINAL

python train_mlflow.py mlflow ui --backend-store-uri sqlite:///mlflow.db OUTPUT (of the first command; MLflow's INFO lines are left out)

n_estimators= 25, max_depth= 8 ->
accuracy=0.8815
n_estimators= 50, max_depth= 12 ->
accuracy=0.8852
n_estimators=100, max_depth=None ->
accuracy=0.8889

Tasks to do:​

  1. Compare all three runs in the UI (http://127.0.0.1:5000). Is the benefit of 100 trees really enough, or was 25 trees (a small, fast model) enough ?

  2. Also log train_accuracy and see how big the difference is between train and test accuracy (overfitting).

  3. With every run, also log the model's file size (MB) as a metric, and write about the trade-off between size and accuracy.

  4. Register the best run with register_model under the name medical-assistant-model and add the champion alias.

  5. Challenge: make app/main.py load the model from models:/medical-assistant-model@champion (MLflow must be installed) and think and write about how this will work in the Docker image.

NOTE

Keep mlflow.db and mlruns/ in .gitignore and .dockerignore ; they can be large and the API does not need them.

40.9 Home-Based Project B: The /predict/image Endpoint for Skin Images​

Goal: give the app two kinds of input : typing symptoms or uploading a picture of a skin problem. For this you will first have to train your own image model (Module 5: transfer learning, for example MobileNetV2 on a dataset like DermNet). After training, save two things: model/skin_model.h5 and model/class_indices.json (in Keras, train_generator.class_indices, which gives name -> index ). Then make app/image_model.py:

app/image_model.py

import io
import json
from pathlib import Path
import numpy as np
from PIL import Image
BASE_DIR = Path(__file__).resolve().parent.parent
_model = None
_classes = None
def _load():
global _model, _classes
if _model is None:
from tensorflow.keras.models import load_model #
import only when needed
_model = load_model(BASE_DIR / "model" / "skin_model.h5")
mapping = json.loads((BASE_DIR / "model" / "class_indices.json").read_text())
_classes = {index: name for name, index in mapping.items()}
return _model, _classes
def predict_skin(image_bytes: bytes, top_k: int = 3):
model, classes = _load()
img = Image.open(io.BytesIO(image_bytes)).convert("RGB").resize((224, 224))
arr = np.asarray(img, dtype="float32") / 255.0
probs = model.predict(arr[None, ...], verbose=0)[0]
top = probs.argsort()[::-1][:top_k]
return [{"condition": classes[int(i)], "probability": round(float(probs[i]), 4)} for i in top]

Then add this endpoint to app/main.py (also write

from fastapi import File, UploadFile

and

from app.image_model import predict_skin

at the top):

app/main.py (new endpoint)

@app.post("/predict/image")
async def predict_image(file: UploadFile = File(...)):
if file.content_type not in ("image/jpeg", "image/png"):
raise HTTPException(status_code=400, detail="Send only a JPG or PNG image")
data = await file.read()
logger.info("predict/image | %d bytes", len(data))
return {"predictions": predict_skin(data), "disclaimer": DISCLAIMER}

Prerequisites: add tensorflow, Pillow and python-multipart to requirements.txt (python-multipart is needed for file upload). Give the image the same preprocessing as in training (here 224x224, RGB, /255.0); if preprocess_input was used in training, use the same here too, otherwise the results will be bad.

VERSION NOTE​

The logic of this endpoint (type check, resize, top 3, response format) has been tested with a fake model : 200 on an image , and 400 on a text file . But the file of your real trained model is not with the book, so you will have to test it yourself with your own model and dataset . Check the format of class_indices.json ( name -> index or index -> name ) by using

print

on your file, and keep the _classes line in line with it.

Tasks to do:​

  1. In index.html, make an image upload button/area and a preview , and send it with fetch("/predict/image", { method: "POST", body: formData }).

  2. Show both responses (symptoms and image) in the same kind of result cards .

  3. Write a test for the image endpoint (by making a small fake image) and check for a 400 on a wrong file type .

  4. Write honestly: put the image model's test accuracy in the README. If it is low (skin disease datasets are often hard), then clearly write in the UI that the result is not reliable.

  5. Docker: because of TensorFlow the image becomes very large . In the Dockerfile, note the difference between tensorflow-cpu and tensorflow and compare the image size (docker images).

40.10 Home-Based Project C: Monitoring and Drift​

Goal: apply the work of Chapter 39 to this project.

  1. In /predict, also log the probability of the top prediction along with the response. If it is below 0.5 , write a WARNING ("low confidence").

  2. Keep a global counter that counts which illness came out on top in how many requests , and show it with GET /stats. Does any one illness always come out on top?

  3. Drift idea: how many symptoms on average are there per request in the training data (X.sum(axis=1).mean()), and how many in live requests? Compare the distributions of the two with the KS test (Chapter 39).

  4. Write a monitor_report.py that reads app.log and prints the count of requests, average latency and count of WARNINGs .

  5. Do not write private information (the list of symptoms) in the logs. This is the test of this task.

40.11 Home-Based Project D: Your Own Model, Your Own API​

Now run the same whole path on one of your old projects that you built in Module 4 (for example Heart Disease, HR Attrition, Credit Card Churn, Titanic ). The important thing: everything should be the same , only the model and input change.

StepTaskChapter
1Save the model with joblib (and theorderof features too)34
2Make /predict and /health with FastAPI +Pydantic35
3Write tests (TestClient + pytest)35
4Dockerfile, .dockerignore, requirements.txt (with versions)36
5Log the training in MLflow, register a good model37
6Push to GitHub, get the green tick from ci.yml, a badge on the README38
7Add logging, and writeone drift check39

Deliverable: a GitHub repo (at least: code, tests, Dockerfile, ci.yml, README), and 1 screenshot of green CI . If you want to run it on hosting (CD), remember that free plans have little memory ; large models do not run there.

Rubric (Home Project)Marks
API and tests (at least 3 tests, including one for wrong input)25
The Docker image builds and runs20
At least 3 runs in MLflow and one registered model20
CI is green and the README clearly sayshow to run it20
Logging and one monitoring/drift check15
Total100

40.12 Summary of Module 7​

ChapterMain IdeaKey Code / Tool
34: Introduction to MLOpsMLOps = ML + DevOps + Data Engineering; the lifecycle
is a cycle
joblib.dump / joblib.load,
requirements.txt
35: FastAPIServing a model as an API; input validation; testingFastAPI, BaseModel, Field, /docs,
TestClient
36: DockerApp and environment in one box; image vs containerDockerfile, docker build, run, ps,
stop
37: MLflowRecord of experiments and the model registrylog_param, log_metric, log_model,
mlflow ui, alias
38: CI/CDAutomatic test and build on every change.github/workflows/ci.yml, pytest,
docker build
39: Logging & MonitoringThe black box (logs) and the health of the live model;
drift
logging, AccuracyMonitor, KS test
(ks_2samp)
40: ProjectsJoining everything in one real projectAI Medical Assistant (FastAPI + UI)

Next step: After this module you know how to deploy and monitor your models. In the next module you will move toward Big Data and Cloud : where the data is so big that it does not fit on one computer, and apps run on the cloud .

40.13 Common Mistakes and Hands-on​

Hands-on​

  • Add a 10th illness to PROFILES (for example Chickenpox : fever, rash, itchy_eyes...), run python train.py again and test it in the UI.

  • Add a "Clear" button to the UI that removes all chips.

  • Write one more test like test_predict_returns_top3 that checks that Allergy comes out on top for the symptoms of Allergy (sneezing, itchy_eyes).

  • docker stop the container and run it again, and see what appears in the logs (docker logs).

  • Write a "Limitations" section in the README: at least 4 things this project cannot do.

KEY TAKEAWAYS​
  • In the AI Medical Assistant , FastAPI, UI, tests, Docker, CI and logging came together in one project.

  • Class Project: model training, /predict, UI, 5 tests, Docker image, the green tick of CI, and a clear README.

  • Home Projects: A MLflow and registry, B /predict/image, C monitoring/drift, D your own model, your own API.

  • In problems like medical ones, disclaimer, limitations and protecting private data are part of the project.

  • Good numbers on synthetic data are no proof for the real world; always write honestly .