File size: 3,361 Bytes
c0afe44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# API Reference

Base URL: `http://localhost:8000` · API prefix: `/api/v1` · Interactive docs: `/docs`

All responses are JSON unless noted. Errors use standard HTTP status codes with
a `{"detail": "..."}` body.

---

## Meta

### `GET /`
Landing info (name, version, docs link).

### `GET /health`
```json
{ "status": "ok", "version": "1.0.0", "model_loaded": false, "database": "sqlite" }
```

---

## Analysis

### `POST /api/v1/analyze`
Analyse a meal image end-to-end. **`multipart/form-data`.**

| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `file` | file | — | Meal image (JPEG/PNG/…) **required** |
| `school_name` | str | — | School name (for analytics) |
| `district` | str | — | District |
| `category` | str | `primary` | Compliance standard key |
| `assess_freshness` | bool | `true` | Run freshness assessment |
| `include_image` | bool | `true` | Return annotated image (base64 PNG) |
| `store` | bool | `true` | Persist to the database |
| `generate_report` | bool | `false` | Also create a PDF report |

**Response** (`AnalyzeResponse`, abridged):
```json
{
  "image_size": {"width": 640, "height": 640},
  "num_items": 4,
  "detections": [{"class_id": 0, "food_item": "rice", "confidence": 0.92, "bbox": [50,300,300,600]}],
  "meal": {"num_items": 4, "items": [ ... ], "totals": {"calories": 556.5, "protein_g": 23.2, ...}},
  "macro_distribution": {"protein": 15.1, "carbohydrates": 63.7, "fat": 21.2},
  "daily_values": {"calories": 27.8, "protein_g": 58.0, ...},
  "compliance": {"status": "Compliant", "calorie_pct": 123.7, "recommendations": ["..."], ...},
  "quality": {"assessed": true, "items_flagged": 0, "min_freshness_score": 0.91},
  "annotated_image": "<base64 PNG>",
  "analysis_id": 12,
  "report_available": true
}
```
**Errors:** `400` empty/invalid image · `413` too large · `503` ML stack missing.

Example:
```bash
curl -F "file=@meal.jpg" -F "category=primary" -F "generate_report=true" \
     http://localhost:8000/api/v1/analyze
```

### `POST /api/v1/nutrition/analyze`
Compute nutrition + compliance from detections only (no image). **JSON body:**
```json
{
  "detections": [{"food_item": "rice", "confidence": 0.9}, {"food_item": "dal"}],
  "image_width": 640, "image_height": 640, "category": "primary"
}
```
Returns `meal`, `compliance`, `macro_distribution`, `daily_values`.
`422` if none of the items are in the nutrition database.

---

## Nutrition reference

### `GET /api/v1/foods`
List every food in the nutrition database with per-100 g values.

### `GET /api/v1/compliance-standards`
```json
[{"key": "primary", "label": "Primary (Class I-V)", "calories": 450, "protein_g": 12}, ...]
```

---

## History & statistics

### `GET /api/v1/history?limit=50&offset=0&school_name=...`
List stored analyses (most recent first).

### `GET /api/v1/history/{id}`
Full stored analysis including all detected items. `404` if not found.

### `DELETE /api/v1/history/{id}`
Delete an analysis (cascades to items).

### `GET /api/v1/stats`
Aggregate analytics: totals, compliance rate, averages, `by_status`,
`by_school`, `top_foods`.

---

## Reports

### `GET /api/v1/reports/{id}`
Download the PDF report for an analysis (`application/pdf`). `404` if none.

### `POST /api/v1/reports/{id}/generate`
(Re)generate the PDF for a stored analysis; returns a `download_url`.