Goal
Reduce latency, network calls, and phone battery drain in hf_space/app.py by caching repeated translation and speech outputs.
Context
The realtime app currently calls Google Translate for every utterance in translate() and uses gTTS in speak() for every spoken output. Field tests repeat many short phrases, so caching should improve responsiveness without changing model behavior.
Suggested implementation
- Add normalized-text cache keys for translation:
- key:
(normalized_text, source_lang, target_lang)
- normalize by trimming and collapsing whitespace.
- Use
functools.lru_cache(maxsize=512) or a small explicit LRU/cachetools cache.
- Consider a separate cache for TTS output:
- key:
(normalized_text, tts_lang)
- value:
(sample_rate, int16_pcm) or a safe immutable/copyable representation.
- Avoid returning a mutable cached NumPy array directly unless copied before yield.
- Add lightweight logging for cache hits/misses, gated by an env var if noisy.
Acceptance criteria
- Repeated identical utterances avoid duplicate Google Translate calls.
- Repeated identical TTS phrases avoid re-generating and re-loading MP3 audio.
- Empty/whitespace input still returns immediately.
- Existing behavior remains unchanged when cache misses occur.
- Works in Hugging Face Spaces without requiring a persistent filesystem.
Files likely involved
Notes
This is a quick win before larger TTS replacement work. Keep the change small and low-risk.
Goal
Reduce latency, network calls, and phone battery drain in
hf_space/app.pyby caching repeated translation and speech outputs.Context
The realtime app currently calls Google Translate for every utterance in
translate()and usesgTTSinspeak()for every spoken output. Field tests repeat many short phrases, so caching should improve responsiveness without changing model behavior.Suggested implementation
(normalized_text, source_lang, target_lang)functools.lru_cache(maxsize=512)or a small explicit LRU/cachetools cache.(normalized_text, tts_lang)(sample_rate, int16_pcm)or a safe immutable/copyable representation.Acceptance criteria
Files likely involved
hf_space/app.pyNotes
This is a quick win before larger TTS replacement work. Keep the change small and low-risk.