This project demonstrates a simple inference server using Flask, Docker, and a pre-trained machine learning model based on the Iris dataset. The server provides an API to predict the category of an Iris plant based on its features.
To run this project, you need to have the following installed:
- Docker
- Docker Compose
If Docker is not installed, follow these steps:
-
Update your package list:
sudo apt-get update
-
Install required dependencies:
sudo apt-get install ca-certificates curl gnupg lsb-release
-
Add Docker’s official GPG key:
sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg -
Set up the Docker stable repository:
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
-
Install Docker:
sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
-
Start Docker:
sudo systemctl start docker sudo systemctl enable docker
-
Clone the repository:
git clone https://github.com/your-username/inference-server-example.git cd inference-server-example -
Install required Python packages (if you are running locally):
pip install -r requirements.txt
Before train the model, you need to create data directory and insert iris_with_category_index.csv file.
Before running the server, you need to train the model and save the trained model as a .pkl file. Run the following command to execute train.py and generate the model file:
python train.pyThis will generate a file named iris_model.pkl model directory in the project.
Once the model is trained and the server is running, you can interact with the API.
If you want to run the server inside Docker, use the following command to build and run the Docker container:
docker-compose up --buildThe API will be accessible at http://localhost:5000.
To get a prediction for the Iris dataset, send a GET request:
curl http://localhost:5000/predict/1{
"data_id": 1,
"class": 0
}If you request an invalid data_id, the server will return a 404 error:
curl http://localhost:5000/predict/9999Response:
{
"error": "Data not found"
}- URL: /predict/<data_id>
- Method: GET
- Parameters:
- data_id (integer): The ID of the data entry from the Iris dataset to predict.
- Response:
- Success: 200 OK with a JSON body containing:
- data_id: The ID of the input data.
- class: The predicted category (classification).
- Failure: 404 Not Found if the data_id is invalid or does not exist.
- Success: 200 OK with a JSON body containing:
Example:
GET /predict/1{
"data_id": 1,
"class": 0
}Logs are stored in the logs directory and can be accessed both from inside and outside the Docker container.
-
The main log file is located at logs/app.log.
-
You can inspect logs using:
cat logs/app.log
-
If the container fails to start, check the logs using:
docker-compose logs
-
If you need to stop the container:
docker-compose down
If you encounter any issues or need further help, please open an issue in the repository.