This documentation provides clear steps for running the FastAPI-based machine learning model API. Follow these steps to set up and run the code in your environment.
Here's a high-level overview of the project structure, showing key files and folders:
├── README.md # Project documentation in English
├── main.py # Entry point for the FastAPI server
├── requirements.txt # List of dependencies
├── data # Contains data files
│ ├── diabetes.csv # Original dataset
│ └── processed_data.csv # Processed dataset
├── models # Directory for models
│ ├── final_model.pkl # Serialized best model
│ └── model_pipeline.py # Script for training and saving the model
├── src # Contains helper scripts
│ └── data_pipeline.py # Data preprocessing functions
└── docs # Documentation files and images
├── all_cicd_pipeline_success.png
├── final_result_prediction_success.png
└── mlops_kurzus_tematikaja.txt
Ensure that main.py
and the required model files (e.g., final_model.pkl
) are available in the directory where you're working.
Navigate to the directory where requirements.txt
is located, and install the necessary Python packages with the following command:
pip install -r requirements.txt
Alternatively, you can install each required package individually:
pip install fastapi==0.112.2 uvicorn==0.30.0 scikit-learn==1.5.1 pandas==2.2.2 numpy==1.25.0 xgboost==2.1.1
Start the FastAPI server with this command, making sure main
is the name of the file where your FastAPI application is defined:
uvicorn main:app --reload
This will launch the server, which listens on http://127.0.0.1:8000
.
FastAPI automatically generates an interactive API documentation interface. Access it in your browser at:
http://127.0.0.1:8000/docs
Here, you can test the API endpoints and see the expected input and output formats.
To make a prediction, use a POST
request to the following URL:
http://127.0.0.1:8000/predict
Send a sample request with this JSON payload:
{
"Pregnancies": 6,
"Glucose": 148,
"BloodPressure": 72,
"SkinThickness": 35,
"Insulin": 0,
"BMI": 33.6,
"DiabetesPedigreeFunction": 0.627,
"Age": 50
}
curl -X POST "http://127.0.0.1:8000/predict" \
-H "Content-Type: application/json" \
-d '{
"Pregnancies": 6,
"Glucose": 148,
"BloodPressure": 72,
"SkinThickness": 35,
"Insulin": 0,
"BMI": 33.6,
"DiabetesPedigreeFunction": 0.627,
"Age": 50
}'
The server will return the prediction in JSON format, such as:
{
"prediction": 1
}
-
ModuleNotFoundError: No module named 'fastapi'
: Ensure that all required packages are installed correctly by following Step 2. -
ValueError: X has 8 features, but the model expects a different number
: This error indicates that the model expects more input features than provided. Double-check the model requirements and input data format. -
Port Conflict: If port
8000
is already in use, try a different port with the following command:uvicorn main:app --reload --port 8080
mlflow ui vagy mlflow ui --port 8000