Skip to content

feat: on-device captions (whisper.rn) with GPU accel + model picker - #14

Merged
morepriyam merged 6 commits into
mainfrom
feat/on-device-captions
Jun 14, 2026
Merged

morepriyam merged 6 commits into
mainfrom
feat/on-device-captions

Conversation

@morepriyam

@morepriyam morepriyam commented Jun 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds an app-wide, on-device speech-to-text engine that keeps every clip in the library captioned, entirely on-device (no network beyond the one-time model download). The feature is surfaced through an On-device AI panel — a privacy-forward home-header entry point (sparkles + "AI") structured so future on-device features slot in alongside Captions. Users pick a Whisper model; the engine downloads it, transcribes the whole library in the background, and renders read-only captions in the in-recorder preview.

All work runs in a single global background engine mounted once at the root — it picks up new/imported/edited clips automatically and yields to the camera while recording so inference never competes with capture.

What's included

Engine — src/features/transcription/

  • Single global engine (TranscriptionProvider → useLibraryTranscription) mounted above all screens. Driven by the persisted model selection it: downloads the chosen model (keeping only one set of weights on disk), transcribes everything that needs it, and resumes work stranded by a killed app.
  • whisper.ts — one reusable Whisper context. Runs with GPU/Metal + Flash Attention (falls back to CPU if the GPU can't init), decodes in the model's language, and emits caption-sized lines (maxLen + token timestamps) instead of whole paragraphs.
  • model.ts — on-demand model download with progress, completeness floor, shared in-flight downloads, single-model-on-disk enforcement, and cancellation of stale downloads on model switch.
  • models.ts — catalog (Tiny/Base/Small English-only + Large Turbo multilingual) with an explicit per-model decode language.
  • active-draft.ts / recording-signal.ts — lightweight module signals: caption the on-screen draft first, and yield while recording.

Persistence — src/db/

  • New transcripts table: one row per segment, keyed by the effective file so a destructive edit invalidates and re-runs the transcript. Stores status (processing/done/error), language, text, and per-line timing JSON.
  • New settings KV table (holds the selected model).
  • Migrations 0002 + 0003 (verified with drizzle-kit check).

UI

  • On-device AI panel (model-switcher-modal.tsx) — reframed from a captions-only picker into a generalized "On-device AI" modal with a privacy-forward header and a Captions section (future on-device features slot in as additional sections). Pick/switch/delete a model, live download + transcription progress, and a confirm prompt before large (≥300 MB) downloads (cellular/data warning).
  • Home-header entry point — a standalone top-right sparkles + "AI" header action (its own slot, centered with the title), accent-tinted when a model is active so it reflects whether on-device AI is live.
  • Captions in the recorder preview (preview-modal.tsx) — the active line synced to playback, plus "Transcribing…"/"unavailable" states.

Correctness & robustness

  • Multilingual model no longer forced to English — lang is en for .en models, auto for Large Turbo.
  • Bounded per-clip error retry (2 attempts/session, reset on relaunch) — transient failures recover instead of being permanently stranded.
  • Lost-wakeup race closed — deferred work can't be stranded if dirty is set during engine teardown.
  • Stale downloads cancelled on switch — no more finishing a 574 MB pull just to delete it.

Performance

  • GPU/Metal + Flash Attention (several × faster, far lighter on battery than CPU).
  • On-screen draft captioned first, so visible captions appear quickly.

Tooling / hygiene

  • Pin whisper.rn to 0.6.0 (matches the repo's exact-pin convention); lockfile synced.
  • Dev seed controls excluded from the production bundle — the __DEV__ home-header seed buttons (+ seed / s2 / s3 / clear) moved into their own component loaded via a __DEV__-guarded require, so Metro's dead-code elimination drops both the component and @/dev/seed (with its perf-test fixtures) from the release bundle. Verified against a production iOS export: the dev string literals are absent from the Hermes bytecode while prod UI strings remain.
  • New jest runner with zero new dependencies (babel-jest + @jest/globals, @/ alias mapped). 17 unit tests covering the timeline math, the transcription decision/retry logic, and the model catalog (incl. a regression test locking in the language fix). npm test.

Verification

  • ✅ tsc --noEmit
  • ✅ expo lint
  • ✅ jest — 17/17
  • ✅ drizzle-kit check
  • ✅ Production iOS export — dev seed strings confirmed stripped from the Hermes bytecode
  • ✅ Tested end-to-end on a real device — pick model → download → record → captions render and stay in sync. GPU/Metal path confirmed initializing (not the CPU fallback), and caption quality/sync verified at maxLen: 42.

Deliberately out of scope (follow-up)

  • Burned-in captions in the exported video — the real payoff, but a genuine native feature: the native merge exposes no subtitle path, and burn-in defeats the stream-copy fast path (forces a re-encode). Planned separately.
  • Charging/idle gate for full-library transcription — would need expo-battery.

Add an app-wide, background speech-to-text engine that keeps every clip
in the library captioned on-device, with a model picker and read-only
caption rendering in the recorder preview.

Engine (src/features/transcription/)
- Single global engine mounted once at the root (TranscriptionProvider).
  Driven by the persisted model selection, it downloads the chosen model
  (keeping only one set of weights on disk), transcribes the whole
  library, picks up new/edited clips automatically, and yields to the
  camera while recording.
- Persistence: new `transcripts` (one row per segment, keyed by the
  effective file so a destructive edit re-runs) and `settings` (KV)
  tables; resume of work stranded by a killed app.
- Whisper runs with GPU/Metal + Flash Attention (CPU fallback), decodes
  in the model's language (en for the .en models, auto for multilingual),
  and emits caption-sized lines (maxLen + token timestamps).
- The on-screen draft is captioned first (active-draft signal).
- Bounded per-clip error retry; in-flight downloads cancel on switch;
  large downloads prompt for confirmation.

Tooling
- Pin whisper.rn to 0.6.0 (matches repo convention).
- Add a jest runner (no new deps: babel-jest + @jest/globals) with unit
  tests for the timeline math, the transcription decision/retry logic,
  and the model catalog.
- Swap the home-header icon from captions.bubble to sparkles (accent
  tint when a model is active), so it reads as on-device AI rather than
  captions-specific.
- Reframe the modal as "On-device AI": a privacy-forward header and a
  "Captions" section, structured so future on-device features slot in as
  additional sections. Generalize the remove action copy.
Move the __DEV__ home-header seed buttons (+ seed / s2 / s3 / clear) into
their own component, loaded via a __DEV__-guarded require. Metro's
dead-code elimination then drops both the component and @/dev/seed (with
its perf-test fixtures) from the release bundle — previously the static
import bundled the seed module in production even though the buttons only
rendered in dev.

Verified against a production iOS export: the dev string literals are
absent from the Hermes bytecode, while prod UI strings remain.
The sparkle button shared the headerRight flex container with the dev
seed row and was bottom-aligned, so it read as part of the dev cluster.
Make it a standalone top-right header action centered with the title,
and move the dev-only seed row onto its own line below the header.
Pair the sparkles icon with an "AI" text label (both accent-tinted when a
model is active) so the entry point is self-explanatory.
@morepriyam
morepriyam merged commit b380dbf into main Jun 14, 2026
1 check passed
@morepriyam
morepriyam deleted the feat/on-device-captions branch June 14, 2026 03:27
morepriyam added a commit that referenced this pull request Aug 12, 2026
feat: on-device captions (whisper.rn) with GPU accel + model picker
morepriyam added a commit that referenced this pull request Aug 12, 2026
…advisories

Bumps transitive dependency resolutions already permitted by existing
semver ranges declared by Expo's own packages and devDependencies:

- js-yaml 3.15.0 -> 3.15.1 (babel-plugin-istanbul, dev-only)
- js-yaml 4.3.0 -> 4.3.1 (@expo/xcpretty, eslint's @eslint/eslintrc)
- brace-expansion 5.0.7 -> 5.0.9 (minimatch used by @expo/cli,
  @expo/config, @expo/config-plugins, @expo/fingerprint,
  @expo/metro-config)
- brace-expansion 1.1.16 -> 1.1.18 (eslint's minimatch@3)

No package.json ranges were changed and no Expo/framework versions
were bumped or overridden - these are patch-level fixes already
allowed by the existing dependency graph.

Closes dependabot alerts #14, #13, #11, #8.

Note: the following dependabot alerts remain open and are not
fixable via a lockfile refresh:
- #17, #16 (image-size DoS): no patched version exists upstream;
  image-size is archived and pulled in transitively via metro, even
  on the latest metro/expo releases. Needs an upstream fix in Metro.
- #2 (uuid missing bounds check): the xcode package (used by
  @expo/config-plugins) pins uuid@^7.0.3 and has not bumped it in
  its latest release. Not practically exploitable here since xcode
  only calls uuid.v4() with no output buffer (the affected code path
  is v3()/v5()/v6() with a caller-supplied buffer).
- #1 (esbuild dev server): comes from drizzle-kit's deprecated
  @esbuild-kit/esm-loader dependency, unrelated to Expo/Metro's dev
  server (already patched). Dev-only, low real-world risk since
  @esbuild-kit only uses esbuild's transform API, not serve().
@morepriyam morepriyam self-assigned this Aug 14, 2026
@github-project-automation github-project-automation Bot moved this from Ready to Done in Scrum Team Jerry Aug 14, 2026
morepriyam added a commit that referenced this pull request Oct 14, 2026
Whole-frame trims and whole-ms clip lengths on both platforms (#12), and
iOS selective merges that FFmpeg reads without holes (#14).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant