mirror of https://github.com/hwchase17/langchain
Create OpenAIWhisperParser for generating Documents from audio files (#5580)
# OpenAIWhisperParser This PR creates a new parser, `OpenAIWhisperParser`, that uses the [OpenAI Whisper model](https://platform.openai.com/docs/guides/speech-to-text/quickstart) to perform transcription of audio files to text (`Documents`). Please see the notebook for usage.pull/5772/head
parent
a4c9053d40
commit
aea090045b
File diff suppressed because one or more lines are too long
@ -0,0 +1,21 @@
|
||||
from typing import Iterator
|
||||
|
||||
from langchain.document_loaders.base import BaseBlobParser
|
||||
from langchain.document_loaders.blob_loaders import Blob
|
||||
from langchain.schema import Document
|
||||
|
||||
|
||||
class OpenAIWhisperParser(BaseBlobParser):
|
||||
"""Transcribe and parse audio files.
|
||||
Audio transcription is with OpenAI Whisper model."""
|
||||
|
||||
def lazy_parse(self, blob: Blob) -> Iterator[Document]:
|
||||
"""Lazily parse the blob."""
|
||||
|
||||
import openai
|
||||
|
||||
with blob.as_bytes_io() as f:
|
||||
transcript = openai.Audio.transcribe("whisper-1", f)
|
||||
yield Document(
|
||||
page_content=transcript.text, metadata={"source": blob.source}
|
||||
)
|
Loading…
Reference in New Issue