File size: 4,342 Bytes
bf17f74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Backend Inference Service

FastAPI-based REST API for waste classification inference and feedback collection.

## Setup

### 1. Install Dependencies

\`\`\`bash
pip install -r backend/requirements.txt
pip install -r ml/requirements.txt
\`\`\`

### 2. Train or Download Model

Ensure you have a trained model at `ml/models/best_model.pth`:

\`\`\`bash
# Train a model
python ml/train.py

# Or download a pretrained model (if available)
# Place it in ml/models/best_model.pth
\`\`\`

### 3. Start Service

\`\`\`bash
# Development
python backend/inference_service.py

# Production with Gunicorn
gunicorn backend.inference_service:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
\`\`\`

Service will be available at `http://localhost:8000`

## API Endpoints

### Health Check

\`\`\`bash
GET /
GET /health
\`\`\`

Response:
\`\`\`json
{
  "status": "healthy",
  "model_loaded": true,
  "timestamp": "2024-01-01T00:00:00"
}
\`\`\`

### Predict

\`\`\`bash
POST /predict
Content-Type: application/json

{
  "image": "data:image/jpeg;base64,/9j/4AAQ..."
}
\`\`\`

Response:
\`\`\`json
{
  "category": "recyclable",
  "confidence": 0.95,
  "probabilities": {
    "recyclable": 0.95,
    "organic": 0.02,
    "wet-waste": 0.01,
    "dry-waste": 0.01,
    "ewaste": 0.005,
    "hazardous": 0.003,
    "landfill": 0.002
  },
  "timestamp": 1704067200000
}
\`\`\`

### Feedback

\`\`\`bash
POST /feedback
Content-Type: application/json

{
  "image": "data:image/jpeg;base64,/9j/4AAQ...",
  "predicted_category": "recyclable",
  "corrected_category": "organic",
  "confidence": 0.75
}
\`\`\`

Response:
\`\`\`json
{
  "status": "success",
  "message": "Feedback saved for retraining",
  "saved_path": "ml/data/retraining/organic/feedback_20240101_120000.jpg"
}
\`\`\`

### Trigger Retraining

\`\`\`bash
POST /retrain
Authorization: Bearer <ADMIN_API_KEY>
\`\`\`

Response:
\`\`\`json
{
  "status": "started",
  "message": "Retraining initiated with 150 new samples",
  "feedback_count": 150
}
\`\`\`

### Retraining Status

\`\`\`bash
GET /retrain/status
\`\`\`

Response:
\`\`\`json
{
  "status": "success",
  "total_retrains": 3,
  "events": [...],
  "latest": {
    "version": 3,
    "timestamp": "2024-01-01T00:00:00",
    "accuracy": 92.5,
    "improvement": 2.3,
    "new_samples": 150
  }
}
\`\`\`

### Statistics

\`\`\`bash
GET /stats
\`\`\`

Response:
\`\`\`json
{
  "model_loaded": true,
  "categories": ["recyclable", "organic", ...],
  "feedback_samples": 150,
  "feedback_by_category": {
    "recyclable": 45,
    "organic": 38,
    ...
  }
}
\`\`\`

## Docker Deployment

### Build and Run

\`\`\`bash
# Build image
docker build -f backend/Dockerfile -t waste-classification-api .

# Run container
docker run -p 8000:8000 \
  -v $(pwd)/ml/models:/app/ml/models \
  -v $(pwd)/ml/data:/app/ml/data \
  waste-classification-api
\`\`\`

### Using Docker Compose

\`\`\`bash
# Start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down
\`\`\`

## Environment Variables

- `PORT`: Server port (default: 8000)
- `ADMIN_API_KEY`: Admin key for retraining endpoint

## Performance

- **Inference Time**: ~50ms per image (CPU)
- **Throughput**: ~20 requests/second (single worker)
- **Memory**: ~500MB with model loaded
- **Scaling**: Deploy multiple workers for higher throughput

## Production Deployment

### Railway / Render

1. Connect your repository
2. Set build command: `pip install -r backend/requirements.txt -r ml/requirements.txt`
3. Set start command: `python backend/inference_service.py`
4. Set environment variables
5. Deploy

### AWS EC2

1. Launch EC2 instance (t3.medium or higher)
2. Install Docker
3. Clone repository
4. Run with Docker Compose
5. Configure security group (port 8000)
6. Set up SSL with Nginx reverse proxy

### Vercel (Not Recommended)

FastAPI with ML models exceeds serverless function limits. Use Railway, Render, or AWS EC2 instead.

## Monitoring

Add application monitoring:

\`\`\`python
from prometheus_fastapi_instrumentator import Instrumentator

Instrumentator().instrument(app).expose(app)
\`\`\`

Access metrics at `/metrics`

## Security

- Add rate limiting with `slowapi`
- Implement proper authentication
- Validate image sizes and formats
- Use HTTPS in production
- Restrict CORS origins
- Sanitize file uploads
\`\`\`