From 1b7caa1a29e58d47acb764b01d7553edde0aaf1e Mon Sep 17 00:00:00 2001 From: "mateusz.wosinski" Date: Mon, 4 Sep 2023 11:50:47 +0200 Subject: [PATCH] PR comments --- .../langchain/tools/eleven_labs/models.py | 8 +++ .../tools/eleven_labs/text2speech.py | 53 +++++++------------ 2 files changed, 26 insertions(+), 35 deletions(-) create mode 100644 libs/langchain/langchain/tools/eleven_labs/models.py diff --git a/libs/langchain/langchain/tools/eleven_labs/models.py b/libs/langchain/langchain/tools/eleven_labs/models.py new file mode 100644 index 0000000000..c977b2972f --- /dev/null +++ b/libs/langchain/langchain/tools/eleven_labs/models.py @@ -0,0 +1,8 @@ +from enum import Enum + + +class ElevenLabsModel(str, Enum): + """Models available for Eleven Labs Text2Speech.""" + + MULTI_LINGUAL = "eleven_multilingual_v1" + MONO_LINGUAL = "eleven_monolingual_v1" diff --git a/libs/langchain/langchain/tools/eleven_labs/text2speech.py b/libs/langchain/langchain/tools/eleven_labs/text2speech.py index e59652fb19..5c6edb00b9 100644 --- a/libs/langchain/langchain/tools/eleven_labs/text2speech.py +++ b/libs/langchain/langchain/tools/eleven_labs/text2speech.py @@ -1,10 +1,19 @@ import tempfile -from typing import Dict +from typing import Dict, Union from langchain.pydantic_v1 import root_validator from langchain.tools.base import BaseTool +from langchain.tools.eleven_labs.models import ElevenLabsModel from langchain.utils import get_from_dict_or_env +try: + import elevenlabs + +except ImportError: + raise ImportError( + "elevenlabs is not installed. " "Run `pip install elevenlabs` to install." + ) + class ElevenLabsText2SpeechTool(BaseTool): """Tool that queries the Eleven Labs Text2Speech API. @@ -13,6 +22,8 @@ class ElevenLabsText2SpeechTool(BaseTool): https://docs.elevenlabs.io/welcome/introduction """ + model: Union[ElevenLabsModel, str] = ElevenLabsModel.MULTI_LINGUAL + name: str = "eleven_labs_text2speech" description: str = ( "A wrapper around Eleven Labs Text2Speech. " @@ -29,16 +40,7 @@ class ElevenLabsText2SpeechTool(BaseTool): return values def _text2speech(self, text: str) -> str: - try: - from elevenlabs import generate - - except ImportError: - raise ImportError( - "elevenlabs is not installed. " - "Run `pip install elevenlabs` to install." - ) - - speech = generate(text=text, model="eleven_multilingual_v1") + speech = elevenlabs.generate(text=text, model=self.model) with tempfile.NamedTemporaryFile(mode="bx", suffix=".wav", delete=False) as f: f.write(speech) return f.name @@ -53,32 +55,13 @@ class ElevenLabsText2SpeechTool(BaseTool): def play(self, speech_file: str) -> None: """Play the text as speech.""" - try: - from elevenlabs import play - - except ImportError: - raise ImportError( - "elevenlabs is not installed. " - "Run `pip install elevenlabs` to install." - ) with open(speech_file, mode="rb") as f: speech = f.read() - play(speech) + elevenlabs.play(speech) def stream(self, query: str) -> None: - """Stream the text as speech.""" - - try: - from elevenlabs import generate, stream - - except ImportError: - raise ImportError( - "elevenlabs is not installed. " - "Run `pip install elevenlabs` to install." - ) - - speech_stream = generate( - text=query, model="eleven_multilingual_v1", stream=True - ) - stream(speech_stream) + """Stream the text as speech as it is generated. + Play the text in your speakers.""" + speech_stream = elevenlabs.generate(text=query, model=self.model, stream=True) + elevenlabs.stream(speech_stream)