Skip to content

Clone your own voice on your own computer: speak any text in 60+ languages and dub videos into another language in your voice. Fully local. Early version.

2.3 KB
Raw Blame
import os
from pathlib import Path

import pytest
from mimvox_engine import reflang, textlang
from mimvox_engine.dataset import lang
from mimvox_engine.redub import langs

MODEL = os.environ.get("MIMVOX_LID_MODEL")


@pytest.fixture
def no_model(monkeypatch):
    monkeypatch.setattr(textlang, "_path", [None])
    monkeypatch.setattr(textlang, "_model", [False])


@pytest.fixture
def model(monkeypatch):
    if not MODEL or not Path(MODEL).is_file():
        pytest.skip("MIMVOX_LID_MODEL names no lid.176.ftz")
    monkeypatch.setattr(textlang, "_path", [Path(MODEL)])
    monkeypatch.setattr(textlang, "_model", [False])


def test_without_the_model_the_old_guesses_stay(no_model):
    assert textlang.detect("i like eating bread") is None
    assert lang.detect("the cat is on the mat and it is happy") == "en"
    assert langs.script_language_guess("Привіт, як справи") == "uk"
    assert reflang.script_language("Привіт, як справи? Це мій голос") == "uk"


def test_configure_finds_the_model_under_the_runtime(tmp_path, no_model):
    engine = tmp_path / "engine" / ".venv" / "bin" / "python"
    textlang.configure({"python": {"engine": str(engine)}})
    assert textlang._path[0] == tmp_path / "models" / "lid" / "lid.176.ftz"
    assert textlang.detect("i like eating bread") is None


def test_a_broken_model_is_no_model(tmp_path, monkeypatch):
    broken = tmp_path / "lid.176.ftz"
    broken.write_bytes(b"not a model")
    monkeypatch.setattr(textlang, "_path", [broken])
    monkeypatch.setattr(textlang, "_model", [False])
    assert textlang.detect("i like eating bread") is None


def test_fasttext_names_the_language(model):
    assert textlang.detect("i like eating bread") == "en"
    assert textlang.detect("Ich esse gern Brot") == "de"
    assert textlang.detect("Это мой новый голос") == "ru"
    assert textlang.detect("Lubię jeść chleb") == "pl"
    assert textlang.detect("Hello") is None
    assert textlang.detect("i like eating bread", among={"de", "fr"}) is None


def test_the_old_guesses_use_fasttext(model):
    assert lang.detect("i like eating bread", among=lang.DETECTABLE) == "en"
    assert langs.script_language_guess("Me gusta comer pan con mantequilla") == "es"
    assert reflang.script_language("Jeg kan godt lide at spise brød") == "da"