-
Notifications
You must be signed in to change notification settings - Fork 5
Fix extractor and gating tests #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| from video_sampler.language.keyword_capture import AudioKeywordExtractor | ||
|
|
||
|
|
||
| def test_audio_extractor_requires_whisper(monkeypatch): | ||
| monkeypatch.setitem(sys.modules, "whisper", None) | ||
| with pytest.raises(ImportError): | ||
| AudioKeywordExtractor(["test"]) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from .keyword_capture import AudioKeywordExtractor, KeywordExtractor | ||
|
|
||
| __all__ = ["KeywordExtractor", "AudioKeywordExtractor"] |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -81,18 +81,7 @@ def __init__(self, keywords: list[str]) -> None: | |||||||||||||||||||||||||||||||||||
| def generate_segments( | ||||||||||||||||||||||||||||||||||||
| self, subtitle_list: list[tuple[tuple[int, int], str]] | ||||||||||||||||||||||||||||||||||||
| ) -> Iterable[subtitle_line]: | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| Captures keyword segments from a list of subtitles. | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||
| subtitle_list (list[tuple[tuple[int, int], str]]): List of subtitles in the format | ||||||||||||||||||||||||||||||||||||
| (start_time, end_time, content). | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| Yields: | ||||||||||||||||||||||||||||||||||||
| subtitle_line: A named tuple representing a keyword segment in the format | ||||||||||||||||||||||||||||||||||||
| (start_time, end_time, lemma, content). | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| """Capture segments containing the configured keywords.""" | ||||||||||||||||||||||||||||||||||||
| for (start_time, end_time), content in subtitle_list: | ||||||||||||||||||||||||||||||||||||
| doc = self.nlp(content.lower()) | ||||||||||||||||||||||||||||||||||||
| for lemma in doc: | ||||||||||||||||||||||||||||||||||||
|
|
@@ -105,8 +94,37 @@ def generate_segments( | |||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| class AudioKeywordExtractor(KeywordExtractor): | ||||||||||||||||||||||||||||||||||||
| """Extract keywords from an audio file by transcribing it first.""" | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def __init__(self, keywords: list[str], model: str = "base") -> None: | ||||||||||||||||||||||||||||||||||||
| super().__init__(keywords) | ||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||
| import whisper | ||||||||||||||||||||||||||||||||||||
| except ImportError as e: # pragma: no cover - optional dependency | ||||||||||||||||||||||||||||||||||||
| raise ImportError( | ||||||||||||||||||||||||||||||||||||
| "To use this feature install whisper by 'pip install openai-whisper'" | ||||||||||||||||||||||||||||||||||||
| ) from e | ||||||||||||||||||||||||||||||||||||
| self.whisper = whisper.load_model(model) | ||||||||||||||||||||||||||||||||||||
|
LemurPwned marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def transcribe(self, audio_path: str) -> list[tuple[tuple[int, int], str]]: | ||||||||||||||||||||||||||||||||||||
| result = self.whisper.transcribe(audio_path) | ||||||||||||||||||||||||||||||||||||
| subtitle_list: list[tuple[tuple[int, int], str]] = [] | ||||||||||||||||||||||||||||||||||||
| for seg in result.get("segments", []): | ||||||||||||||||||||||||||||||||||||
| subtitle_list.append( | ||||||||||||||||||||||||||||||||||||
| ((int(seg["start"] * 1000), int(seg["end"] * 1000)), seg["text"]) | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+113
to
+116
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Trim whitespace from Whisper segment text Call .strip() on seg['text'] to remove extra whitespace and prevent matching issues.
Suggested change
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not change the indents
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point—let’s keep the existing indent structure and just call you can change it to: This way you only trim the whitespace and don’t alter the indentation levels. Let me know if that aligns with your style rules!
Comment on lines
+112
to
+116
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): Convert for loop into list comprehension (
Suggested change
|
||||||||||||||||||||||||||||||||||||
| return subtitle_list | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def generate_segments_from_audio(self, audio_path: str) -> Iterable[subtitle_line]: | ||||||||||||||||||||||||||||||||||||
| subtitle_list = self.transcribe(audio_path) | ||||||||||||||||||||||||||||||||||||
| yield from super().generate_segments(subtitle_list) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def create_extractor(config: dict): | ||||||||||||||||||||||||||||||||||||
| if config["type"] == "keyword": | ||||||||||||||||||||||||||||||||||||
| return KeywordExtractor(**config["args"]) | ||||||||||||||||||||||||||||||||||||
| if config["type"] == "audio_keyword": | ||||||||||||||||||||||||||||||||||||
| return AudioKeywordExtractor(**config["args"]) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| raise NotImplementedError(f"{config['type']} not implemented yet") | ||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.