Inspiration
"By the time we walked out to the car park, half of what the doctor said had already blurred together—and two weeks later, the NHS clinic letter arrived in a language none of us could decode."
If you have ever helped an ageing parent or family member navigate an NHS appointment, you know the quiet anxiety that starts the moment the consultation room door closes.
A loved one sits through a busy ten-minute appointment where a clinician explains a new diagnosis, adjusts a blood-pressure tablet, starts a blood thinner, and mentions in passing which over-the-counter painkillers to avoid. Weeks later, a dense NHS outpatient clinic letter lands on the doormat—packed with abbreviations like paroxysmal AF, CHA₂DS₂-VASc, BD, MR, and repeat U&Es. Trying to connect what was said in the room, what the clinic letter says in medical shorthand, and what is sitting in three different boxes from the pharmacy becomes exhausting—and genuinely risky. One missed warning about taking Ibuprofen alongside a blood thinner, or accidentally taking a second evening dose, can send someone straight back to A&E.
At the same time, clinicians cannot simply drop raw patient audio or identifiable records into consumer wearable clouds or unguarded cloud LLMs. Healthcare needs on-device privacy where personal identifiers live, paired with cloud intelligence where complex clinical synthesis helps most—and deterministic offline safety when you are standing in a basement pharmacy with no mobile signal.
That gap is why we built Peonies: a connected care loop across Web (Chrome PWA), Android, and Wear OS powered by Firebase AI Logic hybrid inference.
What it does
Peonies bridges the clinician's desk and the patient's pocket and wrist so nothing important is lost between the appointment, the clinic letter, and the pharmacy counter:
- 1-Click Clinician-to-Device Pairing & Encrypted Capture: In production, clinician access is strictly restricted to verified
@nhs.netaccounts (for this hackathon demo and judging, we also enabled@google.com,@devpost.com, and@chatpye.com). A clinician adds or selects a patient and sends a live pairing request straight to the patient's phone or Wear OS watch. The patient accepts in one tap, drops wrist bookmarks on key dosage instructions, and syncs consultation audio encrypted locally with AES-256-GCM—or clinicians can upload external recorder clips safely. - On-Device HIPAA Safe Harbor Privacy Firewall: Before a single word of transcript leaves the device, Peonies runs a fail-closed two-stage privacy firewall—combining deterministic HIPAA 18-identifier + UK NHS number checks with on-device Gemini Nano via Firebase AI Logic to catch remaining contextual names, relatives, and addresses locally.
- Evidence-Linked Clinical Notes & Verified Handoff: Only the de-identified transcript goes to Gemini 3.8 Flash in the cloud to draft structured SOAP notes, extract active medications (while excluding prohibited drugs like "do not take Ibuprofen"), and anchor every claim to verbatim quotes. Once the clinician reviews and confirms, Peonies unlocks FHIR R4 export and pushes the verified care plan live to the patient.
- Plain-Language Care Plan & NHS Clinic Letter Simplifier: The patient receives their clinician-verified summary immediately. When a jargon-heavy NHS clinic letter arrives in the post, the family can paste it in—and Firebase AI Logic hybrid inference prefers on-device Gemini Nano (seamlessly falling back to Gemini 3.5 Flash-Lite in the cloud when needed) to turn medical shorthand into plain language across 7 languages with a checklist of follow-up questions for the next visit.
- Offline UK Pharmacy Safety Shield: At the pharmacy counter—even with zero mobile signal—patients can scan a GS1 DataMatrix 2D medicine box barcode or dispensing label to verify prescription pickups (matching UK brands like Tritace to generic Ramipril, catching strength or modified-release mismatches) and check over-the-counter remedies (instantly flashing a red bleeding-risk warning if someone on Apixaban checks Nurofen / Ibuprofen).
- Wear OS Haptics, Anti-Double-Dosing & Instant 999 Guardrail: Confirmed prescriptions sync to the patient's medication schedule and Wear OS watch with haptic wrist reminders, pill stock counts, an anti-double-dose cooldown guard, and an offline Emergency ICE card flagging high-risk anticoagulants and allergies. At home, grounded Voice Q&A speaks natural answers strictly from the verified care plan—while a zero-latency deterministic NHS 999 / 111 safety guard intercepts emergency symptoms like sudden chest pain or shortness of breath immediately.
How we built it
Peonies is built as a connected multi-platform care experience across Web (Chrome PWA), Android, and Wear OS, powered by Firebase AI Logic hybrid inference and a zero-trust Firebase & Google Cloud backend in London (europe-west2).
1. Three-Tier Hybrid AI with Firebase AI Logic
Rather than using one model everywhere, we route each clinical task to the right execution tier for privacy, speed, and reliability:
- Strictly On-Device (
Gemini Nano): Runs Stage 2 of our HIPAA privacy firewall locally so personal identifiers never leave the user's device, even if offline. - On-Device First, Cloud Fallback (
Gemini Nano→Gemini 3.5 Flash-Lite): Simplifies NHS clinic letters, translates care plans into 7 languages, and powers grounded patient voice Q&A—fast and private on-device when Gemini Nano is available, with seamless cloud fallback on any other device. - Cloud-Only After De-Identification (
Gemini 3.8 Flash&Gemini 3.8 Flash TTS): Transcribes consultation audio, drafts evidence-linked SOAP notes and medication plans from scrubbed text, and streams natural spoken guidance.
2. Deterministic Clinical Safety & NHS Number Validation
Generative AI handles comprehension and summarization, while deterministic safety rules own high-stakes clinical boundaries offline—including GS1 DataMatrix pharmacy verification, OTC bleeding-interaction alerts, anti-double-dose cooldowns, and instant 999 / 111 emergency interception.
Before Gemini Nano inspects a transcript, our Stage 1 local privacy firewall validates candidate 10-digit UK NHS numbers using the official Modulus-11 check-digit algorithm so real NHS numbers are always redacted without accidentally scrubbing dosages or blood pressure readings:
$$r = \left(\sum_{i=1}^{9} (11 - i)\, d_i\right) \bmod 11, \qquad c = \begin{cases} 0 & \text{if } 11 - r = 11 \ 11 - r & \text{otherwise} \end{cases}$$
A 10-digit sequence d₁ d₂ … d₁₀ is redacted as a valid NHS Number only when c ≠ 10 and d₁₀ = c.
3. Zero-Trust Firebase & Multi-Platform Stack
- Firebase Authentication & Role-Based Access: Mandatory Google or email sign-in with anonymous access disabled. In production, clinician access is strictly restricted to verified
@nhs.netaccounts (for this hackathon demo and judging, we also enabled@google.com,@devpost.com, and@chatpye.com). Patients sign in with any Google or email account and can only view their own confirmed care plans. - Cloud Firestore & Encrypted Cloud Storage: Real-time 1-click pairing and care-plan sync with strict clinician-to-patient isolation, append-only compliance audit logs containing zero PHI, and client-side AES-256-GCM encrypted audio storage.
- Firebase App Check & Google Cloud Run: Protected with App Check and hosted as a Next.js 15 PWA on Google Cloud Run (
europe-west2), paired with native Kotlin & Jetpack Compose Android and Wear OS apps. - Built with Antigravity & Firebase Agent Skills: Engineered in Antigravity using official Firebase Agent Skills for Firebase AI Logic, Authentication, and Firestore Security Rules.
Challenges we ran into
- Firestore Security Rules Are Not Filters: During live pairing tests between the Clinician Workstation and Patient Companion, checking the session code length inside a combined read rule worked for single-document lookups, but silently rejected the patient's real-time incoming pairing query. Separating single-document reads from user-scoped collection queries resolved it cleanly while preserving strict isolation.
- Never Letting Raw PHI Leak on Fallback: Standard hybrid inference falls back to the cloud when Gemini Nano is unavailable—ideal for simplifying a clinic letter, but unacceptable for scrubbing raw patient names and NHS numbers. Designing a three-tier routing policy (fail-closed on-device for PHI scrubbing, hybrid on-device-first for patient comprehension, and cloud-only after verified de-identification) ensured privacy never degrades silently.
- Preventing Contraindicated Drugs from Becoming Prescriptions: Early extraction tests picked up "please do not take Ibuprofen" as a prescribed drug. We built negation-aware clinical extraction alongside our GS1 DataMatrix pharmacy engine so prohibited drugs stay out of the medication schedule while actively powering OTC interaction alerts.
- Monorepo Next.js 15 Deployment on Cloud Run: Next.js standalone builds initially omitted shared workspace packages inside Docker. Refining our multi-stage container build and Cloud Build pipeline produced clean, reproducible deployments in
europe-west2.
Accomplishments that we're proud of
- True End-to-End Multi-Device Sync: Watching a clinician add a patient on desktop, click Pair device, see the live prompt land on the patient's companion in under a second, scrub 7 PHI identifiers locally, confirm medications, and immediately activate offline pharmacy safety alerts and Wear OS ICE cards feels genuinely magical.
- Real Clinical Safety Without Cloud Latency: Every safety-critical patient check—UK NHS Mod-11 PHI validation, GS1 DataMatrix pharmacy verification, OTC anticoagulant/NSAID bleeding alerts, anti-double-dosing cooldowns, and the 999 / 111 red-flag interceptor—works deterministically offline with 15/15 passing automated contract tests.
- Calm, Zero-Clutter Healthcare UX: Healthcare tools are often intimidating walls of dense forms. We focused relentlessly on serene typography, icon-first navigation, and short plain-English copy so patients and families feel supported rather than overwhelmed.
What we learned
- Hybrid AI is an architectural mindset, not just a cost optimization: Choosing between strictly on-device, hybrid on-device-first, and cloud-only per clinical task lets one unified SDK (Firebase AI Logic) simultaneously satisfy strict medical privacy boundaries, offline accessibility, and deep multimodal synthesis.
- Deterministic guardrails and generative AI belong together: LLMs excel at turning messy spoken conversations and NHS letters into human clarity, while deterministic rules must own high-stakes clinical boundaries like Mod-11 identifiers, drug-class bleeding interactions, double-dose cooldowns, and 999 emergency escalation.
What's next for Peonies
- NHS GP Connect & Spine FHIR R4 Integration: Connecting our confirmed-only FHIR R4 export directly to NHS clinical systems so GPs never double-enter notes.
- Family Caregiver Circle: Opt-in, patient-controlled read-only notifications so an adult child or carer can see simplified care updates and refill reminders without compromising clinical confidentiality.
- iOS + Apple Foundation Models via Firebase AI Logic: Extending our shared hybrid inference architecture to iPhone and Apple Watch alongside Chrome Web, Android, and Wear OS.
- Live Camera OCR + Pharmacy EPS Integration: Expanding our offline GS1 DataMatrix scanner with on-device camera label OCR and direct repeat-prescription handoffs.