Serving a route-discovery model to two Android clients
Team lead · backend, both Android clients, ML co-development · 2025–2026
An ML-powered transit platform for Cairo's informal microbus network: a FastAPI backend, a 13-table PostgreSQL schema, and the serving layer feeding two Android clients.
Training data
Trained on Beijing taxi GPS — Microsoft T-Drive, 10,357 trips from 2008–2009 — plus about 500 synthetic Cairo segments generated with the Google Maps Directions API. No real Cairo microbus GPS was used.
- Route detection F1
- 0.963
- Hard split; precision 0.929, recall 1.000. Measured on synthetic data built along the same corridors, so treat it as an upper bound.
- ETA mean absolute error
- 3.28 min
- 420 synthetic trips, single split. Five-fold cross-validation gives 3.50 ± 0.35 min. No naive baseline; the project's target was ≤ 3.0 min.
- Backend test suite
- 42 / 42
- Integration tests only. No load testing, and no tests of the ML pipeline itself.
The problem
Microbuses carry most of Cairo's daily trips, with no published routes, timetables or stops. You learn the network from hand signals at the roadside, or by stopping a bus and asking where it's going. A system that serves this network has to infer it first.
What I built
Mobile clients
Backend
Machine learning
Six components across three layers. Select one to see what it does and what it connects to.
- Driver app
- Streams latitude, longitude, speed and bearing every five seconds during a trip, from an Android foreground service. Every trace the backend clusters starts here. Connects to: FastAPI.
- Commuter app
- Polls a driver's position every five seconds while a commuter is tracking, and shows the predicted arrival time as a countdown. Pickup alerts fire when the bus is within 200 metres. Connects to: FastAPI.
- FastAPI
- The API both apps talk to: six routers, JWT authentication, and Firebase Cloud Messaging for trip events. It runs the ML models at request time. Connects to: Driver app, Commuter app.
- PostgreSQL
- Thirteen tables. Each trip stores its full GPS trace and a quality score; the points ledger records every earning event with the balance after it, so any driver's total can be explained. Connects to: DBSCAN, Linear regression.
- DBSCAN
- Groups GPS trajectories into routes without being told how many exist, using Hausdorff distance between trajectories and treating GPS drift as noise rather than forcing it into a route. Connects to: PostgreSQL.
- Linear regression
- Predicts arrival time from 23 features — distance, time of day, route history and trajectory shape. Chosen over tree-based models for fast inference and a small model. Connects to: PostgreSQL.
The FastAPI backend, a 13-table PostgreSQL schema, and the layer that serves ML predictions to both Android apps.
A ledger, not a balance. Points convert to money, so instead of a running total I stored every earning event with the balance after it. Any driver's total can be explained.
Two days lost to a 401.
- API models changed
- Database not migrated
- Registration fails
- No driver, so no login token
- Every request returns 401where it showed
I changed the API models without migrating the database. Registration failed, so login had nothing to authenticate. The bug was three steps upstream of where it showed.
Results
| Metric | Result | Measured on |
|---|---|---|
| Route detection F1 | 0.963 | Hard split. Precision 0.929, recall 1.000 |
| ETA mean absolute error | 3.28 min | 420 synthetic trips, 336 train / 84 test |
| Five-fold cross-validated MAE | 3.50 ± 0.35 min | Same data, all folds |
| Backend integration tests | 42 / 42 | Auth, profile, favourites, history, FCM, logout |
Recall of 1.000 reflects the data, not the model. The cross-validated error is higher than the 3.28 we reported, and both miss the ≤ 3.0 target.
The hard decision
Models trained on Beijing taxi data didn't transfer to Cairo, and we couldn't afford to collect real microbus traces. So we generated Cairo data through the Google Maps Directions API.
A routing engine gives the path a bus should take, not the one it does. That's why every number on this page is an upper bound.
What I'd do differently
- Ask Cairo's transport authority for real data first
- Test on real Cairo data the models had never seen
- Compare against a simple distance ÷ speed baseline
- Report the cross-validated number as the headline