Merge pull request #2518 from zgreathouse/hume-tts-service
Hume tts service
This commit is contained in:
10
CHANGELOG.md
10
CHANGELOG.md
@@ -5,6 +5,16 @@ All notable changes to **Pipecat** will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Added `HumeTTSService` for text-to-speech synthesis using Hume AI's expressive voice models.
|
||||||
|
Provides high-quality, emotionally expressive speech synthesis with support for various voice models.
|
||||||
|
Includes example in `examples/foundational/07ad-interruptible-hume.py`.
|
||||||
|
|
||||||
|
- Added `hume` optional dependency group for Hume AI TTS integration.
|
||||||
|
|
||||||
## [0.0.87] - 2025-10-02
|
## [0.0.87] - 2025-10-02
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ You can connect to Pipecat from any platform using our official SDKs:
|
|||||||
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| Speech-to-Text | [AssemblyAI](https://docs.pipecat.ai/server/services/stt/assemblyai), [AWS](https://docs.pipecat.ai/server/services/stt/aws), [Azure](https://docs.pipecat.ai/server/services/stt/azure), [Cartesia](https://docs.pipecat.ai/server/services/stt/cartesia), [Deepgram](https://docs.pipecat.ai/server/services/stt/deepgram), [ElevenLabs](https://docs.pipecat.ai/server/services/stt/elevenlabs), [Fal Wizper](https://docs.pipecat.ai/server/services/stt/fal), [Gladia](https://docs.pipecat.ai/server/services/stt/gladia), [Google](https://docs.pipecat.ai/server/services/stt/google), [Groq (Whisper)](https://docs.pipecat.ai/server/services/stt/groq), [NVIDIA Riva](https://docs.pipecat.ai/server/services/stt/riva), [OpenAI (Whisper)](https://docs.pipecat.ai/server/services/stt/openai), [SambaNova (Whisper)](https://docs.pipecat.ai/server/services/stt/sambanova), [Soniox](https://docs.pipecat.ai/server/services/stt/soniox), [Speechmatics](https://docs.pipecat.ai/server/services/stt/speechmatics), [Ultravox](https://docs.pipecat.ai/server/services/stt/ultravox), [Whisper](https://docs.pipecat.ai/server/services/stt/whisper) |
|
| Speech-to-Text | [AssemblyAI](https://docs.pipecat.ai/server/services/stt/assemblyai), [AWS](https://docs.pipecat.ai/server/services/stt/aws), [Azure](https://docs.pipecat.ai/server/services/stt/azure), [Cartesia](https://docs.pipecat.ai/server/services/stt/cartesia), [Deepgram](https://docs.pipecat.ai/server/services/stt/deepgram), [ElevenLabs](https://docs.pipecat.ai/server/services/stt/elevenlabs), [Fal Wizper](https://docs.pipecat.ai/server/services/stt/fal), [Gladia](https://docs.pipecat.ai/server/services/stt/gladia), [Google](https://docs.pipecat.ai/server/services/stt/google), [Groq (Whisper)](https://docs.pipecat.ai/server/services/stt/groq), [NVIDIA Riva](https://docs.pipecat.ai/server/services/stt/riva), [OpenAI (Whisper)](https://docs.pipecat.ai/server/services/stt/openai), [SambaNova (Whisper)](https://docs.pipecat.ai/server/services/stt/sambanova), [Soniox](https://docs.pipecat.ai/server/services/stt/soniox), [Speechmatics](https://docs.pipecat.ai/server/services/stt/speechmatics), [Ultravox](https://docs.pipecat.ai/server/services/stt/ultravox), [Whisper](https://docs.pipecat.ai/server/services/stt/whisper) |
|
||||||
| LLMs | [Anthropic](https://docs.pipecat.ai/server/services/llm/anthropic), [AWS](https://docs.pipecat.ai/server/services/llm/aws), [Azure](https://docs.pipecat.ai/server/services/llm/azure), [Cerebras](https://docs.pipecat.ai/server/services/llm/cerebras), [DeepSeek](https://docs.pipecat.ai/server/services/llm/deepseek), [Fireworks AI](https://docs.pipecat.ai/server/services/llm/fireworks), [Gemini](https://docs.pipecat.ai/server/services/llm/gemini), [Grok](https://docs.pipecat.ai/server/services/llm/grok), [Groq](https://docs.pipecat.ai/server/services/llm/groq), [Mistral](https://docs.pipecat.ai/server/services/llm/mistral), [NVIDIA NIM](https://docs.pipecat.ai/server/services/llm/nim), [Ollama](https://docs.pipecat.ai/server/services/llm/ollama), [OpenAI](https://docs.pipecat.ai/server/services/llm/openai), [OpenRouter](https://docs.pipecat.ai/server/services/llm/openrouter), [Perplexity](https://docs.pipecat.ai/server/services/llm/perplexity), [Qwen](https://docs.pipecat.ai/server/services/llm/qwen), [SambaNova](https://docs.pipecat.ai/server/services/llm/sambanova) [Together AI](https://docs.pipecat.ai/server/services/llm/together) |
|
| LLMs | [Anthropic](https://docs.pipecat.ai/server/services/llm/anthropic), [AWS](https://docs.pipecat.ai/server/services/llm/aws), [Azure](https://docs.pipecat.ai/server/services/llm/azure), [Cerebras](https://docs.pipecat.ai/server/services/llm/cerebras), [DeepSeek](https://docs.pipecat.ai/server/services/llm/deepseek), [Fireworks AI](https://docs.pipecat.ai/server/services/llm/fireworks), [Gemini](https://docs.pipecat.ai/server/services/llm/gemini), [Grok](https://docs.pipecat.ai/server/services/llm/grok), [Groq](https://docs.pipecat.ai/server/services/llm/groq), [Mistral](https://docs.pipecat.ai/server/services/llm/mistral), [NVIDIA NIM](https://docs.pipecat.ai/server/services/llm/nim), [Ollama](https://docs.pipecat.ai/server/services/llm/ollama), [OpenAI](https://docs.pipecat.ai/server/services/llm/openai), [OpenRouter](https://docs.pipecat.ai/server/services/llm/openrouter), [Perplexity](https://docs.pipecat.ai/server/services/llm/perplexity), [Qwen](https://docs.pipecat.ai/server/services/llm/qwen), [SambaNova](https://docs.pipecat.ai/server/services/llm/sambanova) [Together AI](https://docs.pipecat.ai/server/services/llm/together) |
|
||||||
| Text-to-Speech | [Async](https://docs.pipecat.ai/server/services/tts/asyncai), [AWS](https://docs.pipecat.ai/server/services/tts/aws), [Azure](https://docs.pipecat.ai/server/services/tts/azure), [Cartesia](https://docs.pipecat.ai/server/services/tts/cartesia), [Deepgram](https://docs.pipecat.ai/server/services/tts/deepgram), [ElevenLabs](https://docs.pipecat.ai/server/services/tts/elevenlabs), [Fish](https://docs.pipecat.ai/server/services/tts/fish), [Google](https://docs.pipecat.ai/server/services/tts/google), [Groq](https://docs.pipecat.ai/server/services/tts/groq), [Inworld](https://docs.pipecat.ai/server/services/tts/inworld), [LMNT](https://docs.pipecat.ai/server/services/tts/lmnt), [MiniMax](https://docs.pipecat.ai/server/services/tts/minimax), [Neuphonic](https://docs.pipecat.ai/server/services/tts/neuphonic), [NVIDIA Riva](https://docs.pipecat.ai/server/services/tts/riva), [OpenAI](https://docs.pipecat.ai/server/services/tts/openai), [Piper](https://docs.pipecat.ai/server/services/tts/piper), [PlayHT](https://docs.pipecat.ai/server/services/tts/playht), [Rime](https://docs.pipecat.ai/server/services/tts/rime), [Sarvam](https://docs.pipecat.ai/server/services/tts/sarvam), [XTTS](https://docs.pipecat.ai/server/services/tts/xtts) |
|
| Text-to-Speech | [Async](https://docs.pipecat.ai/server/services/tts/asyncai), [AWS](https://docs.pipecat.ai/server/services/tts/aws), [Azure](https://docs.pipecat.ai/server/services/tts/azure), [Cartesia](https://docs.pipecat.ai/server/services/tts/cartesia), [Deepgram](https://docs.pipecat.ai/server/services/tts/deepgram), [ElevenLabs](https://docs.pipecat.ai/server/services/tts/elevenlabs), [Fish](https://docs.pipecat.ai/server/services/tts/fish), [Google](https://docs.pipecat.ai/server/services/tts/google), [Groq](https://docs.pipecat.ai/server/services/tts/groq), [Hume](https://docs.pipecat.ai/server/services/tts/hume), [Inworld](https://docs.pipecat.ai/server/services/tts/inworld), [LMNT](https://docs.pipecat.ai/server/services/tts/lmnt), [MiniMax](https://docs.pipecat.ai/server/services/tts/minimax), [Neuphonic](https://docs.pipecat.ai/server/services/tts/neuphonic), [NVIDIA Riva](https://docs.pipecat.ai/server/services/tts/riva), [OpenAI](https://docs.pipecat.ai/server/services/tts/openai), [Piper](https://docs.pipecat.ai/server/services/tts/piper), [PlayHT](https://docs.pipecat.ai/server/services/tts/playht), [Rime](https://docs.pipecat.ai/server/services/tts/rime), [Sarvam](https://docs.pipecat.ai/server/services/tts/sarvam), [XTTS](https://docs.pipecat.ai/server/services/tts/xtts) |
|
||||||
| Speech-to-Speech | [AWS Nova Sonic](https://docs.pipecat.ai/server/services/s2s/aws), [Gemini Multimodal Live](https://docs.pipecat.ai/server/services/s2s/gemini), [OpenAI Realtime](https://docs.pipecat.ai/server/services/s2s/openai) |
|
| Speech-to-Speech | [AWS Nova Sonic](https://docs.pipecat.ai/server/services/s2s/aws), [Gemini Multimodal Live](https://docs.pipecat.ai/server/services/s2s/gemini), [OpenAI Realtime](https://docs.pipecat.ai/server/services/s2s/openai) |
|
||||||
| Transport | [Daily (WebRTC)](https://docs.pipecat.ai/server/services/transport/daily), [FastAPI Websocket](https://docs.pipecat.ai/server/services/transport/fastapi-websocket), [SmallWebRTCTransport](https://docs.pipecat.ai/server/services/transport/small-webrtc), [WebSocket Server](https://docs.pipecat.ai/server/services/transport/websocket-server), Local |
|
| Transport | [Daily (WebRTC)](https://docs.pipecat.ai/server/services/transport/daily), [FastAPI Websocket](https://docs.pipecat.ai/server/services/transport/fastapi-websocket), [SmallWebRTCTransport](https://docs.pipecat.ai/server/services/transport/small-webrtc), [WebSocket Server](https://docs.pipecat.ai/server/services/transport/websocket-server), Local |
|
||||||
| Serializers | [Plivo](https://docs.pipecat.ai/server/utilities/serializers/plivo), [Twilio](https://docs.pipecat.ai/server/utilities/serializers/twilio), [Telnyx](https://docs.pipecat.ai/server/utilities/serializers/telnyx) |
|
| Serializers | [Plivo](https://docs.pipecat.ai/server/utilities/serializers/plivo), [Twilio](https://docs.pipecat.ai/server/utilities/serializers/twilio), [Telnyx](https://docs.pipecat.ai/server/utilities/serializers/telnyx) |
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ GOOGLE_CLOUD_PROJECT_ID=...
|
|||||||
GOOGLE_TEST_CREDENTIALS=...
|
GOOGLE_TEST_CREDENTIALS=...
|
||||||
GOOGLE_VERTEX_TEST_CREDENTIALS=...
|
GOOGLE_VERTEX_TEST_CREDENTIALS=...
|
||||||
|
|
||||||
|
# Hume
|
||||||
|
HUME_API_KEY=...
|
||||||
|
|
||||||
# LMNT
|
# LMNT
|
||||||
LMNT_API_KEY=...
|
LMNT_API_KEY=...
|
||||||
LMNT_VOICE_ID=...
|
LMNT_VOICE_ID=...
|
||||||
|
|||||||
118
examples/foundational/07ae-interruptible-hume.py
Normal file
118
examples/foundational/07ae-interruptible-hume.py
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
#
|
||||||
|
# Copyright (c) 2024–2025, Daily
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: BSD 2-Clause License
|
||||||
|
#
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
from pipecat.frames.frames import LLMRunFrame
|
||||||
|
from pipecat.pipeline.pipeline import Pipeline
|
||||||
|
from pipecat.pipeline.runner import PipelineRunner
|
||||||
|
from pipecat.pipeline.task import PipelineParams, PipelineTask
|
||||||
|
from pipecat.processors.aggregators.llm_context import LLMContext
|
||||||
|
from pipecat.processors.aggregators.llm_response_universal import LLMContextAggregatorPair
|
||||||
|
from pipecat.runner.types import RunnerArguments
|
||||||
|
from pipecat.runner.utils import create_transport
|
||||||
|
from pipecat.services.deepgram.stt import DeepgramSTTService
|
||||||
|
from pipecat.services.hume.tts import HUME_SAMPLE_RATE, HumeTTSService
|
||||||
|
from pipecat.services.openai.llm import OpenAILLMService
|
||||||
|
from pipecat.transports.base_transport import BaseTransport, TransportParams
|
||||||
|
from pipecat.transports.daily.transport import DailyParams
|
||||||
|
from pipecat.transports.websocket.fastapi import FastAPIWebsocketParams
|
||||||
|
|
||||||
|
load_dotenv(override=True)
|
||||||
|
|
||||||
|
# We store functions so objects (e.g. SileroVADAnalyzer) don't get
|
||||||
|
# instantiated. The function will be called when the desired transport gets
|
||||||
|
# selected.
|
||||||
|
transport_params = {
|
||||||
|
"daily": lambda: DailyParams(
|
||||||
|
audio_in_enabled=True,
|
||||||
|
audio_out_enabled=True,
|
||||||
|
vad_analyzer=SileroVADAnalyzer(),
|
||||||
|
),
|
||||||
|
"twilio": lambda: FastAPIWebsocketParams(
|
||||||
|
audio_in_enabled=True,
|
||||||
|
audio_out_enabled=True,
|
||||||
|
vad_analyzer=SileroVADAnalyzer(),
|
||||||
|
),
|
||||||
|
"webrtc": lambda: TransportParams(),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def run_bot(transport: BaseTransport, runner_args: RunnerArguments):
|
||||||
|
logger.info(f"Starting bot")
|
||||||
|
|
||||||
|
stt = DeepgramSTTService(api_key=os.getenv("DEEPGRAM_API_KEY"))
|
||||||
|
|
||||||
|
tts = HumeTTSService(
|
||||||
|
api_key=os.getenv("HUME_API_KEY"),
|
||||||
|
# Replace with your Hume voice ID
|
||||||
|
voice_id="f898a92e-685f-43fa-985b-a46920f0650b",
|
||||||
|
)
|
||||||
|
|
||||||
|
llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"))
|
||||||
|
|
||||||
|
messages = [
|
||||||
|
{
|
||||||
|
"role": "system",
|
||||||
|
"content": "You are a helpful LLM in a WebRTC call. Your goal is to demonstrate your capabilities in a succinct way. Your output will be converted to audio so don't include special characters in your answers. Respond to what the user said in a creative and helpful way.",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
context = LLMContext(messages)
|
||||||
|
context_aggregator = LLMContextAggregatorPair(context)
|
||||||
|
|
||||||
|
pipeline = Pipeline(
|
||||||
|
[
|
||||||
|
transport.input(), # Transport user input
|
||||||
|
stt,
|
||||||
|
context_aggregator.user(), # User responses
|
||||||
|
llm, # LLM
|
||||||
|
tts, # TTS
|
||||||
|
transport.output(), # Transport bot output
|
||||||
|
context_aggregator.assistant(), # Assistant spoken responses
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
task = PipelineTask(
|
||||||
|
pipeline,
|
||||||
|
params=PipelineParams(
|
||||||
|
enable_metrics=True,
|
||||||
|
enable_usage_metrics=True,
|
||||||
|
audio_out_sample_rate=HUME_SAMPLE_RATE,
|
||||||
|
),
|
||||||
|
idle_timeout_secs=runner_args.pipeline_idle_timeout_secs,
|
||||||
|
)
|
||||||
|
|
||||||
|
@transport.event_handler("on_client_connected")
|
||||||
|
async def on_client_connected(transport, client):
|
||||||
|
logger.info(f"Client connected")
|
||||||
|
# Kick off the conversation.
|
||||||
|
messages.append({"role": "system", "content": "Please introduce yourself to the user."})
|
||||||
|
await task.queue_frames([LLMRunFrame()])
|
||||||
|
|
||||||
|
@transport.event_handler("on_client_disconnected")
|
||||||
|
async def on_client_disconnected(transport, client):
|
||||||
|
logger.info(f"Client disconnected")
|
||||||
|
await task.cancel()
|
||||||
|
|
||||||
|
runner = PipelineRunner(handle_sigint=runner_args.handle_sigint)
|
||||||
|
|
||||||
|
await runner.run(task)
|
||||||
|
|
||||||
|
|
||||||
|
async def bot(runner_args: RunnerArguments):
|
||||||
|
"""Main bot entry point compatible with Pipecat Cloud."""
|
||||||
|
transport = await create_transport(runner_args, transport_params)
|
||||||
|
|
||||||
|
await run_bot(transport, runner_args)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from pipecat.runner.run import main
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -67,6 +67,7 @@ grok = []
|
|||||||
groq = [ "groq~=0.23.0" ]
|
groq = [ "groq~=0.23.0" ]
|
||||||
gstreamer = [ "pygobject~=3.50.0" ]
|
gstreamer = [ "pygobject~=3.50.0" ]
|
||||||
heygen = [ "livekit>=1.0.13", "pipecat-ai[websockets-base]" ]
|
heygen = [ "livekit>=1.0.13", "pipecat-ai[websockets-base]" ]
|
||||||
|
hume = [ "hume>=0.11.2" ]
|
||||||
inworld = []
|
inworld = []
|
||||||
krisp = [ "pipecat-ai-krisp~=0.4.0" ]
|
krisp = [ "pipecat-ai-krisp~=0.4.0" ]
|
||||||
koala = [ "pvkoala~=2.0.3" ]
|
koala = [ "pvkoala~=2.0.3" ]
|
||||||
|
|||||||
@@ -103,6 +103,7 @@ TESTS_07 = [
|
|||||||
("07w-interruptible-fal.py", PROMPT_SIMPLE_MATH, EVAL_SIMPLE_MATH, BOT_SPEAKS_FIRST),
|
("07w-interruptible-fal.py", PROMPT_SIMPLE_MATH, EVAL_SIMPLE_MATH, BOT_SPEAKS_FIRST),
|
||||||
("07y-interruptible-minimax.py", PROMPT_SIMPLE_MATH, EVAL_SIMPLE_MATH, BOT_SPEAKS_FIRST),
|
("07y-interruptible-minimax.py", PROMPT_SIMPLE_MATH, EVAL_SIMPLE_MATH, BOT_SPEAKS_FIRST),
|
||||||
("07z-interruptible-sarvam.py", PROMPT_SIMPLE_MATH, EVAL_SIMPLE_MATH, BOT_SPEAKS_FIRST),
|
("07z-interruptible-sarvam.py", PROMPT_SIMPLE_MATH, EVAL_SIMPLE_MATH, BOT_SPEAKS_FIRST),
|
||||||
|
("07ae-interruptible-hume.py", PROMPT_SIMPLE_MATH, EVAL_SIMPLE_MATH, BOT_SPEAKS_FIRST),
|
||||||
# Needs a local XTTS docker instance running.
|
# Needs a local XTTS docker instance running.
|
||||||
# ("07i-interruptible-xtts.py", PROMPT_SIMPLE_MATH, EVAL_SIMPLE_MATH, BOT_SPEAKS_FIRST),
|
# ("07i-interruptible-xtts.py", PROMPT_SIMPLE_MATH, EVAL_SIMPLE_MATH, BOT_SPEAKS_FIRST),
|
||||||
# Needs a Krisp license.
|
# Needs a Krisp license.
|
||||||
|
|||||||
5
src/pipecat/services/hume/__init__.py
Normal file
5
src/pipecat/services/hume/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#
|
||||||
|
# Copyright (c) 2024–2025, Daily
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: BSD 2-Clause License
|
||||||
|
#
|
||||||
212
src/pipecat/services/hume/tts.py
Normal file
212
src/pipecat/services/hume/tts.py
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
# Copyright (c) 2024–2025, Daily
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: BSD 2-Clause License
|
||||||
|
|
||||||
|
"""Hume Text-to-Speech service implementation."""
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import os
|
||||||
|
from typing import Any, AsyncGenerator, Optional
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from pipecat.frames.frames import (
|
||||||
|
ErrorFrame,
|
||||||
|
Frame,
|
||||||
|
StartFrame,
|
||||||
|
TTSAudioRawFrame,
|
||||||
|
TTSStartedFrame,
|
||||||
|
TTSStoppedFrame,
|
||||||
|
)
|
||||||
|
from pipecat.services.tts_service import TTSService
|
||||||
|
from pipecat.utils.tracing.service_decorators import traced_tts
|
||||||
|
|
||||||
|
try:
|
||||||
|
from hume import AsyncHumeClient
|
||||||
|
from hume.tts import (
|
||||||
|
FormatPcm,
|
||||||
|
PostedUtterance,
|
||||||
|
PostedUtteranceVoiceWithId,
|
||||||
|
)
|
||||||
|
except ModuleNotFoundError as e: # pragma: no cover - import-time guidance
|
||||||
|
logger.error(f"Exception: {e}")
|
||||||
|
logger.error("In order to use Hume, you need to `pip install pipecat-ai[hume]`.")
|
||||||
|
raise Exception(f"Missing module: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
HUME_SAMPLE_RATE = 48_000 # Hume TTS streams at 48 kHz
|
||||||
|
|
||||||
|
|
||||||
|
class HumeTTSService(TTSService):
|
||||||
|
"""Hume Octave Text-to-Speech service.
|
||||||
|
|
||||||
|
Streams PCM audio via Hume's HTTP output streaming (JSON chunks) endpoint
|
||||||
|
using the Python SDK and emits `TTSAudioRawFrame`s suitable for Pipecat transports.
|
||||||
|
|
||||||
|
Supported features:
|
||||||
|
|
||||||
|
- Generates speech from text using Hume TTS.
|
||||||
|
- Streams PCM audio.
|
||||||
|
- Supports dynamic updates of voice and synthesis parameters at runtime.
|
||||||
|
- Provides metrics for Time To First Byte (TTFB) and TTS usage.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class InputParams(BaseModel):
|
||||||
|
"""Optional synthesis parameters for Hume TTS.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
description: Natural-language acting directions (up to 100 characters).
|
||||||
|
speed: Speaking-rate multiplier (0.5-2.0).
|
||||||
|
trailing_silence: Seconds of silence to append at the end (0-5).
|
||||||
|
"""
|
||||||
|
|
||||||
|
description: Optional[str] = None
|
||||||
|
speed: Optional[float] = None
|
||||||
|
trailing_silence: Optional[float] = None
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
api_key: Optional[str] = None,
|
||||||
|
voice_id: str,
|
||||||
|
params: Optional[InputParams] = None,
|
||||||
|
sample_rate: Optional[int] = HUME_SAMPLE_RATE,
|
||||||
|
**kwargs,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the HumeTTSService.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
api_key: Hume API key. If omitted, reads the ``HUME_API_KEY`` environment variable.
|
||||||
|
voice_id: ID of the voice to use (ID-only; names are not supported here).
|
||||||
|
params: Optional synthesis controls (acting instructions, speed, trailing silence).
|
||||||
|
sample_rate: Output sample rate for emitted PCM frames. Defaults to 48_000 (Hume).
|
||||||
|
**kwargs: Additional arguments passed to the parent class.
|
||||||
|
"""
|
||||||
|
api_key = api_key or os.getenv("HUME_API_KEY")
|
||||||
|
if not api_key:
|
||||||
|
raise ValueError("HumeTTSService requires an API key (env HUME_API_KEY or api_key=)")
|
||||||
|
|
||||||
|
if sample_rate != HUME_SAMPLE_RATE:
|
||||||
|
logger.warning(
|
||||||
|
f"Hume TTS streams at {HUME_SAMPLE_RATE} Hz; configured sample_rate={sample_rate}"
|
||||||
|
)
|
||||||
|
|
||||||
|
super().__init__(sample_rate=sample_rate, **kwargs)
|
||||||
|
|
||||||
|
self._client = AsyncHumeClient(api_key=api_key)
|
||||||
|
self._params = params or HumeTTSService.InputParams()
|
||||||
|
|
||||||
|
# Store voice in the base class (mirrors other services)
|
||||||
|
self.set_voice(voice_id)
|
||||||
|
|
||||||
|
self._audio_bytes = b""
|
||||||
|
|
||||||
|
def can_generate_metrics(self) -> bool:
|
||||||
|
"""Can generate metrics.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if metrics can be generated, False otherwise.
|
||||||
|
"""
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def start(self, frame: StartFrame) -> None:
|
||||||
|
"""Start the service.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frame: The start frame.
|
||||||
|
"""
|
||||||
|
await super().start(frame)
|
||||||
|
|
||||||
|
async def update_setting(self, key: str, value: Any) -> None:
|
||||||
|
"""Runtime updates via `TTSUpdateSettingsFrame`.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
key: The name of the setting to update. Recognized keys are:
|
||||||
|
- "voice_id"
|
||||||
|
- "description"
|
||||||
|
- "speed"
|
||||||
|
- "trailing_silence"
|
||||||
|
value: The new value for the setting.
|
||||||
|
"""
|
||||||
|
key_l = (key or "").lower()
|
||||||
|
|
||||||
|
if key_l == "voice_id":
|
||||||
|
self.set_voice(str(value))
|
||||||
|
logger.info(f"HumeTTSService voice_id set to: {self.voice}")
|
||||||
|
elif key_l == "description":
|
||||||
|
self._params.description = None if value is None else str(value)
|
||||||
|
elif key_l == "speed":
|
||||||
|
self._params.speed = None if value is None else float(value)
|
||||||
|
elif key_l == "trailing_silence":
|
||||||
|
self._params.trailing_silence = None if value is None else float(value)
|
||||||
|
else:
|
||||||
|
# Defer unknown keys to the base class
|
||||||
|
await super().update_setting(key, value)
|
||||||
|
|
||||||
|
@traced_tts
|
||||||
|
async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
|
||||||
|
"""Generate speech from text using Hume TTS.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
text: The text to be synthesized.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
An async generator that yields `Frame` objects, including
|
||||||
|
`TTSStartedFrame`, `TTSAudioRawFrame`, `ErrorFrame`, and
|
||||||
|
`TTSStoppedFrame`.
|
||||||
|
"""
|
||||||
|
logger.debug(f"{self}: Generating Hume TTS: [{text}]")
|
||||||
|
|
||||||
|
# Build the request payload
|
||||||
|
utterance_kwargs: dict[str, Any] = {
|
||||||
|
"text": text,
|
||||||
|
"voice": PostedUtteranceVoiceWithId(id=self._voice_id),
|
||||||
|
}
|
||||||
|
if self._params.description is not None:
|
||||||
|
utterance_kwargs["description"] = self._params.description
|
||||||
|
if self._params.speed is not None:
|
||||||
|
utterance_kwargs["speed"] = self._params.speed
|
||||||
|
if self._params.trailing_silence is not None:
|
||||||
|
utterance_kwargs["trailing_silence"] = self._params.trailing_silence
|
||||||
|
|
||||||
|
utterance = PostedUtterance(**utterance_kwargs)
|
||||||
|
|
||||||
|
# Request raw PCM chunks in the streaming JSON
|
||||||
|
pcm_fmt = FormatPcm(type="pcm")
|
||||||
|
|
||||||
|
await self.start_ttfb_metrics()
|
||||||
|
await self.start_tts_usage_metrics(text)
|
||||||
|
yield TTSStartedFrame()
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Instant mode is always enabled here (not user-configurable)
|
||||||
|
# Hume emits mono PCM at 48 kHz; downstream can resample if needed.
|
||||||
|
# We buffer audio bytes before sending to prevent glitches.
|
||||||
|
self._audio_bytes = b""
|
||||||
|
async for chunk in self._client.tts.synthesize_json_streaming(
|
||||||
|
utterances=[utterance],
|
||||||
|
format=pcm_fmt,
|
||||||
|
instant_mode=True,
|
||||||
|
version="2",
|
||||||
|
):
|
||||||
|
audio_b64 = getattr(chunk, "audio", None)
|
||||||
|
if not audio_b64:
|
||||||
|
continue
|
||||||
|
|
||||||
|
pcm_bytes = base64.b64decode(audio_b64)
|
||||||
|
self._audio_bytes += pcm_bytes
|
||||||
|
|
||||||
|
# Buffer audio until we have enough to avoid glitches
|
||||||
|
if len(self._audio_bytes) < self.chunk_size:
|
||||||
|
continue
|
||||||
|
|
||||||
|
yield TTSAudioRawFrame(self._audio_bytes, self.sample_rate, 1)
|
||||||
|
self._audio_bytes = b""
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(f"{self} error generating TTS: {e}")
|
||||||
|
yield ErrorFrame(error=str(e))
|
||||||
|
finally:
|
||||||
|
# Ensure TTFB timer is stopped even on early failures
|
||||||
|
yield TTSStoppedFrame()
|
||||||
Reference in New Issue
Block a user