https://github.com/uday160386/ml-model-as-rest-api
A simple example on how to provide ML model (DecissionTreeClassifier) as a REST Service. The app is containerize and deployed in Azure Cloud
https://github.com/uday160386/ml-model-as-rest-api
ai-ml fastapi machine-learning machine-learning-algorithms ml-ops python vuk-ai-cloud
Last synced: about 2 months ago
JSON representation
A simple example on how to provide ML model (DecissionTreeClassifier) as a REST Service. The app is containerize and deployed in Azure Cloud
- Host: GitHub
- URL: https://github.com/uday160386/ml-model-as-rest-api
- Owner: uday160386
- Created: 2024-05-18T23:47:39.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-08-25T10:22:09.000Z (10 months ago)
- Last Synced: 2025-08-25T12:32:58.953Z (10 months ago)
- Topics: ai-ml, fastapi, machine-learning, machine-learning-algorithms, ml-ops, python, vuk-ai-cloud
- Language: Python
- Homepage:
- Size: 89.3 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ML Model Integration and Deployment
Supervised(DecisionTreeClassifier) machine learning algorithm, that trains on the Iris dataset, exposes it as a REST API using FastAPI, and deploys it as an Azure Function for scalable predictions.
## ๐ Features
- **Machine Learning Model**: DecisionTreeClassifier trained on the famous Iris dataset
- **REST API**: FastAPI-powered endpoints with automatic documentation
- **Cloud Deployment**: Serverless deployment on Azure Functions
- **Containerization**: Docker support for consistent environments
## ๐โโ๏ธ Quick Start
### Prerequisites
- Python 3.8+
- pip or conda
- Docker (optional)
- Azure CLI (for Azure deployment)
### Local Installation
1. **Clone the repository**
```bash
git clone
cd ml-model-deployment
```
2. **Install dependencies**
```bash
pip install -r requirements.txt
```
3. **Run the application**
```bash
uvicorn main:app --reload
```
4. **Access the application**
- API: http://localhost:8000
- Interactive docs: http://localhost:8000/docs
- Alternative docs: http://localhost:8000/redoc
## ๐ API Documentation
### Endpoints
#### `POST /predict`
Predict the Iris species based on flower measurements.
**Request Body:**
```json
{
"sepal_length": 5.1,
"sepal_width": 3.5,
"petal_length": 1.4,
"petal_width": 0.2
}
```
**Response:**
```json
{
"prediction": "setosa",
"confidence": 0.95,
"all_probabilities": {
"setosa": 0.95,
"versicolor": 0.03,
"virginica": 0.02
}
}
```
## ๐ณ Docker Deployment
```bash
docker run -p 8000:8000 venmaum/ml-simple-app
```
## โ๏ธ Azure Functions Deployment
### Prerequisites
- Azure CLI installed and configured
- Azure Functions Core Tools
### Deployment Steps
1. **Login to Azure**
```bash
az login
```
2. **Create Resource Group**
```bash
az group create --name ml-app-rg --location eastus
```
3. **Deploy Function App**
```bash
func azure functionapp publish
```
4. **Test the deployed function**
```bash
curl -X POST https://.azurewebsites.net/api/predict \
-H "Content-Type: application/json" \
-d '{"sepal_length": 5.1, "sepal_width": 3.5, "petal_length": 1.4, "petal_width": 0.2}'
```
## ๐บ Dataset Information
**Iris Dataset** from scikit-learn:
- **Samples**: 150
- **Features**: 4 (sepal length, sepal width, petal length, petal width)
- **Classes**: 3 (Setosa, Versicolor, Virginica)
- **Type**: Multiclass classification
The dataset is automatically loaded and preprocessed when training the model.
## ๐ Project Structure
```
ml-model-deployment/
โโโ main.py # FastAPI application
โโโ model/
โ โโโ __init__.py
โ โโโ train.py # Model training script
โ โโโ iris_model.joblib # Trained model file
โโโ requirements.txt # Python dependencies
โโโ Dockerfile # Docker configuration
โโโ README.md # This file
```
## ๐ ๏ธ Development
### Setting up Development Environment
1. **Create virtual environment**
```bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```
2. **Install development dependencies**
```bash
pip install -r requirements-dev.txt
```
## ๐งช Testing
### Manual Testing
Use the provided examples in the interactive documentation or test with curl:
```bash
curl -X POST "http://localhost:8000/predict" \
-H "Content-Type: application/json" \
-d '{
"sepal_length": 6.3,
"sepal_width": 2.5,
"petal_length": 5.0,
"petal_width": 1.9
}'
```
## ๐ Model Performance
- **Accuracy**: ~96% on test set
- **Training Time**: < 1 second
- **Prediction Time**: < 10ms per request
- **Model Size**: ~2KB
3. **Model File Not Found**: Retrain the model
```bash
python model/train.py
## ๐ References
- [Scikit-learn Decision Trees](https://scikit-learn.org/stable/modules/tree.html)
- [FastAPI Documentation](https://fastapi.tiangolo.com/)
- [Azure Functions Python Guide](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python)
## ๐ท๏ธ Tags
`machine-learning` `fastapi` `azure-functions` `docker` `iris-dataset` `decision-tree` `rest-api` `python` `scikit-learn`