Implement translation + TTS caching (real code) - #15
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)@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).OrderedDict,TTS_CACHE_MAX=256) on(normalized_text, lang). Caches only successful audio (neverNone, 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).CACHE_LOG=1env-gates hit/miss lines.APP_VERSIONdefault →v5.1.0-cacheso field-test screenshots/logs identify the build (the version-stamp UI already exists in main).Verification (local, no network)
"Hi"and" Hi "→ one network call, not two.py_compileclean.Notes
speak()/translate(), so whichever merges second will need a small reconciliation — I'll handle it.Refs #10. Supersedes the brief-only #12.
🤖 Generated with Claude Code