mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-24 23:54:14 +00:00
PR comments
This commit is contained in:
parent
e9abe176bc
commit
1b7caa1a29
8
libs/langchain/langchain/tools/eleven_labs/models.py
Normal file
8
libs/langchain/langchain/tools/eleven_labs/models.py
Normal file
@ -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"
|
@ -1,10 +1,19 @@
|
|||||||
import tempfile
|
import tempfile
|
||||||
from typing import Dict
|
from typing import Dict, Union
|
||||||
|
|
||||||
from langchain.pydantic_v1 import root_validator
|
from langchain.pydantic_v1 import root_validator
|
||||||
from langchain.tools.base import BaseTool
|
from langchain.tools.base import BaseTool
|
||||||
|
from langchain.tools.eleven_labs.models import ElevenLabsModel
|
||||||
from langchain.utils import get_from_dict_or_env
|
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):
|
class ElevenLabsText2SpeechTool(BaseTool):
|
||||||
"""Tool that queries the Eleven Labs Text2Speech API.
|
"""Tool that queries the Eleven Labs Text2Speech API.
|
||||||
@ -13,6 +22,8 @@ class ElevenLabsText2SpeechTool(BaseTool):
|
|||||||
https://docs.elevenlabs.io/welcome/introduction
|
https://docs.elevenlabs.io/welcome/introduction
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
model: Union[ElevenLabsModel, str] = ElevenLabsModel.MULTI_LINGUAL
|
||||||
|
|
||||||
name: str = "eleven_labs_text2speech"
|
name: str = "eleven_labs_text2speech"
|
||||||
description: str = (
|
description: str = (
|
||||||
"A wrapper around Eleven Labs Text2Speech. "
|
"A wrapper around Eleven Labs Text2Speech. "
|
||||||
@ -29,16 +40,7 @@ class ElevenLabsText2SpeechTool(BaseTool):
|
|||||||
return values
|
return values
|
||||||
|
|
||||||
def _text2speech(self, text: str) -> str:
|
def _text2speech(self, text: str) -> str:
|
||||||
try:
|
speech = elevenlabs.generate(text=text, model=self.model)
|
||||||
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")
|
|
||||||
with tempfile.NamedTemporaryFile(mode="bx", suffix=".wav", delete=False) as f:
|
with tempfile.NamedTemporaryFile(mode="bx", suffix=".wav", delete=False) as f:
|
||||||
f.write(speech)
|
f.write(speech)
|
||||||
return f.name
|
return f.name
|
||||||
@ -53,32 +55,13 @@ class ElevenLabsText2SpeechTool(BaseTool):
|
|||||||
|
|
||||||
def play(self, speech_file: str) -> None:
|
def play(self, speech_file: str) -> None:
|
||||||
"""Play the text as speech."""
|
"""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:
|
with open(speech_file, mode="rb") as f:
|
||||||
speech = f.read()
|
speech = f.read()
|
||||||
|
|
||||||
play(speech)
|
elevenlabs.play(speech)
|
||||||
|
|
||||||
def stream(self, query: str) -> None:
|
def stream(self, query: str) -> None:
|
||||||
"""Stream the text as speech."""
|
"""Stream the text as speech as it is generated.
|
||||||
|
Play the text in your speakers."""
|
||||||
try:
|
speech_stream = elevenlabs.generate(text=query, model=self.model, stream=True)
|
||||||
from elevenlabs import generate, stream
|
elevenlabs.stream(speech_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)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user