Spaces:
Running
Running
File size: 3,302 Bytes
1d971c4 5aa4b6e 1d971c4 e559a71 1d971c4 e559a71 1d971c4 e559a71 1d971c4 e559a71 5aba070 e559a71 5aba070 e559a71 5aba070 e559a71 5aba070 e559a71 5aba070 e559a71 5aba070 e559a71 5aba070 e559a71 5aba070 e559a71 5aba070 e559a71 1d971c4 e559a71 5aba070 1d971c4 e559a71 5aba070 e559a71 5aba070 e559a71 5aba070 e559a71 5aba070 e559a71 1d971c4 e559a71 | 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 149 150 151 152 153 154 155 156 | ---
title: Food Spoilage Detection
emoji: π
colorFrom: green
colorTo: red
sdk: docker
app_file: app.py
pinned: false
---
# siPeka β Food Spoilage Detection API
A REST API that classifies fruits and vegetables as **fresh** or **spoiled** using a fine-tuned Swin Transformer Tiny.
**Live demo:** [huggingface.co/spaces/crtal/siPeka-model-api](https://huggingface.co/spaces/crtal/siPeka-model-api)
---
## Tech Stack
| Layer | Technology |
|---|---|
| API Framework | FastAPI |
| Model Architecture | Swin Transformer Tiny (torchvision) |
| Training | PyTorch 2.10 Β· AdamW Β· Cosine Annealing LR |
| Inference | CUDA / CPU (auto-detected) |
| Containerization | Docker |
| Deployment | Hugging Face Spaces |
| CI/CD | GitHub Actions |
---
## API Reference
### `GET /`
Returns basic API info.
**Response**
```json
{
"message": "Food Spoilage Detection API is running.",
"usage": "POST an image to /predict",
"supported_classes": ["fresh", "spoiled"]
}
```
---
### `GET /health`
Health check endpoint.
**Response**
```json
{ "status": "ok" }
```
---
### `POST /predict`
Classifies an image as fresh or spoiled.
**Request**
| Field | Type | Description |
|---|---|---|
| `file` | `multipart/form-data` | Image file (`.jpg`, `.jpeg`, `.png`) |
**Constraints**
- Max file size: 20 MB
- File must be a valid image readable by PIL
**Responses**
`200 OK` β Successful prediction
```json
{
"status": "success",
"prediction": "fresh",
"confidence": 0.95,
"model": "swin_transformer_tiny_finetuned"
}
```
`200 OK` β Low confidence (image may not be a food item)
```json
{
"status": "ambiguous",
"confidence": 0.61,
"message": "Model confidence too low. The image might not be a recognized food item or is too low quality.",
"suggestion": "Please provide a clearer image of a fruit or vegetable."
}
```
`400 Bad Request` β Invalid input
```json
{ "detail": "File too large. Maximum size is 20 MB." }
{ "detail": "Could not read image. Please upload a valid image file." }
```
`500 Internal Server Error` β Inference failure
```json
{ "detail": "Error processing image: <reason>" }
```
**Example**
```bash
curl -X POST "https://crtal-sipeka-model-api.hf.space/predict" \
-F "file=@apple.jpg"
```
---
## Run Locally
```bash
git clone https://github.com/yukienjoyer7/sipeka-model-api.git
cd sipeka-model-api
pip install -r requirements.txt
uvicorn app:app --host 0.0.0.0 --port 7860
```
---
## Training
### 1. Prepare data
```bash
python scripts/prepare_data.py
```
Restructures the raw dataset into binary `fresh` / `spoiled` classes and splits into `train` / `val` / `test` (70 / 15 / 15).
### 2. Train
```bash
python scripts/train.py \
--arch swin_t_finetune \
--output models/spoilage_model_swin_finetuned.pth \
--epochs 20 \
--lr 0.0001 \
--scheduler
```
| `--arch` | Description |
|---|---|
| `mobilenet` | MobileNetV2, head only |
| `swin_t` | Swin Transformer Tiny, head only |
| `swin_t_finetune` | Swin Transformer Tiny, last block + head unfrozen |
| `swin_s` | Swin Transformer Small, last block + head unfrozen |
### 3. Evaluate
```bash
python scripts/evaluate.py \
--arch swin_t_finetune \
--weights models/spoilage_model_swin_finetuned.pth
```
Prints a full classification report and confusion matrix on the test set.
|