File size: 3,370 Bytes
59b15f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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