Skip to content

Add LRU caching for repeated translations and TTS output #10

Description

@trinitron88

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

  • hf_space/app.py

Notes

This is a quick win before larger TTS replacement work. Keep the change small and low-risk.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions