Inspiration
Cats are masters at hiding pain. By the time most owners notice something is wrong, the problem is often advanced. Syahmi's cat Laila looked completely fine, but she had a problem in her digestive system, and by the time we knew she was hurting it was too late to catch it early. Vets have a tool for this, the Feline Grimace Scale, which reads pain from five facial cues: ears, eyes, muzzle, whiskers and head position. Almost no owner knows it exists.
We wanted every cat owner to have that vet's eye in their pocket, several times a day, without sending photos of their home to the cloud every time. That's where hybrid AI clicked for us, and why our team is called TwoBrain: an on-device brain for frequent, private work, and a cloud brain for the rare moments that matter.
What it does
Take a photo of your cat and PurrSense tells you:
- Who it is. It compares an on-device photo embedding with the embeddings of your registered cats. A cat it doesn't know is offered as a new one.
- Mood: positive, neutral or negative. We use three moods on purpose: the research we read found no scientific basis for finer mood labels from a single photo.
- A pain score out of 10 on the Feline Grimace Scale: five facial parts scored 0–2, with the total added up in code, not by the model. 4 or more is the published threshold for a vet to check again.
- Visible health signs: eye discharge, squinting, nasal discharge, an unkempt coat.
- A care note in plain words, and a weekly trend for each cat.
- A Deep Check: if the score is high or signs are visible, a second, focused cloud check takes a closer look, with its own prompt and a higher-resolution image. It looks for more signs and gives an urgency level.
- The TwoBrain trace: for every step, a badge says whether it ran On-device, in the Cloud or by a Fixed rule, with its time. You can see exactly where your data went.
PurrSense is a screening aid, not a diagnosis. It never names an illness or suggests medicine.
Plain rules around the AI
The part we are proudest of is what the app refuses to do. A model that guesses is worse than no answer, so plain, explainable code sits around the AI:
- One photo is not enough to say "pain". The same photo scored 2 on one run and 4 on another. So a first high score only asks for a second photo. Both must reach 4 for Possible pain; if they disagree the result is Not confirmed.
- No score for photos that can't be scored. The grimace scale needs an awake cat facing the camera. The app says "face turned", "looks asleep", "grooming or eating", "face partly covered", "too blurred" or "face not visible" instead of inventing a number. On iOS, Apple Vision checks the face on the phone first, so those photos are never uploaded. On the web, MediaPipe stops photos with no cat in the browser, and the model's own answer, checked by code, decides on the face. On Android the model's answer decides.
- A cat's usual face. Some cats always look sleepy. The owner can set a cat's usual face, and only what goes beyond it counts.
- Questions for the owner. When a cat looks unwell, a few yes/no questions (breathing, collapse, peeing, eating…) turn into one of four plain outcomes: go to a vet now, call your vet today, keep watching, or older photo. Fixed rules, so the same answers always give the same advice.
- A check on the words. Small on-device models write odd things. Ours once wrote a letter, invented illnesses and took a cat called Latte for a coffee. A care note is shown only if it passes our checks; otherwise the app writes one from the facts. "What to do next" is always added by code, never by the model.
How we built it
Firebase AI Logic hybrid inference is the core.
- iOS (SwiftUI) is the complete app. Apple's on-device Foundation Model is text-only, so we split the work: Apple Vision finds and crops the cat and makes the photo embedding on the phone; Gemini reads the face (only the cat crop is sent); and the care note and weekly trend run through a
HybridModel(primary: SystemLanguageModel, secondary: Gemini)session, so they use Apple Intelligence on the phone and fall back to Gemini in the cloud. Judges can't install it without TestFlight, so from source it opens in a no-setup sample mode. - Android (Kotlin, Jetpack Compose): one hybrid model with
OnDeviceConfig(mode = InferenceMode.PREFER_ON_DEVICE). Gemini Nano reads the photo on supported phones and the same call falls back to cloud Gemini elsewhere;response.inferenceSourcepowers the trace. Cat recognition uses an on-device MediaPipe image embedder. It runs the same plain rules as iOS (two photos, usual face, unscorable photos, owner questions, care-note check) in Kotlin, in the same design, and has a sample mode like iOS. It doesn't have iOS's on-phone Apple Vision face check, so there the model's answer decides whether a photo can be scored. Debug builds use the App Check debug provider and release builds use Play Integrity. - Web (React + Vite): MediaPipe runs in the browser: an EfficientDet-Lite0 detector checks there is a cat and crops it, and the same MobileNet-V3 embedder as Android makes the photo embedding, so the vectors match. Gemini in the cloud reads the face from the crop only. The care note and weekly trend use Firebase AI Logic with
PREFER_ON_DEVICE: Gemini Nano in Chrome writes them when the browser has it, and cloud Gemini otherwise, withinferenceSourcedriving the trace. The same plain rules are ported to TypeScript with unit tests. Adding?sampleto the URL opens a sample mode. - Escalation is a rule, not a vibe: the Deep Check runs when the pain score reaches the threshold \(\tau\) (set in Remote Config) or when visible signs are reported. The score is the five grimace parts minus the cat's usual face.
- App Check protects AI Logic, anonymous Authentication removes sign-up friction, and Cloud Firestore stores per-cat results with offline caching and per-user security rules. The photo itself is never stored in the cloud.
- Built with Claude Code, Android Studio and Xcode.
Pre-existing work and credits
All our own code was written during the submission period. Third-party work we included, not something we made:
- Bundled model:
mobilenet_v3_small.tflite, a MobileNet-V3 Small image embedder from Google's MediaPipe (Apache-2.0). We did not train it. It is used for cat recognition on Android and web. - Model loaded at run time (web): Google's MediaPipe EfficientDet-Lite0 object detector (Apache-2.0). We use its "cat" class. We did not train it.
- Libraries and services: Firebase SDKs, MediaPipe Tasks, React and Vite, Apple Vision and Foundation Models, Gemini through Firebase AI Logic.
- Sample photos: four CC0 / public-domain photos from Wikimedia Commons, used only in sample mode (credits). The cats and readings in sample mode are made up.
- Feline Grimace Scale: Evangelista et al., Scientific Reports, 2019.
Challenges we ran into
- Different on-device abilities per platform. Gemini Nano on Android can see images; Apple's model can't (yet). We redesigned the iOS pipeline around Apple Vision plus on-device text steps instead of forcing one architecture onto both.
- Our own safety margin backfired. Research says Gemini scores cat pain low, so we first added 2 points to every score. On Laila's photos, that ended 42% of two-photo scans as possible pain (19% at 0, 9% once her usual face was set). We removed the margin (it is now 0) and added the two-photo rule and the cat's usual face instead. The honest result: fewer alerts, but the model can still miss mild pain.
- Small models misbehave. On-device requests are capped around 4k tokens, and the model sometimes breaks the JSON format or writes nonsense. We added a lenient parser, an automatic retry and the note check.
- Free-tier quota. The Deep Check first used
gemini-3.8-flash, whose free tier allows only 20 requests a day. Testing used them all up, and the app showed "Deep Check was not available" and kept the first reading, as designed. We moved it togemini-3.5-flash-lite, which allows 500 requests a day (shared with the quick read). The model is a Remote Config value, so a larger one can be switched in without a release. - Responsible health claims. We framed PurrSense as screening, not diagnosis: score added up in code, "no score" when a photo can't be scored, and a clear "contact your vet" path.
- App Check and a public judging period. Firebase AI Logic requires App Check and our project enforces it, so each device needs a registered token or a real attestation provider for AI calls, and we don't publish debug tokens (anyone holding one can call our project). That's why every app also has a no-setup sample mode. Nobody on the team has seen Gemini Nano run on a device yet; where it isn't available, the trace honestly shows the cloud fallback.
Accomplishments that we're proud of
- A hybrid AI app where the plain rules around the model matter as much as the model
- Two very different on-device models (Gemini Nano and Apple Intelligence) designed behind one Firebase approach
- One product on three platforms: iOS, Android and the web, with the same rules in Swift, Kotlin and TypeScript
- Grounded in a published veterinary pain scale rather than an invented "sick score"
- The TwoBrain trace makes hybrid AI visible to non-technical users
- We say what the app can't do, including where it was tuned on very few photos (see the README's Known limitations)
What we learned
- The same step gives a different result on the phone than in the cloud. The small on-device model is not a smaller copy of the cloud one. It is capped at about 4k tokens in and 256 out, it sometimes breaks the JSON format, and it wrote letters, invented illnesses and took a cat called Latte for a coffee. Apple's on-device model can't read images at all, so the face reading always goes to the cloud on iOS. With hybrid inference one feature can come from either brain, so we couldn't treat them as interchangeable. That is why the TwoBrain trace says which one ran, and why a care note is checked before it is shown.
- Every model mistake changed our design, and the fix was a rule, not a better prompt.
- The +2 safety margin ended 42% of Laila's two-photo scans as possible pain. The offset is now 0 and the cat's usual face is a rule.
- One photo scoring 2 and then 4. "Possible pain" now needs two different photos that both reach 4.
- The Latte-as-coffee note. A care note is now shown only if it passes a check; otherwise the app writes one from the facts.
- A health tool needs a scored dataset. We have no photos scored by vets, and trying the app on two real cats is not a validation. The research agrees: the headline 95.5% accuracy for automated grimace scoring was tested on 100 images where a cat could appear in both training and test data, and studies on unseen cats get 66–78%. Next time we would start from a vet-scored, properly licensed dataset, not from prompting a general model.
- Building one product on three platforms was challenging, and rewarding. Keeping the rules the same in Swift, Kotlin and TypeScript meant writing them down plainly, which also made them easier to explain here.
What's next for PurrSense
- Replace Gemini as the pain scorer with small on-device models trained for cat faces, which needs a vet-scored photo dataset with a clear licence
- Check the scores against vets on owner-taken photos
- Better recognition for look-alike cats, with a tuned match threshold
- Live camera scanning and daily reminders
- Vet-shareable reports of grimace trends
- A pilot with a local shelter or vet clinic
Try it: docs/TESTING.md has the steps. Code: github.com/msyxhmx/PurrSense (Apache-2.0).
Built With
- apple-foundation-models
- apple-intelligence
- apple-vision
- chrome-built-in-ai
- claude-code
- cloud-firestore
- firebase
- firebase-ai-logic
- firebase-app-check
- firebase-authentication
- firebase-hosting
- firebase-remote-config
- gemini
- gemini-nano
- jetpack-compose
- kotlin
- mediapipe
- play-integrity
- react
- recaptcha-enterprise
- swift
- swiftui
- tensorflow-lite
- typescript
- vite