A FastAPI-based REST API for classifying Iris flower species using machine learning. This service provides endpoints for single predictions, batch processing, and file uploads.
- Overview
- Features
- API Endpoints
- Installation
- Usage
- API Reference
- Request/Response Models
- Error Handling
- CORS Configuration
- Testing
- Deployment
- Performance
- Author
This FastAPI service provides a production-ready REST API for the Iris flower classification model. It offers multiple prediction endpoints to handle different use cases, from single sample predictions to batch processing and file uploads.
The API is built on top of the trained Logistic Regression model and provides:
- Real-time predictions with probability scores
- Batch processing capabilities
- File upload support for CSV datasets
- Comprehensive error handling
- CORS support for web applications
- Interactive API documentation
- RESTful API: Clean, intuitive REST endpoints
- Multiple Input Methods: Single samples, batch requests, and file uploads
- Probability Scores: Returns prediction probabilities for all classes
- CORS Enabled: Ready for web application integration
- Interactive Documentation: Auto-generated Swagger UI and ReDoc
- Error Handling: Comprehensive error responses
- Type Safety: Pydantic models for request/response validation
- Health Check: Service health monitoring endpoint
| Method | Endpoint | Description |
|---|---|---|
GET |
/ |
Welcome message and endpoint overview |
GET |
/health |
Health check endpoint |
POST |
/predict |
Single sample prediction |
POST |
/predict_batch |
Batch prediction for multiple samples |
POST |
/predict_file |
File upload prediction (CSV) |
GET |
/docs |
Interactive API documentation (Swagger UI) |
GET |
/redoc |
Alternative API documentation (ReDoc) |
- Python 3.8+
- Trained Iris model (run
main.pyfirst to generate the model) - uv package manager (recommended)
-
Ensure the model is trained
# Train the model first if not already done uv run main.py -
Install FastAPI dependencies
# Add FastAPI dependencies to your project uv add fastapi uvicorn python-multipart -
Alternative: Install with pip
pip install fastapi uvicorn python-multipart
Development Server:
# Using uv
uv run uvicorn service:app --reload --host 0.0.0.0 --port 8000
# Traditional method
uvicorn service:app --reload --host 0.0.0.0 --port 8000Production Server:
# Using uv with Gunicorn
uv run gunicorn service:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
# Traditional method
gunicorn service:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000The API will be available at:
- API Base URL:
http://localhost:8000 - Interactive Docs:
http://localhost:8000/docs - Alternative Docs:
http://localhost:8000/redoc
curl -X GET "http://localhost:8000/health"Response:
{
"status": "ok"
}curl -X POST "http://localhost:8000/predict" \
-H "Content-Type: application/json" \
-d '{
"sepal_length": 5.1,
"sepal_width": 3.5,
"petal_length": 1.4,
"petal_width": 0.2
}'Response:
{
"prediction": "setosa",
"probabilities": {
"setosa": 0.9876,
"versicolor": 0.0123,
"virginica": 0.0001
}
}curl -X POST "http://localhost:8000/predict_batch" \
-H "Content-Type: application/json" \
-d '{
"samples": [
{
"sepal_length": 5.1,
"sepal_width": 3.5,
"petal_length": 1.4,
"petal_width": 0.2
},
{
"sepal_length": 6.7,
"sepal_width": 3.1,
"petal_length": 4.7,
"petal_width": 1.5
}
]
}'Response:
[
{
"prediction": "setosa",
"probabilities": {
"setosa": 0.9876,
"versicolor": 0.0123,
"virginica": 0.0001
}
},
{
"prediction": "versicolor",
"probabilities": {
"setosa": 0.0023,
"versicolor": 0.8456,
"virginica": 0.1521
}
}
]curl -X POST "http://localhost:8000/predict_file" \
-H "Content-Type: multipart/form-data" \
-F "file=@iris_sample.csv"Sample CSV format:
sepal_length,sepal_width,petal_length,petal_width
5.1,3.5,1.4,0.2
6.7,3.1,4.7,1.5
5.8,2.7,5.1,1.9
class Features(BaseModel):
sepal_length: float # Sepal length in cm
sepal_width: float # Sepal width in cm
petal_length: float # Petal length in cm
petal_width: float # Petal width in cmclass BatchRequest(BaseModel):
samples: List[Features] # List of feature samplesclass PredictionResult(BaseModel):
prediction: str # Predicted species name
probabilities: Dict[str, float] # Prediction probabilities by classDescription: Welcome endpoint with API overview Response: JSON object with welcome message and available endpoints
Description: Health check endpoint for monitoring
Response: {"status": "ok"}
Description: Predict species for a single sample
Request Body: Features model
Response: PredictionResult model
Description: Predict species for multiple samples
Request Body: BatchRequest model
Response: List[PredictionResult]
Description: Predict species from uploaded CSV file Request: Multipart form data with CSV file Response: List of prediction results File Requirements:
- Content type:
text/csv - Required columns:
sepal_length,sepal_width,petal_length,petal_width
The API provides comprehensive error handling:
200: Success400: Bad Request (invalid input data)422: Validation Error (invalid request format)500: Internal Server Error
{
"detail": "Only CSV files are accepted."
}- Invalid file type: Only CSV files are accepted for file upload
- Missing features: All four features must be provided
- Invalid data types: Features must be numeric values
- Model not found: Ensure the model is trained and saved
The API is configured with CORS middleware to allow cross-origin requests:
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allow all origins
allow_methods=["POST", "GET"], # Allowed HTTP methods
allow_headers=["Content-Type"], # Allowed headers
)Production Note: Replace allow_origins=["*"] with specific domains for security.
Test the API using the interactive documentation at http://localhost:8000/docs or use curl commands as shown in the usage examples above.
Create a Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "service:app", "--host", "0.0.0.0", "--port", "8000"]Build and run:
docker build -t iris-api .
docker run -p 8000:8000 iris-api- Heroku: Use
Procfilewithweb: uvicorn service:app --host=0.0.0.0 --port=${PORT:-5000} - AWS Lambda: Use Mangum adapter for serverless deployment
- Google Cloud Run: Deploy with cloud-native scaling
- Azure Container Instances: Easy container deployment
Set these environment variables for production:
MODEL_PATH: Path to the trained model fileHOST: Server host (default: 0.0.0.0)PORT: Server port (default: 8000)
- Single Prediction: ~10-50ms response time
- Batch Processing: Scales linearly with sample count
- Memory Usage: ~50-100MB per worker process
- Model is cached using
@lru_cachefor efficient memory usage
Utkarsh Gaikwad
This project is open source and available under the MIT License.
Quick Start:
- Train the model:
uv run main.py - Start the API:
uv run uvicorn service:app --reload - Visit
http://localhost:8000/docsfor interactive documentation - Test with:
curl -X GET "http://localhost:8000/health"