Skip to content

Implement translation + TTS caching (real code) - #15

Merged
trinitron88 merged 2 commits into
mainfrom
feature/cache-translations-tts
Jun 15, 2026
Merged

Implement translation + TTS caching (real code)#15
trinitron88 merged 2 commits into
mainfrom
feature/cache-translations-tts

Conversation

@trinitron88

Copy link
Copy Markdown
Owner

Implements CODEx_TASK_CACHE.md. The earlier PR #12 with the same name merged only the task brief — no caching code shipped. This is the actual implementation.

Why

Conversations repeat phrases ("yes", "okay", "how are you"). Each repeat currently re-hits Google Translate and regenerates TTS audio. Caching both cuts latency, network calls, and phone battery.

What changed (hf_space/app.py)

  • Translation@functools.lru_cache(512) on a whitespace-normalized (text, sl, tl) key. The cached function raises on failure, so a failed request isn't cached (the wrapper catches it and falls back to the original text without poisoning the cache).
  • TTS — bounded LRU (OrderedDict, TTS_CACHE_MAX=256) on (normalized_text, lang). Caches only successful audio (never None, so failures / Chin-text-only keep retrying), and stores/returns copies so a consumer can't mutate the cached PCM (the gotcha the brief flagged).
  • LoggingCACHE_LOG=1 env-gates hit/miss lines.
  • Version — bumped APP_VERSION default → v5.1.0-cache so field-test screenshots/logs identify the build (the version-stamp UI already exists in main).
  • README documents the new env vars.

Verification (local, no network)

  • Whitespace-normalized keys dedupe: "Hi" and " Hi " → one network call, not two.
  • Copy-safety: mutating a returned TTS array does not corrupt the cached copy.
  • py_compile clean.

Notes

  • Independent of the Piper PR (Implement Piper TTS for realtime English (gTTS fallback) #14) by design (you asked for its own branch). Both touch speak()/translate(), so whichever merges second will need a small reconciliation — I'll handle it.
  • Safe on Spaces: pure in-process LRUs, nothing persisted to disk.

Refs #10. Supersedes the brief-only #12.

🤖 Generated with Claude Code

trinitron88 and others added 2 commits June 14, 2026 21:16
PR #12 merged only the task brief; this implements it. Repeated phrases (common
in conversation) otherwise re-hit Google Translate and regenerate TTS every time.

- translate(): @lru_cache(512) on a whitespace-normalized (text, sl, tl) key.
  The cached fn raises on failure so failures aren't cached (wrapper falls back
  to the original text without poisoning the cache).
- speak(): bounded LRU (OrderedDict, TTS_CACHE_MAX=256) on (normalized_text,
  lang). Caches only successful audio (never None), and stores/returns COPIES so
  a consumer can't mutate the cached PCM. _synthesize() holds the gTTS logic.
- CACHE_LOG=1 env-gates hit/miss logging.
- Bump APP_VERSION default → v5.1.0-cache so field-test screenshots/logs identify
  the build (the version stamp UI already exists).
- README: document caching + CACHE_LOG/TTS_CACHE_MAX.

Verified locally: whitespace-normalized keys dedupe network calls, and mutating
a returned TTS array does not corrupt the cached copy.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The version stamp should equal the deployed code, so make source the single
source of truth — drop os.environ.get so a stray Space variable can never pin
the 'Build:' stamp. Bump the constant when shipping a change.

Side effect: this file change means the next deploy is a real commit (not a
'no files modified' no-op), so it will actually rebuild/restart the Space.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@trinitron88
trinitron88 merged commit 414c4bf into main Jun 15, 2026
@trinitron88
trinitron88 deleted the feature/cache-translations-tts branch June 15, 2026 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant