Ubuntu
Add multi-variant FastAPI support (code and configs)
59b15f5
# FastAPI Multi-Variant API for Milk Spoilage Classification
This API supports 10 model variants optimized for different data availability scenarios.
## Features
- **10 Model Variants** with test accuracies ranging from 62.8% to 95.8%
- **Automatic Feature Validation** - API validates you provide required features for selected variant
- **RESTful API** with comprehensive OpenAPI documentation
- **Custom GPT Ready** - Designed for seamless ChatGPT integration
## Quick Start
### Local Development
```bash
# Install dependencies
pip install -r requirements.txt
# Run the server
python app.py
```
The API will be available at `http://localhost:7860`
### Docker Deployment
```bash
docker build -t milk-spoilage-api .
docker run -p 7860:7860 milk-spoilage-api
```
## API Endpoints
### GET /variants
List all available model variants with metadata
### POST /predict
Make a prediction using the specified model variant
**Request Body:**
```json
{
"spc_d7": 2.1,
"spc_d14": 4.7,
"spc_d21": 6.4,
"tgn_d7": 1.0,
"tgn_d14": 3.7,
"tgn_d21": 5.3,
"model_variant": "baseline"
}
```
**Response:**
```json
{
"prediction": "PPC",
"probabilities": {
"PPC": 0.97,
"no spoilage": 0.02,
"spore spoilage": 0.01
},
"confidence": 0.97,
"variant_used": {
"variant_id": "baseline",
"name": "Baseline (All Features)",
"description": "Uses all 6 microbiological measurements",
"features": ["SPC_D7", "SPC_D14", "SPC_D21", "TGN_D7", "TGN_D14", "TGN_D21"],
"test_accuracy": 0.9576
}
}
```
## Model Variants
| Variant | Test Acc | Features Required |
|---------|----------|-------------------|
| baseline | 95.8% | All 6 features |
| scenario_1_days14_21 | 94.2% | SPC_D14, SPC_D21, TGN_D14, TGN_D21 |
| scenario_3_day21 | 93.7% | SPC_D21, TGN_D21 |
| scenario_4_day14 | 87.4% | SPC_D14, TGN_D14 |
| scenario_2_days7_14 | 87.3% | SPC_D7, SPC_D14, TGN_D7, TGN_D14 |
| scenario_6_spc_all | 78.3% | SPC_D7, SPC_D14, SPC_D21 |
| scenario_8_spc_7_14 | 73.3% | SPC_D7, SPC_D14 |
| scenario_9_tgn_7_14 | 73.1% | TGN_D7, TGN_D14 |
| scenario_7_tgn_all | 69.9% | TGN_D7, TGN_D14, TGN_D21 |
| scenario_5_day7 | 62.8% | SPC_D7, TGN_D7 |
## Custom GPT Integration
See `../../docs/CUSTOM_GPT_SETUP_MULTIVARIANT.md` for complete setup instructions.
## Example Usage
### Python
```python
import requests
url = "https://chenhaoq87-milkspoilageclassifier-api-variants.hf.space/predict"
# Using baseline model with all features
response = requests.post(url, json={
"spc_d7": 2.1,
"spc_d14": 4.7,
"spc_d21": 6.4,
"tgn_d7": 1.0,
"tgn_d14": 3.7,
"tgn_d21": 5.3,
"model_variant": "baseline"
})
print(response.json())
# Using Day 21 only model (when you don't have earlier measurements)
response = requests.post(url, json={
"spc_d21": 6.4,
"tgn_d21": 5.3,
"model_variant": "scenario_3_day21"
})
print(response.json())
```
### cURL
```bash
# List available variants
curl https://chenhaoq87-milkspoilageclassifier-api-variants.hf.space/variants
# Make prediction with baseline model
curl -X POST https://chenhaoq87-milkspoilageclassifier-api-variants.hf.space/predict \
-H "Content-Type: application/json" \
-d '{
"spc_d7": 2.1,
"spc_d14": 4.7,
"spc_d21": 6.4,
"tgn_d7": 1.0,
"tgn_d14": 3.7,
"tgn_d21": 5.3,
"model_variant": "baseline"
}'
```
## License
MIT