diff --git a/libs/langchain/langchain/document_loaders/audio.py b/libs/langchain/langchain/document_loaders/audio.py index 4fe4985a6b788..db35165daa729 100644 --- a/libs/langchain/langchain/document_loaders/audio.py +++ b/libs/langchain/langchain/document_loaders/audio.py @@ -1,27 +1,24 @@ -from typing import Any, Iterator, List +from __future__ import annotations -from langchain_core.documents.base import Document +from pathlib import Path +from typing import Any, Union -from langchain.document_loaders import Blob -from langchain.document_loaders.base import BaseLoader +from langchain.document_loaders.blob_loaders import FileSystemBlobLoader +from langchain.document_loaders.generic import GenericLoader from langchain.document_loaders.parsers.audio import AzureSpeechServiceParser -class AzureSpeechServiceLoader(BaseLoader): - def __init__(self, file_path: str, **kwargs: Any) -> None: - """Initialize with file path.""" - super().__init__() - - self.file_path = file_path - self.parser = AzureSpeechServiceParser(**kwargs) - - def load(self) -> List[Document]: - """Eagerly load the content.""" - return list(self.lazy_load()) - - def lazy_load( - self, - ) -> Iterator[Document]: - """Lazily lod documents.""" - blob = Blob.from_path(self.file_path) - return iter(self.parser.parse(blob)) +class AzureSpeechServiceLoader(GenericLoader): + @classmethod + def from_path( + cls, path: Union[str, Path], **kwargs: Any + ) -> AzureSpeechServiceLoader: + path = path if isinstance(path, Path) else Path(path) + if path.is_file(): + loader_params: dict = {"glob": path.name} + path = path.parent + else: + loader_params = {} + loader = FileSystemBlobLoader(path, **loader_params) + parser = AzureSpeechServiceParser(**kwargs) + return cls(loader, parser) diff --git a/libs/langchain/langchain/document_loaders/blob_loaders/schema.py b/libs/langchain/langchain/document_loaders/blob_loaders/schema.py index 9d1e737e3745a..b9bfaa4aff992 100644 --- a/libs/langchain/langchain/document_loaders/blob_loaders/schema.py +++ b/libs/langchain/langchain/document_loaders/blob_loaders/schema.py @@ -155,9 +155,7 @@ class BlobLoader(ABC): """ @abstractmethod - def yield_blobs( - self, - ) -> Iterable[Blob]: + def yield_blobs(self) -> Iterable[Blob]: """A lazy loader for raw data represented by LangChain's Blob object. Returns: diff --git a/libs/langchain/langchain/document_loaders/parsers/audio.py b/libs/langchain/langchain/document_loaders/parsers/audio.py index 3c73a8eb312f7..f14f5e64d6142 100644 --- a/libs/langchain/langchain/document_loaders/parsers/audio.py +++ b/libs/langchain/langchain/document_loaders/parsers/audio.py @@ -346,11 +346,9 @@ def lazy_parse(self, blob: Blob) -> Iterator[Document]: except ImportError: raise ImportError( "azure.cognitiveservices.speech package not found, please install " - "it with `pip install azure-cognitiveservices-speech`" + "it with `pip install azure-cognitiveservices-speech`." ) - """transcribes a conversation""" - def conversation_transcriber_recognition_canceled_cb( evt: speechsdk.SessionEventArgs ) -> None: diff --git a/libs/langchain/tests/unit_tests/document_loaders/test_audio.py b/libs/langchain/tests/integration_tests/test_audio.py similarity index 78% rename from libs/langchain/tests/unit_tests/document_loaders/test_audio.py rename to libs/langchain/tests/integration_tests/test_audio.py index b09f117eaf547..f4b23d7291671 100644 --- a/libs/langchain/tests/unit_tests/document_loaders/test_audio.py +++ b/libs/langchain/tests/integration_tests/test_audio.py @@ -15,7 +15,7 @@ def _get_csv_file_path() -> str: def test_azure_speech_load_key_region_auto_detect_languages() -> None: - loader = AzureSpeechServiceLoader( + loader = AzureSpeechServiceLoader.from_path( _get_csv_file_path(), key=SPEECH_SERVICE_KEY, region=SPEECH_SERVICE_REGION, @@ -26,7 +26,7 @@ def test_azure_speech_load_key_region_auto_detect_languages() -> None: def test_azure_speech_load_key_region_language() -> None: - loader = AzureSpeechServiceLoader( + loader = AzureSpeechServiceLoader.from_path( _get_csv_file_path(), key=SPEECH_SERVICE_KEY, region=SPEECH_SERVICE_REGION, @@ -37,7 +37,7 @@ def test_azure_speech_load_key_region_language() -> None: def test_azure_speech_load_key_region() -> None: - loader = AzureSpeechServiceLoader( + loader = AzureSpeechServiceLoader.from_path( _get_csv_file_path(), key=SPEECH_SERVICE_KEY, region=SPEECH_SERVICE_REGION ) documents = loader.load() @@ -45,10 +45,13 @@ def test_azure_speech_load_key_region() -> None: def test_azure_speech_load_key_endpoint() -> None: - loader = AzureSpeechServiceLoader( + loader = AzureSpeechServiceLoader.from_path( _get_csv_file_path(), key=SPEECH_SERVICE_KEY, - endpoint=f"wss://{SPEECH_SERVICE_REGION}.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1", + endpoint=( + f"wss://{SPEECH_SERVICE_REGION}.stt.speech.microsoft.com/speech/recognition" + "/conversation/cognitiveservices/v1", + ), ) documents = loader.load() assert "what" in documents[0].page_content.lower() diff --git a/libs/langchain/tests/unit_tests/document_loaders/test_audio/whatstheweatherlike.wav b/libs/langchain/tests/integration_tests/test_audio/whatstheweatherlike.wav similarity index 100% rename from libs/langchain/tests/unit_tests/document_loaders/test_audio/whatstheweatherlike.wav rename to libs/langchain/tests/integration_tests/test_audio/whatstheweatherlike.wav diff --git a/libs/langchain/tests/unit_tests/document_loaders/test_audio.png b/libs/langchain/tests/unit_tests/document_loaders/test_audio.png deleted file mode 100644 index 2c05c29d811fe..0000000000000 Binary files a/libs/langchain/tests/unit_tests/document_loaders/test_audio.png and /dev/null differ