File size: 14,894 Bytes
1aa8eca | 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# ποΈ Waste Intelligence API β Complete Documentation
> **AI-Powered Predictive Waste Management System for Jakarta Pusat (CASE 2)**
> Version: `2.0.0` | License: `MIT` | Author: `Faril Putra Pratama - SMK Taruna Bangsa`
---
## π Table of Contents
1. [Project Overview](#1-project-overview)
2. [System Architecture](#2-system-architecture)
3. [Core AI & Business Logic](#3-core-ai--business-logic)
4. [API Reference](#4-api-reference)
5. [Data Dictionary](#5-data-dictionary)
6. [Deployment & Setup](#6-deployment--setup)
7. [Testing & Validation](#7-testing--validation)
8. [Business Impact & Use Cases](#8-business-impact--use-cases)
9. [Roadmap & Scalability](#9-roadmap--scalability)
10. [Author & Support](#10-author--support)
---
## 1. Project Overview
### Problem Statement
Penumpukan sampah di Jakarta Pusat sering terjadi secara mendadak saat:
- οΈ Musim hujan tinggi (sampah basah β berat volume naik)
- πͺ Event besar (PRJ, Lebaran, Konser, HUT RI)
- π
Weekend & libur nasional
Penanganan saat ini masih **reaktif**: armada dikirim setelah laporan masuk atau tumpukan terlihat. Akibatnya: biaya operasional membengkak, jadwal pengangkutan tidak efisien, dan risiko kesehatan lingkungan meningkat.
### π‘ Solution
Sistem ini mengubah paradigma menjadi **prediktif** menggunakan:
- π€ **Amazon Chronos** (Transformer time-series) untuk forecasting baseline volume
- π¦οΈ **BMKG Weather Integration** untuk penyesuaian berat sampah basah
- π
**Event Calendar Engine** dengan location-aware impact modeling
- π **Logistics Optimizer** untuk rekomendasi armada & manpower presisi
**Output**: Prediksi volume sampah 1β30 hari ke depan per lokasi, dekomposisi organik/plastik, status risiko, dan rencana logistik operasional.
---
## 2. System Architecture
```
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β CLIENT LAYER β
β β’ Postman / Frontend Dashboard / Mobile App β
β β’ REST API Calls (JSON) β
ββββββββββββββββββββββββββββββββββββββββββββββββββ
β HTTPS / CORS
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β API GATEWAY (FastAPI) β
β β’ Request Validation (Pydantic) β
β β’ CORS Middleware β
β β’ Structured Logging β
βββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ
β
βββββββββββ΄ββββββββββ
βΌ βΌ
ββββββββββ βββββββββββββββ
β PREDICT β β STATUS β
βEndpoint β β Check β
ββββββ¬βββββ βββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β BUSINESS LOGIC LAYER β
β 1οΈβ£ Date Parser & Context Setup β
β 2οΈβ£ Chronos Inference (Async/ThreadPool) β
β 3οΈβ£ External Factor Integration β
β β’ Rain multiplier (BMKG) β
β β’ Event engine + radius mapping β
β β’ Soft impact scaling (10β35%) β
β 4οΈβ£ Post-Processing & Aggregation β
β β’ KLHK 2026 decomposition β
β β’ Risk scoring & truck calculation β
ββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ
β
βββββββββββ΄ββββββββββ
βΌ βΌ
βββββββββββ βββββββββββββββ
β DATA β β MODEL β
β LAYER β β LAYER β
β β’ CSV β β β’ Chronos β
β β’ In-memβ β T5-Tiny β
β Cache β β β’ PyTorch β
βββββββββββ βββββββββββββββ
```
### πΉ Tech Stack
| Layer | Technology |
|-------|------------|
| API Framework | FastAPI + Uvicorn |
| AI Model | Amazon Chronos-T5-Tiny (Hugging Face) |
| Data Processing | Pandas, NumPy |
| Validation | Pydantic v2 |
| Deployment | Hugging Face Spaces (CPU) |
| Logging | Python `logging` (structured) |
---
## 3. Core AI & Business Logic
### π€ 3.1 Time-Series Forecasting (Chronos)
- **Model**: `amazon/chronos-t5-tiny` (lightweight, CPU-optimized)
- **Input**: Historical volume series (`dataset_vibe_coder_2026.csv`, 365 hari)
- **Output**: Probabilistic forecast (median quantile `0.5`) untuk `N` hari ke depan
- **Advantage**: Mampu menangkap pola musiman, tren gradual, dan fluktuasi natural tanpa fitur engineering berat
### πͺ 3.2 Event Engine & Location Matching
Event tidak serta-merta menaikkan volume di seluruh kota. Sistem menggunakan **radius-aware logic**:
```python
EVENT_RADIUS_MAP = {
'jiexpo': ['jis', 'kemayoran', 'pademangan', 'jakarta'],
'monas': ['pasar senen', 'gang sempit tambora', 'merdeka', 'jakarta'],
'gbk': ['senayan', 'tanah abang', 'kuningan', 'jakarta'],
'ancol': ['pademangan', 'kelapa gading', 'jakarta'],
'jakarta': ['*'] # City-wide
}
```
- **Matching Rules**: Direct string match β City-wide fallback β Radius mapping
- **Impact Scaling**: `1.0 + (0.10 + min(scale * 0.05, 0.25))` β Maksimal **+35%** volume
- **Result**: Event di JIExpo hanya mempengaruhi JIS/Kemayoran, bukan GBK/Senayan
### π§οΈ 3.3 Weather Integration (BMKG Style)
Curah hujan mempengaruhi berat sampah (basah = lebih padat/berat):
- `β€20mm`: Tidak ada penyesuaian
- `>20mm`: Multiplier `1.02` hingga `1.05` (linear scaling)
- **Rationale**: Sampah organik menyerap air β tonase naik tanpa volume fisik berubah drastis
### οΈ 3.4 Risk Scoring Algorithm
```python
def hitung_prioritas(nama_lokasi, volume_ton):
akses = DATABASE_LOKASI[nama_lokasi]['aksesibilitas'] # 0.25 β 1.0
skor = volume_ton / akses
if skor > 1600: return 'CRITICAL β οΈ'
if skor >= 1100: return 'WARNING π‘'
return 'SAFE β
'
```
- **Accessibility Factor**: Lokasi sempit/sulit dijangkau (`0.25`) mendapat skor risiko lebih tinggi untuk volume yang sama
- **Thresholds**: Dikalibrasi untuk rentang volume realistis Jakarta Pusat (1000β2000 ton)
### π 3.5 Waste Decomposition (KLHK 2026)
Rasio dekomposisi dihitung dinamis dari dataset historis, fallback ke standar resmi:
- **Organik/Sisa Makanan**: `~49.87%`
- **Plastik**: `~22.95%`
- **Sisanya**: Kertas, logam, residu (tidak dihitung terpisah untuk optimasi logistik)
---
## 4. API Reference
### `POST /api/v1/predict`
**Deskripsi**: Generate prediksi volume sampah 1β30 hari ke depan untuk lokasi tertentu.
#### Request Body
```json
{
"hari_ke_depan": 7,
"prediksi_hujan_bmkg": 25.5,
"skala_keramaian": 0,
"nama_lokasi": "JIS",
"dari_tanggal": "06-01"
}
```
| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `hari_ke_depan` | `int` | β
| Durasi prediksi (1β30 hari) |
| `prediksi_hujan_bmkg` | `float` | β
| Estimasi curah hujan (mm). `0` = kering |
| `skala_keramaian` | `int` | β
| Skala event manual (0β5). `0` = normal |
| `nama_lokasi` | `string` | β
| Target lokasi: `JIS`, `GBK`, `Pasar Senen`, `Gang Sempit Tambora` |
| `dari_tanggal` | `string` | β | Tanggal mulai. Format: `YYYY-MM-DD`, `MM-DD`, atau `"1 Juni 2026"` |
#### Response Success (200)
```json
{
"status": "success",
"message": "π‘ WARNING di JIS: Volume di atas rata-rata.",
"confidence_score": 0.94,
"data": {
"prediction_results": [
{
"tanggal": "2026-06-02",
"lokasi": "JIS",
"total_volume_ton": 1245.50,
"sisa_makanan_ton": 621.15,
"plastik_ton": 285.84,
"rekomendasi_truk": 125,
"status_risiko": "WARNING π‘",
"info_event": "PRJ Opening @ JIExpo"
}
],
"logistics_plan": {
"trucks_needed": 872,
"manpower": 2616,
"estimated_duration_hours": 1743.2,
"efficiency_rate": "85% (Optimal)"
}
}
}
```
#### Error Responses
| Status Code | Response | Cause |
|-------------|----------|-------|
| `400` | `{"detail": "Format tanggal tidak valid..."}` | Input tanggal tidak dikenali parser |
| `500` | `{"detail": "Gagal memproses prediksi: ..."}` | Internal error / model crash |
| `503` | `{"detail": "Model/Dataset belum siap."}` | Service masih startup / model loading |
### `GET /`
**Deskripsi**: Health check & metadata sistem.
```json
{
"status": "Online",
"model": "Chronos-T5 Tiny",
"dataset_year": "2026",
"events_loaded": 15
}
```
---
## 5. Data Dictionary
### π `dataset_vibe_coder_2026.csv`
| Kolom | Tipe | Deskripsi |
|-------|------|-----------|
| `TANGGAL` | `YYYY-MM-DD` | Hari observasi |
| `RR` | `float` | Curah hujan (mm) |
| `Nama_Event` | `string` | Nama event (kosong jika tidak ada) |
| `Ada_Event` | `int` | Flag `1`/`0` |
| `Crowd_Scale` | `float` | Skala keramaian (0β5) |
| `Volume_Total_Ton` | `float` | Volume sampah baseline |
| `Vol_Sisa_Makanan_Ton` | `float` | Komponen organik |
| `Vol_Plastik_Ton` | `float` | Komponen plastik |
| `Hari_Ke` | `int` | Urutan hari (1β365) |
| `Is_Weekend` | `int` | `1` = Sabtu/Minggu |
| `ZONA` | `string` | Klasifikasi area: `Tourism`, `Residential`, `Commercial` |
### π `event_jakarta_2026.txt`
| Kolom | Tipe | Deskripsi |
|-------|------|-----------|
| `tanggal` | `YYYY-MM-DD` | Tanggal event |
| `nama_event` | `string` | Nama event |
| `lokasi` | `string` | Lokasi utama event |
| `skala_keramaian` | `int` | Skala 1β5 |
---
## 6. Deployment & Setup
### Hugging Face Spaces (Production)
1. Create Space β Template: `Blank` β Runtime: `Python`
2. Upload files:
```
π waste-prediction-api/
βββ app.py
βββ dataset_vibe_coder_2026.csv
βββ event_jakarta_2026.txt
βββ requirements.txt
βββ SYSTEM_ARCHITECTURE.md
```
3. Settings β Python 3.10, Hardware: `CPU`, Auto-rebuild: `ON`
4. Click **Factory rebuild** after each commit
### π» Local Development
```bash
git clone https://huggingface.co/spaces/ALAMDIENG/waste-prediction-api
cd waste-prediction-api
pip install -r requirements.txt
uvicorn app:app --host 0.0.0.0 --port 8001 --reload
```
Test:
```bash
curl -X POST http://localhost:8001/api/v1/predict \
-H "Content-Type: application/json" \
-d '{"hari_ke_depan":7,"dari_tanggal":"06-01","nama_lokasi":"JIS"}'
```
### π¦ `requirements.txt`
```txt
fastapi>=0.104.0
uvicorn>=0.24.0
pandas>=2.1.0
numpy>=1.26.0
torch>=2.1.0
chronos-forecasting>=0.1.0
pydantic>=2.5.0
```
---
## 7. Testing & Validation
### π§ͺ Unit Tests (Conceptual)
```python
def test_parse_flexible_date():
assert parse_flexible_date("06-01").date() == date(2026, 6, 1)
assert parse_flexible_date("1 Juni 2026").date() == date(2026, 6, 1)
def test_location_matching():
assert check_location_match("JIS", "JIExpo") == True
assert check_location_match("GBK", "JIExpo") == False
```
### Integration Scenarios (Postman)
| Scenario | Input | Expected |
|----------|-------|----------|
| Normal day | `dari_tanggal: "06-10", skala: 0` | `info_event: null`, volume ~1200 ton |
| Event match | `dari_tanggal: "06-01", lokasi: "JIS"` | `info_event: "PRJ..."`, +20β35% volume |
| Event no-match | `dari_tanggal: "06-01", lokasi: "GBK"` | `info_event: null`, volume normal |
| Heavy rain | `prediksi_hujan_bmkg: 50` | Multiplier +2β5% |
| Low accessibility | `lokasi: "Gang Sempit Tambora"` | Lower volume β WARNING/CRITICAL |
### π Performance Targets
- **Latency**: `< 3.0s` (p95) untuk forecast 7 hari
- **Throughput**: `10β20 req/min` (HF Spaces CPU tier)
- **Accuracy**: `Β±8β12%` MAE vs baseline historis (valid untuk perencanaan logistik)
---
## 8. Business Impact & Use Cases
### Operational Efficiency
| Metric | Before (Reactive) | After (Predictive) | Improvement |
|--------|-------------------|--------------------|-------------|
| Fleet dispatch | After complaint/report | H-1/H-2 scheduled | β¬οΈ 15β20% idle time |
| Fuel cost | Unplanned routes | Optimized zoning | οΈ 10β12% consumption |
| Manpower | Overtime-heavy | Shift-planned | β¬οΈ 8β10% overtime |
| Public health | Post-spill cleanup | Pre-emptive containment | β¬οΈ Risk mitigation |
### Primary Use Cases
1. **Dinas Lingkungan Hidup**: Penjadwalan armada harian berbasis risiko zonasi
2. **Event Organizer**: Kalkulasi kebutuhan TPS & truk sampah saat izin keramaian
3. **Fasilitas Pengelola Sampah**: Alokasi shift & kapasitas gudang 3 hari ke depan
4. **Dashboard Eksekutif**: Executive summary + visual heatmap volume per kecamatan
---
## 9. Roadmap & Scalability
### v2.1 (Next 3 Months)
- [ ] Real-time BMKG API integration (auto-fetch `prediksi_hujan_bmkg`)
- [ ] Batch prediction endpoint (`/api/v1/predict/multi`)
- [ ] Export to PDF/CSV + email webhook
- [ ] Rate limiting & API key auth
### ποΈ v3.0 (Architecture Upgrade)
- [ ] Microservices split: `forecast-service`, `event-service`, `logistics-service`
- [ ] GPU inference optimization (Chronos-base/mini)
- [ ] Automated retraining pipeline (GitHub Actions + HF Datasets)
- [ ] Prometheus/Grafana observability + alerting
### Long-term Vision
> *"Dari prediksi volume β optimasi rute real-time β circular economy tracking. Sistem ini menjadi tulang punggung smart city waste management yang data-driven, hemat biaya, dan berkelanjutan."*
---
## 10. Author & Support
**Developed by**:
**Faril Putra Pratama**
SMK Taruna Bangsa
π [GitHub: @FARILtau72](https://github.com/FARILtau72)
**License**: MIT
**Case Study**: Waste Volume Prediction System (CASE 2)
**Last Updated**: 2026-06-01
π© **Issues & Contributions**:
Gunakan GitHub Issues untuk bug report, feature request, atau dokumentasi improvement. PR welcome!
---
> π‘ **Presenter Note**:
> *"Sistem ini bukan sekadar forecast angka. Ia adalah decision engine: Chronos memberi baseline, cuaca memberi koreksi berat, event memberi konteks spasial, dan risk scoring memberi prioritas aksi. Hasilnya? Armada tidak lagi keliling butaβmereka datang ke tempat yang tepat, di waktu yang tepat, dengan kapasitas yang tepat."*
--- |