Repository navigation
Description
Parent Issue
Sub-issue of #4651 — Translate API cost optimization (target: 84–89% reduction)
Objective
Cost saving: The explicit detect_language() API accounts for ~43% of total Translate spend. Eliminating it is the single largest cost lever.
Problem
translate_text() at translation.py:298 sets no source_language_code. Google auto-detects the source language for free inside every translate call and returns detected_language_code in the response. But the code ALSO calls _client.detect_language() explicitly at translation.py:191, paying $20/M chars for detection that translate already does for free.
Every foreign-language segment is billed 2x: once for detect, once for translate. Same per-character rate — pure waste.
Changes Required
1. Remove explicit detection path
backend/utils/translation.py: Remove_detect_with_google_cloud()function (lines 189-196) and all_client.detect_language()callsbackend/utils/translation_cache.py: Remove or simplifyTranscriptSegmentLanguageCache— detection is no longer needed as a pre-filter
2. Use detected_language_code from translate response
- In
translate_text()(translation.py:273-316): captureresponse.translations[0].detected_language_code - Return both translated text AND detected language code
3. Fix same-language check in transcribe.py
backend/routers/transcribe.py:1155: Replace brittletranslated_text == segment_textwithdetected_language_code == target_language- Google Translate normalizes punctuation/spacing/quotes even for same-language text — equality check produces false negatives
4. Keep free langdetect for any non-translation language needs
- If any other code path needs language detection without translation, use the free
langdetectlibrary (already imported)
Files to Modify
| File | Change |
|---|---|
backend/utils/translation.py |
Remove _detect_with_google_cloud, update translate_text() return |
backend/utils/translation_cache.py |
Simplify or remove |
backend/routers/transcribe.py |
Use detected_language_code instead of text equality |
Test Plan
- Verify translation still works for non-target-language segments
- Verify target-language segments are correctly identified via
detected_language_code - Verify no
detect_languageAPI calls in Cloud Console after deploy - Run existing translation tests
Acceptance Criteria
- Zero
detect_languageAPI calls to Google Cloud - Translation behavior unchanged for users
detected_language_codeused for same-language detection
Sub-issue 1/4 of #4651. Do this FIRST — largest single impact (~43% reduction).
Activity
perf: eliminate redundant detect_language API calls (#4712)
perf(translate): eliminate paid detect_language API, add Redis cache,…
perf(translate): eliminate redundant API calls, add Redis cache, batc…
perf(translate): eliminate paid detect_language API, add Redis cache,…
perf(translate): eliminate redundant API calls, add Redis cache, batc…