Add translation and TTS caching
This commit is contained in:
@@ -8,6 +8,7 @@ import httpx
|
||||
import pytest
|
||||
|
||||
from src.core_utils import TranslationError
|
||||
from src import translation
|
||||
from src.translation import LMStudioTranslator, TranslationConfig
|
||||
|
||||
|
||||
@@ -56,7 +57,8 @@ def test_build_contextual_batch_payload_includes_neighboring_segments():
|
||||
|
||||
assert payload["model"] == "gemma-3-4b-it"
|
||||
assert payload["messages"][0]["role"] == "system"
|
||||
assert "expert audiovisual translator for dubbed video content" in payload["messages"][0]["content"]
|
||||
assert "expert audiovisual translator and dubbing script adapter" in payload["messages"][0]["content"]
|
||||
assert "Preserve segment ids and output order exactly" in payload["messages"][0]["content"]
|
||||
assert payload["temperature"] == 0.0
|
||||
assert payload["top_p"] == 1.0
|
||||
assert user_payload == {
|
||||
@@ -116,6 +118,29 @@ def test_translate_segments_batches_context_and_preserves_exact_mapping():
|
||||
assert [item["id"] for item in requests[1]["segments"]] == ["4", "5", "6"]
|
||||
|
||||
|
||||
def test_translate_segments_uses_persistent_cache(tmp_path, monkeypatch):
|
||||
requests = {"count": 0}
|
||||
monkeypatch.setattr(translation, "TRANSLATION_CACHE_DIR", tmp_path)
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
requests["count"] += 1
|
||||
payload = _read_request_json(request)
|
||||
batch_request = json.loads(payload["messages"][1]["content"])
|
||||
translations = [
|
||||
{"id": item["id"], "translated_text": f"cached::{item['text']}"}
|
||||
for item in batch_request["segments"]
|
||||
]
|
||||
return _mock_batch_response(translations)
|
||||
|
||||
config = TranslationConfig(model="cache-model")
|
||||
first_translator = LMStudioTranslator(config, client=_mock_client(handler))
|
||||
second_translator = LMStudioTranslator(config, client=_mock_client(handler))
|
||||
|
||||
assert first_translator.translate_segments(["hello", "world"], "fr", "en") == ["cached::hello", "cached::world"]
|
||||
assert second_translator.translate_segments(["hello", "world"], "fr", "en") == ["cached::hello", "cached::world"]
|
||||
assert requests["count"] == 1
|
||||
|
||||
|
||||
def test_retry_on_transient_http_error_then_succeeds():
|
||||
attempts = {"count": 0}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user