Are you sure you want to delete this access key?
This guide provides all the commands needed to set up and run the iris model serving API locally and with Docker.
Run the automated setup script:
./setup.sh
pip install -r requirements.txt
python simple_train.py
python app.py
The API will be available at:
curl http://localhost:8000/
curl -X POST "http://localhost:8000/predict" \
-H "Content-Type: application/json" \
-d '{"features": [5.1, 3.5, 1.4, 0.2]}'
Expected response:
{
"prediction": 0,
"features": [5.1, 3.5, 1.4, 0.2]
}
docker build -t iris-model-api .
docker run -p 8000:8000 iris-model-api
# Test welcome endpoint
curl http://localhost:8000/
# Test prediction
curl -X POST "http://localhost:8000/predict" \
-H "Content-Type: application/json" \
-d '{"features": [5.9, 3.0, 5.1, 1.8]}'
# Install Heroku CLI first
heroku create your-iris-api
git push heroku main
# Connect your GitHub repo to Railway
# Set PORT environment variable to 8000
gcloud run deploy iris-api \
--source . \
--platform managed \
--region us-central1 \
--allow-unauthenticated
/
Returns a welcome message.
Response:
{
"message": "Welcome to the Iris Model Prediction API"
}
/predict
Makes predictions using the trained iris model.
Request Body:
{
"features": [5.1, 3.5, 1.4, 0.2]
}
Response:
{
"prediction": 0,
"features": [5.1, 3.5, 1.4, 0.2]
}
Iris Classes:
0
: Iris Setosa1
: Iris Versicolor2
: Iris VirginicaIf you get a "Model not loaded" error:
# Train a new model
python simple_train.py
# Restart the API
python app.py
If port 8000 is busy:
# Kill existing processes
pkill -f "python app.py"
# Or use a different port
uvicorn app:app --host 0.0.0.0 --port 8001
Ensure you have:
models/iris_model.pkl
# Simple load test
for i in {1..100}; do
curl -s -X POST "http://localhost:8000/predict" \
-H "Content-Type: application/json" \
-d '{"features": [5.1, 3.5, 1.4, 0.2]}' &
done
wait
# Install apache2-utils first
ab -n 1000 -c 10 -T "application/json" \
-p test_payload.json http://localhost:8000/predict
Create test_payload.json
:
{ "features": [5.1, 3.5, 1.4, 0.2] }
Add this to your app.py
for monitoring:
@app.get("/health")
async def health_check():
return {
"status": "healthy",
"model_loaded": model is not None,
"timestamp": datetime.now().isoformat()
}
# Docker logs
docker logs <container_id>
# Local logs
tail -f /var/log/iris-api.log
import requests
def predict_iris(features):
response = requests.post(
"http://localhost:8000/predict",
json={"features": features}
)
return response.json()
# Usage
result = predict_iris([5.1, 3.5, 1.4, 0.2])
print(f"Predicted class: {result['prediction']}")
async function predictIris(features) {
const response = await fetch("http://localhost:8000/predict", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ features: features }),
});
return await response.json();
}
// Usage
predictIris([5.1, 3.5, 1.4, 0.2]).then((result) =>
console.log("Prediction:", result.prediction)
);
You now have a complete model serving solution that:
The API is production-ready and can handle real-time predictions for the Iris dataset classification task.
Press p or to see the previous file or, n or to see the next file
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?