Prepared by: Vikas Neriyanuru
For this lab submission, I built a FastAPI application that predicts the wine
cultivar from 13 chemical measurements using the Wine Recognition dataset from
scikit-learn.
- Replaced the original dataset with the Wine Recognition dataset
- Replaced the original model with a
StandardScaler + LogisticRegressionpipeline - Added a
/model-infoendpoint to show the saved model metadata and evaluation metrics - Added a
/sample-payloadsendpoint to make testing easier - Updated the prediction response to include both the predicted class name and class probabilities
- Added automated API tests using
pytest - Wrote a new README with my own setup, rerun steps, and sample API call
FastAPI_Labs/
├── model/
├── src/
│ ├── __init__.py
│ ├── data.py
│ ├── main.py
│ ├── predict.py
│ └── train.py
├── tests/
│ └── test_api.py
├── README.md
└── requirements.txt
- Move into the lab folder:
cd Labs/API_Labs/FastAPI_Labs- Create and activate a virtual environment:
python3 -m venv .venv
source .venv/bin/activate- Install the required packages:
pip install -r requirements.txt- Train the model and save the artifacts:
python3 -m src.train- Start the FastAPI application:
uvicorn src.main:app --reload- Open the Swagger UI in your browser:
http://127.0.0.1:8000/docs
GET /returns a basic status message and quick linksGET /healthchecks if the API is running and whether the model artifact existsGET /model-inforeturns model metadata, feature names, class labels, and evaluation metricsGET /sample-payloadsreturns sample JSON payloads for each wine classPOST /predictpredicts the wine class from the request body
curl -X POST "http://127.0.0.1:8000/predict" \
-H "Content-Type: application/json" \
-d '{
"alcohol": 14.23,
"malic_acid": 1.71,
"ash": 2.43,
"alcalinity_of_ash": 15.6,
"magnesium": 127.0,
"total_phenols": 2.8,
"flavanoids": 3.06,
"nonflavanoid_phenols": 0.28,
"proanthocyanins": 2.29,
"color_intensity": 5.64,
"hue": 1.04,
"od280_od315_of_diluted_wines": 3.92,
"proline": 1065.0
}'{
"predicted_class_id": 0,
"predicted_class_name": "class_0",
"class_probabilities": {
"class_0": 0.999591,
"class_1": 0.000377,
"class_2": 0.000031
}
}After running python3 -m src.train, the saved metadata reported:
- Test accuracy:
1.0 - Test macro F1 score:
1.0 - Training rows:
133 - Test rows:
45
python3 -m pytest tests/test_api.py -v- This lab is from the
API_Labsfolder, which is different from the folders I used in earlier submissions - The dataset and model are different from the original repo version
- The README is fully customized for my version of the lab