Predictive Mobile Agent #3: Installing the Brain — Bringing Your On-Device AI to Life in Flutter
Article Summary
Amorn Apichattanakul from KBTG reveals the hidden trap that breaks on-device AI: your Dart preprocessing code must match your Python training code bit-for-bit, or your 15KB model becomes useless.
This is part 3 of a series on building predictive mobile agents. After training a multi-head neural network in Python to predict payment behavior (should suggest? which shop? how much?), the author tackles the critical challenge of deploying it in Flutter without breaking the model's assumptions.
Key Takeaways
- 15.70 KB TFLite model runs inference in 1-2ms on-device with zero network calls
- Weekday index mismatch between Python and Dart silently breaks Friday predictions
- TFLite output tensor order differs from Keras, requiring manual verification during export
- Mock mode enables full testing without device permissions or real sensors
- Android wraps WiFi SSIDs in quotes, breaking trust score lookups if not stripped
The article demonstrates that 90% of on-device AI effort should go into preprocessing contract correctness, not model architecture, with detailed Dart ports of Python encoding functions.
About This Article
Amorn Apichattanakul found that Python's datetime.weekday() returns 0-6 for Monday through Sunday, while Dart's weekday returns 1-7. When the model received index 0 instead of 6 for Sunday, predictions failed silently. Friday family dinner predictions got worse, but no error messages appeared.
The team ported Python's encoding functions to Dart line by line. They applied the correction (dt.weekday - 1) % 7 to fix the weekday index mismatch. They also implemented trigonometric time encoding using dart:math for sin and cos operations to match Python's output exactly.
The unified preprocessing contract let the 15.70 KB model run inference in 1-2 ms on-device. Prediction accuracy stayed consistent across mock and live sensor modes. Six test scenarios verified the fix, including Rule 1 for morning coffee and Rule 7 for WFH guard patterns.