Skip to content
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

Skip OpenAIWhisperParser extremely small audio chunks to avoid api error #11450

Merged
merged 6 commits into from
Feb 23, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@

class OpenAIWhisperParser(BaseBlobParser):
"""Transcribe and parse audio files.
Audio transcription is with OpenAI Whisper model."""
Audio transcription is with OpenAI Whisper model.

Parameters:
api_key - OpenAI API key
chunk_duration_threshold - minimum duration of a chunk in seconds
NOTE: According to the OpenAI API, the chunk duration should be at least 0.1
seconds. If the chunk duration is less or equal than the threshold,
it will be skipped.
"""

def __init__(self, api_key: Optional[str] = None):
def __init__(
self, api_key: Optional[str] = None, chunk_duration_threshold: float = 0.1
):
self.api_key = api_key
self.chunk_duration_threshold = chunk_duration_threshold

def lazy_parse(self, blob: Blob) -> Iterator[Document]:
"""Lazily parse the blob."""
Expand Down Expand Up @@ -57,6 +68,9 @@ def lazy_parse(self, blob: Blob) -> Iterator[Document]:
for split_number, i in enumerate(range(0, len(audio), chunk_duration_ms)):
# Audio chunk
chunk = audio[i : i + chunk_duration_ms]
# Skip chunks that are too short to transcribe
if chunk.duration_seconds <= self.chunk_duration_threshold:
continue
file_obj = io.BytesIO(chunk.export(format="mp3").read())
if blob.source is not None:
file_obj.name = blob.source + f"_part_{split_number}.mp3"
Expand Down
Loading