From b81323d6765c997d33d17c30b8930894ba3f533b Mon Sep 17 00:00:00 2001 From: Mark Backman Date: Fri, 17 Jan 2025 20:11:16 -0500 Subject: [PATCH] Code review fixes + docstrings --- .../07d-interruptible-elevenlabs-http.py | 104 ------------------ src/pipecat/services/elevenlabs.py | 39 +++---- 2 files changed, 17 insertions(+), 126 deletions(-) delete mode 100644 examples/foundational/07d-interruptible-elevenlabs-http.py diff --git a/examples/foundational/07d-interruptible-elevenlabs-http.py b/examples/foundational/07d-interruptible-elevenlabs-http.py deleted file mode 100644 index 4b8aefd98..000000000 --- a/examples/foundational/07d-interruptible-elevenlabs-http.py +++ /dev/null @@ -1,104 +0,0 @@ -# -# Copyright (c) 2024–2025, Daily -# -# SPDX-License-Identifier: BSD 2-Clause License -# - -import asyncio -import os -import sys - -import aiohttp -from dotenv import load_dotenv -from loguru import logger -from runner import configure - -from pipecat.audio.vad.silero import SileroVADAnalyzer -from pipecat.frames.frames import EndFrame -from pipecat.pipeline.pipeline import Pipeline -from pipecat.pipeline.runner import PipelineRunner -from pipecat.pipeline.task import PipelineParams, PipelineTask -from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext -from pipecat.services.elevenlabs import ElevenLabsHttpTTSService -from pipecat.services.openai import OpenAILLMService -from pipecat.transports.services.daily import DailyParams, DailyTransport - -load_dotenv(override=True) - -logger.remove(0) -logger.add(sys.stderr, level="DEBUG") - - -async def main(): - async with aiohttp.ClientSession() as session: - (room_url, token) = await configure(session) - - transport = DailyTransport( - room_url, - token, - "Respond bot", - DailyParams( - audio_out_enabled=True, - transcription_enabled=True, - vad_enabled=True, - vad_analyzer=SileroVADAnalyzer(), - ), - ) - - tts = ElevenLabsHttpTTSService( - api_key=os.getenv("ELEVENLABS_API_KEY", ""), - voice_id=os.getenv("ELEVENLABS_VOICE_ID", ""), - # params=ElevenLabsHttpTTSService.InputParams(language="en"), - ) - - llm = OpenAILLMService(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o") - - 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 = OpenAILLMContext(messages) - context_aggregator = llm.create_context_aggregator(context) - - pipeline = Pipeline( - [ - transport.input(), # Transport user input - context_aggregator.user(), # User responses - llm, # LLM - tts, # TTS - transport.output(), # Transport bot output - context_aggregator.assistant(), # Assistant spoken responses - ] - ) - - task = PipelineTask( - pipeline, - PipelineParams( - allow_interruptions=True, - enable_metrics=True, - enable_usage_metrics=True, - report_only_initial_ttfb=True, - ), - ) - - @transport.event_handler("on_first_participant_joined") - async def on_first_participant_joined(transport, participant): - await transport.capture_participant_transcription(participant["id"]) - # Kick off the conversation. - messages.append({"role": "system", "content": "Please introduce yourself to the user."}) - await task.queue_frames([context_aggregator.user().get_context_frame()]) - - @transport.event_handler("on_participant_left") - async def on_participant_left(transport, participant, reason): - await task.queue_frame(EndFrame()) - - runner = PipelineRunner() - - await runner.run(task) - - -if __name__ == "__main__": - asyncio.run(main()) diff --git a/src/pipecat/services/elevenlabs.py b/src/pipecat/services/elevenlabs.py index 0ad542821..188c5a9c9 100644 --- a/src/pipecat/services/elevenlabs.py +++ b/src/pipecat/services/elevenlabs.py @@ -430,6 +430,7 @@ class ElevenLabsHttpTTSService(TTSService): Args: api_key: ElevenLabs API key voice_id: ID of the voice to use + aiohttp_session: aiohttp ClientSession model: Model ID (default: "eleven_flash_v2_5" for low latency) base_url: API base URL output_format: Audio output format (PCM) @@ -449,20 +450,20 @@ class ElevenLabsHttpTTSService(TTSService): *, api_key: str, voice_id: str, + aiohttp_session: aiohttp.ClientSession, model: str = "eleven_flash_v2_5", base_url: str = "https://api.elevenlabs.io", output_format: ElevenLabsOutputFormat = "pcm_24000", params: InputParams = InputParams(), **kwargs, ): - sample_rate = sample_rate_from_output_format(output_format) - super().__init__(sample_rate=sample_rate, **kwargs) + super().__init__(sample_rate=sample_rate_from_output_format(output_format), **kwargs) self._api_key = api_key self._base_url = base_url self._output_format = output_format self._params = params - self._session: Optional[aiohttp.ClientSession] = None + self._session = aiohttp_session self._settings = { "sample_rate": sample_rate_from_output_format(output_format), @@ -484,6 +485,11 @@ class ElevenLabsHttpTTSService(TTSService): return True def _set_voice_settings(self) -> Optional[Dict[str, Union[float, bool]]]: + """Configure voice settings if stability and similarity_boost are provided. + + Returns: + Dictionary of voice settings or None if required parameters are missing. + """ voice_settings: Dict[str, Union[float, bool]] = {} if ( self._settings["stability"] is not None @@ -507,27 +513,16 @@ class ElevenLabsHttpTTSService(TTSService): return voice_settings or None - async def start(self, frame: StartFrame): - await super().start(frame) - self._session = aiohttp.ClientSession() - - async def stop(self, frame: EndFrame): - await super().stop(frame) - if self._session: - await self._session.close() - self._session = None - - async def cancel(self, frame: CancelFrame): - await super().cancel(frame) - if self._session: - await self._session.close() - self._session = None - async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]: - logger.debug(f"Generating TTS: [{text}]") + """Generate speech from text using ElevenLabs streaming API. - if not self._session: - self._session = aiohttp.ClientSession() + Args: + text: The text to convert to speech + + Yields: + Frames containing audio data and status information + """ + logger.debug(f"Generating TTS: [{text}]") url = f"{self._base_url}/v1/text-to-speech/{self._voice_id}/stream"