-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: Add MP3Reader class for mp3 file reader #194
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
Open
Deepchavda007
wants to merge
10
commits into
Cinnamon:main
Choose a base branch
from
Deepchavda007:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bbb0f98
Add MP3Reader class for mp3 file read
Deepchavda007 ffe2313
Add MP3Reader class for mp3 file read files.py
Deepchavda007 919ca75
Add MP3Reader class for mp3 file read mp3_loader.py
Deepchavda007 ab862e4
Merge branch 'main' into main
Deepchavda007 27aef82
fix: make MP3Reader runnable
cin-albert 127bc5c
Merge branch 'main' into main
cin-albert 5a1cad5
feat: use Param.auto() to lazy load and cache the ASR pipeline
cin-albert fa3ce2c
test: add unit test for MP3Reader
cin-albert 32b1ee0
style: fix pre-commit
cin-albert 1335a0b
test: skip test of mp3_reader when librosa not installed
cin-albert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import torch | ||
| from loguru import logger | ||
| from typing import Optional, List | ||
| from kotaemon.base import Document, BaseReader | ||
| from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline | ||
|
|
||
|
|
||
| ###--------------------------------------------------------------------------### | ||
|
|
||
|
|
||
| class MP3Reader(BaseReader): | ||
| def __init__( | ||
| self, | ||
| model_id: str = "distil-whisper/distil-large-v3", | ||
| cache_dir: str = "models", | ||
| ): | ||
| try: | ||
| # Device and model configuration | ||
| self.torch_dtype = ( | ||
| torch.float16 if torch.cuda.is_available() else torch.float32 | ||
| ) | ||
| self.device = "cuda:0" if torch.cuda.is_available() else "cpu" | ||
|
|
||
| # Model and processor initialization | ||
| self.model = AutoModelForSpeechSeq2Seq.from_pretrained( | ||
| model_id, | ||
| torch_dtype=self.torch_dtype, | ||
| low_cpu_mem_usage=True, | ||
| use_safetensors=True, | ||
| cache_dir=cache_dir, | ||
| ).to(self.device) | ||
|
|
||
| self.processor = AutoProcessor.from_pretrained(model_id) | ||
|
|
||
| # ASR pipeline setup | ||
| self.asr_pipeline = pipeline( | ||
| "automatic-speech-recognition", | ||
| model=self.model, | ||
| tokenizer=self.processor.tokenizer, | ||
| feature_extractor=self.processor.feature_extractor, | ||
| max_new_tokens=128, | ||
| torch_dtype=self.torch_dtype, | ||
| device=self.device, | ||
| ) | ||
| logger.info("ASR pipeline setup successful.") | ||
| except Exception as e: | ||
| logger.error(f"Error occurred during ASR pipeline setup: {e}") | ||
| raise | ||
|
|
||
| def speech_to_text(self, audio_path: str) -> str: | ||
| try: | ||
| # Performing speech recognition | ||
| result = self.asr_pipeline(audio_path) | ||
| return result.get("text", "Error: Text not found in the result") | ||
| except Exception as e: | ||
| logger.error(f"Error occurred during speech recognition: {e}") | ||
| return "Error: Speech recognition failed" | ||
|
|
||
| def load_data( | ||
| self, audio_file: str, extra_info: Optional[dict] = None | ||
| ) -> List[Document]: | ||
| try: | ||
| # Get text from the audio file | ||
| text = self.speech_to_text(audio_file) | ||
|
|
||
| metadata = extra_info or {} | ||
|
|
||
| return [Document(text=text, metadata=metadata)] | ||
| except Exception as e: | ||
| logger.error(f"Error occurred while loading data: {e}") | ||
| return [] | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.