跳到主要內容

Eleven Labs Text2Speech

本筆記本展示如何與 ElevenLabs API 互動以實現文字轉語音功能。

首先,您需要設定一個 ElevenLabs 帳戶。您可以按照這裡的指示進行。

%pip install --upgrade --quiet  elevenlabs langchain-community
import os

os.environ["ELEVEN_API_KEY"] = ""

使用方式

from langchain_community.tools import ElevenLabsText2SpeechTool

text_to_speak = "Hello world! I am the real slim shady"

tts = ElevenLabsText2SpeechTool()
tts.name
'eleven_labs_text2speech'

我們可以產生音訊,將其儲存到暫存檔,然後播放。

speech_file = tts.run(text_to_speak)
tts.play(speech_file)

或者直接串流音訊。

tts.stream_speech(text_to_speak)

在 Agent 中使用

from langchain.agents import AgentType, initialize_agent, load_tools
from langchain_openai import OpenAI
llm = OpenAI(temperature=0)
tools = load_tools(["eleven_labs_text2speech"])
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
)
audio_file = agent.run("Tell me a joke and read it out for me.")


> Entering new AgentExecutor chain...
Action:
\`\`\`
{
"action": "eleven_labs_text2speech",
"action_input": {
"query": "Why did the chicken cross the playground? To get to the other slide!"
}
}
\`\`\`


Observation: /tmp/tmpsfg783f1.wav
Thought: I have the audio file ready to be sent to the human
Action:
\`\`\`
{
"action": "Final Answer",
"action_input": "/tmp/tmpsfg783f1.wav"
}
\`\`\`



> Finished chain.
tts.play(audio_file)

此頁面是否對您有幫助?