Files
stimmwerk/tools/test_transcribe_words.py
wlkns-dev 3e40b7346b Whisper-Werkzeug: Option --language, Sprache standardmäßig automatisch erkennen
Die Sprache war fest auf Englisch gestellt; deutscher Gesang wurde als Englisch erkannt
(falsche Wörter, falsches Timing). Das Skript läuft jetzt über main() und hat einen Test
mit Attrappe für faster-whisper.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-02 09:07:51 +01:00

65 lines
2.0 KiB
Python

"""Tests für tools/transcribe_words.py ohne Whisper-Modell (faster_whisper wird durch eine Attrappe ersetzt)."""
import importlib.util
import json
import sys
import tempfile
import types
import unittest
from pathlib import Path
calls = []
class FakeModel:
def __init__(self, *args, **kwargs):
pass
def transcribe(self, audio, **kwargs):
calls.append(kwargs)
word = types.SimpleNamespace(word=" hallo", start=1.0, end=1.5, probability=0.9)
seg = types.SimpleNamespace(start=1.0, end=1.5, text=" hallo", words=[word])
info = types.SimpleNamespace(duration=3.0, language="de", language_probability=0.97)
return iter([seg]), info
def load_tool():
stub = types.ModuleType("faster_whisper")
stub.WhisperModel = FakeModel
sys.modules["faster_whisper"] = stub
path = Path(__file__).with_name("transcribe_words.py")
spec = importlib.util.spec_from_file_location("transcribe_words", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
class TranscribeWordsTest(unittest.TestCase):
def setUp(self):
calls.clear()
self.tool = load_tool()
self.tmp = tempfile.TemporaryDirectory()
self.out = Path(self.tmp.name) / "words.json"
def tearDown(self):
self.tmp.cleanup()
def run_tool(self, *extra):
self.tool.main(["lied.mp3", "--out", str(self.out), *extra])
return json.loads(self.out.read_text(encoding="utf-8"))
def test_without_language_whisper_detects_it(self):
self.run_tool()
self.assertIsNone(calls[0]["language"])
def test_language_is_passed_through(self):
self.run_tool("--language", "de")
self.assertEqual(calls[0]["language"], "de")
def test_output_contains_words_with_times(self):
data = self.run_tool("--language", "de")
self.assertEqual(data["segments"][0]["words"][0], {"w": "hallo", "start": 1.0, "end": 1.5, "p": 0.9})
if __name__ == "__main__":
unittest.main()