add Moss result to db
This commit is contained in:
@@ -2,10 +2,12 @@ import numpy as np
|
||||
|
||||
from sentence_api.processing import (
|
||||
MOSS_ALGORITHM_VERSION,
|
||||
VideoProcessor,
|
||||
document_from_transcript,
|
||||
split_sentence_text,
|
||||
)
|
||||
from sentence_api.models import SentenceBoundary
|
||||
from sentence_api.models import SentenceBoundary, SentenceBoundaryDocument
|
||||
from sentence_api.repository import VideoRepository
|
||||
from sentence_api.transcription import (
|
||||
Transcript,
|
||||
TranscriptionSegment,
|
||||
@@ -158,3 +160,166 @@ def test_split_sentence_text_uses_whisper_word_timestamps(tmp_path):
|
||||
("Alpha beta", 1000, 3000),
|
||||
("Gamma delta", 3000, 5000),
|
||||
]
|
||||
|
||||
|
||||
def test_processor_persists_original_moss_segments(tmp_path, monkeypatch):
|
||||
from sentence_api.config import Settings
|
||||
from dataclasses import replace
|
||||
|
||||
settings = replace(Settings.from_env(), data_dir=tmp_path / "data")
|
||||
settings.ensure_directories()
|
||||
media_path = settings.videos_dir / f"{VIDEO_HASH}.mp4"
|
||||
media_path.write_bytes(b"video")
|
||||
repository = VideoRepository(settings.database_path)
|
||||
repository.upsert_upload(
|
||||
video_hash=VIDEO_HASH,
|
||||
title="MOSS lesson",
|
||||
filename="lesson.mp4",
|
||||
stored_filename=f"{VIDEO_HASH}.mp4",
|
||||
content_type="video/mp4",
|
||||
size_bytes=5,
|
||||
language="en",
|
||||
)
|
||||
transcript = Transcript(
|
||||
text="Hello world. Next.",
|
||||
segments=[
|
||||
TranscriptionSegment(
|
||||
start_seconds=0.0,
|
||||
end_seconds=1.2,
|
||||
text="Hello world.",
|
||||
speaker="S01",
|
||||
words=[WordTimestamp(0.0, 0.5, "Hello"), WordTimestamp(0.5, 1.2, "world.")],
|
||||
),
|
||||
TranscriptionSegment(
|
||||
start_seconds=1.2,
|
||||
end_seconds=2.4,
|
||||
text="Next.",
|
||||
speaker="S01",
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
class AvailableTranscriber:
|
||||
available = True
|
||||
|
||||
def transcribe(self, audio_path, language=None):
|
||||
return transcript
|
||||
|
||||
monkeypatch.setattr("sentence_api.processing._ensure_webm_cues_front", lambda path: None)
|
||||
monkeypatch.setattr(
|
||||
"sentence_api.processing.extract_audio",
|
||||
lambda src, dst: dst.write_bytes(b"RIFF"),
|
||||
)
|
||||
monkeypatch.setattr("sentence_api.processing._media_duration_ms", lambda path: 2400)
|
||||
monkeypatch.setattr(
|
||||
"sentence_api.processing.document_from_transcript",
|
||||
lambda **kwargs: SentenceBoundaryDocument(
|
||||
video_hash=kwargs["video_hash"],
|
||||
duration_ms=2400,
|
||||
algorithm_version=MOSS_ALGORITHM_VERSION,
|
||||
sentences=[
|
||||
SentenceBoundary(
|
||||
index=0,
|
||||
start_ms=0,
|
||||
end_ms=1200,
|
||||
text="Hello world.",
|
||||
language="en",
|
||||
reference_speech_duration_ms=1100,
|
||||
),
|
||||
SentenceBoundary(
|
||||
index=1,
|
||||
start_ms=1200,
|
||||
end_ms=2400,
|
||||
text="Next.",
|
||||
language="en",
|
||||
reference_speech_duration_ms=1100,
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
|
||||
VideoProcessor(settings, repository, AvailableTranscriber()).process(VIDEO_HASH)
|
||||
|
||||
segments = repository.get_moss_segments(VIDEO_HASH)
|
||||
assert [(item["start_ms"], item["end_ms"], item["text"], item["speaker"]) for item in segments] == [
|
||||
(0, 1200, "Hello world.", "S01"),
|
||||
(1200, 2400, "Next.", "S01"),
|
||||
]
|
||||
assert segments[0]["words"] == [
|
||||
{"start_seconds": 0.0, "end_seconds": 0.5, "text": "Hello"},
|
||||
{"start_seconds": 0.5, "end_seconds": 1.2, "text": "world."},
|
||||
]
|
||||
assert segments[1]["words"] is None
|
||||
|
||||
|
||||
def test_reprocess_without_moss_clears_original_segments(tmp_path, monkeypatch):
|
||||
from sentence_api.config import Settings
|
||||
from dataclasses import replace
|
||||
|
||||
settings = replace(Settings.from_env(), data_dir=tmp_path / "data")
|
||||
settings.ensure_directories()
|
||||
media_path = settings.videos_dir / f"{VIDEO_HASH}.mp4"
|
||||
media_path.write_bytes(b"video")
|
||||
repository = VideoRepository(settings.database_path)
|
||||
repository.upsert_upload(
|
||||
video_hash=VIDEO_HASH,
|
||||
title="MOSS lesson",
|
||||
filename="lesson.mp4",
|
||||
stored_filename=f"{VIDEO_HASH}.mp4",
|
||||
content_type="video/mp4",
|
||||
size_bytes=5,
|
||||
language="en",
|
||||
)
|
||||
repository.save_processing_result(
|
||||
SentenceBoundaryDocument(
|
||||
video_hash=VIDEO_HASH,
|
||||
duration_ms=2400,
|
||||
algorithm_version=MOSS_ALGORITHM_VERSION,
|
||||
sentences=[
|
||||
SentenceBoundary(
|
||||
index=0,
|
||||
start_ms=0,
|
||||
end_ms=2400,
|
||||
text="Hello.",
|
||||
language="en",
|
||||
reference_speech_duration_ms=2000,
|
||||
)
|
||||
],
|
||||
),
|
||||
"Hello.",
|
||||
moss_segments=[
|
||||
{
|
||||
"start_seconds": 0.0,
|
||||
"end_seconds": 2.4,
|
||||
"text": "Hello.",
|
||||
"speaker": "S01",
|
||||
}
|
||||
],
|
||||
)
|
||||
assert repository.get_moss_segments(VIDEO_HASH)
|
||||
|
||||
class UnavailableTranscriber:
|
||||
available = False
|
||||
|
||||
monkeypatch.setattr("sentence_api.processing._ensure_webm_cues_front", lambda path: None)
|
||||
monkeypatch.setattr(
|
||||
"sentence_api.processing.make_entry",
|
||||
lambda *args, **kwargs: (
|
||||
{
|
||||
"duration_ms": 2400,
|
||||
"sentences": [
|
||||
SentenceBoundary(
|
||||
index=0,
|
||||
start_ms=0,
|
||||
end_ms=2400,
|
||||
text=None,
|
||||
language="en",
|
||||
)
|
||||
],
|
||||
},
|
||||
{},
|
||||
),
|
||||
)
|
||||
VideoProcessor(settings, repository, UnavailableTranscriber()).process(VIDEO_HASH)
|
||||
assert repository.get_moss_segments(VIDEO_HASH) == []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user