- Problem: a guest tells a restaurant what they can't eat in their own words ("doctor says no ocean stuff for me", "saya tak boleh sentuh benda bersusu", 我老婆碰到带壳的海产就不行). Menu rules miss phrasing like that, and then recommend the laksa. On two blind tests the rules alone missed 21.5% and 26.3% of such messages.
- Who: guests with allergies, diets (vegetarian, vegan, halal), meats they don't eat or no alcohol, at Malaysian restaurants and hawker stalls, typing in English, Malay, Manglish or Chinese; and the restaurant, whose assistant, kiosk and host stand answer them.
- What it does:
- one guest message in, what they avoid out (14 allergens, 3 diets, 5 meats, no alcohol);
- read by Gemini Nano on the device when Chrome has it, by the cloud model through Firebase AI Logic when it doesn't, with the page showing where it ran and how long it took;
- the reading is only ever added to the rules' own reading, never put in its place, so it can make an answer more careful but never less safe;
- the model's reading is kept only when the message says something is avoided (a small model lists every food a message names; this check stops it);
- on the restaurant's kiosk the whole answer is made on the kiosk: the allergy never leaves it, and answers keep coming when the Wi-Fi drops (Gemini Nano needs no App Check and no network);
- when the model can't answer (no network, over quota, too slow), the rules answer alone, and say so.
- How Firebase AI Logic is used (
src/ui/hybrid.ts):getAI+getGenerativeModelwithInferenceMode.PREFER_ON_DEVICE(alsoONLY_ON_DEVICEandONLY_IN_CLOUDfor the side-by-side test);initializeDeviceModelon the guest's tap (download progress shown);- structured output on both sides:
responseJsonSchemain the cloud,responseConstrainton the device; inferenceSourceshown to the guest ("read on this device" / "in the cloud");- cloud model Gemini 3.5 Flash-Lite at minimal thinking, chosen against 3.8 Flash and 4 Hydrogen for speed (median 0.87 s), cost and the free tier;
- App Check with reCAPTCHA Enterprise (Google Cloud Fraud Defense), for the cloud model only;
- the free quota handled: a 429's retry time pauses the cloud, and the device carries on.
- Evidence (blind tests by authors who never saw the app; the bar written before any code):
First blind test, a1 (403 messages), the reader without the output check:
| | Missed (of 247) | False alarms (of 156) | |---|---|---| | Rules alone | 53 (21.5%) | 38 (24.4%) | | Rules + cloud reader | 0 (every language) | 42 (26.9%) | | Rules + Gemini Nano on device | 2 (0.8%) | 69 (44.2%) |
Second blind test, a2 (386 messages), after the rules' fix and with the output check:
| | Missed (of 240) | False alarms (of 146) | |---|---|---| | Rules alone | 63 (26.3%) | 8 (5.5%) | | Rules + Gemini Nano on device | 20 (8.3%) | 8 (5.5%), none added by the model |
- Said plainly: the false-alarm bar (5%) was missed on a2 by one message, all the rules' own. The
check stops Gemini Nano's over-reading but costs some catches (0.8% missed on a1 without it, 8.3% on
a2 with it). Readings took a median 4.7 s on a1 and 8.5 s on the a2 run.
- Challenges:
- App Check with Fraud Defense: the key ID, not the secret, goes into Firebase;
- one time budget for App Check, Firebase loading and the model, so a guest never waits on a model;
- Google's free tier: 20 requests a day for 3.8 Flash, so Flash-Lite, and pacing that respects quotas;
- a small on-device model lists every food it sees: rewording didn't fix it, a check on its output did;
- measuring honestly: blind sets scored once, failures included.
- Next: the check's words, so fewer real readings are dropped; the cloud reader on a2; Android (Gemini Nano 4) for the restaurant's Sunmi POS app, which already wraps this web app.
Built With
- app-check
- chrome
- cloudflare-d1
- cloudflare-workers
- firebase
- firebase-ai-logic
- gemini
- gemini-nano
- playwright
- react
- recaptcha-enterprise
- typescript