siPeka-model-api / README.md
crtal's picture
docs: rewrite README with tech stack, full API reference, and training guide
e559a71
|
Raw History Blame Contribute Delete
3.3 kB
---
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.