From 3cdaeb719a0c8c480e9fab7dd3d5876f351cdcde Mon Sep 17 00:00:00 2001 From: Matej Marinko Date: Wed, 9 Jul 2025 09:28:43 +0200 Subject: [PATCH] Update examples to new format --- .../foundational/07za-interruptible-soniox.py | 54 +++++++++---------- ...ription.py => 13i-soniox-transcription.py} | 38 +++++++------ src/pipecat/services/soniox/stt.py | 4 +- 3 files changed, 50 insertions(+), 46 deletions(-) rename examples/foundational/{13f-soniox-transcription.py => 13i-soniox-transcription.py} (67%) diff --git a/examples/foundational/07za-interruptible-soniox.py b/examples/foundational/07za-interruptible-soniox.py index f3b3487d7..d909d5e1d 100644 --- a/examples/foundational/07za-interruptible-soniox.py +++ b/examples/foundational/07za-interruptible-soniox.py @@ -20,33 +20,36 @@ from pipecat.services.openai.llm import OpenAILLMService from pipecat.services.soniox.config import SonioxInputParams from pipecat.services.soniox.stt import SonioxSTTService from pipecat.transcriptions.language import Language -from pipecat.transports.base_transport import TransportParams -from pipecat.transports.network.small_webrtc import SmallWebRTCTransport -from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection +from pipecat.transports.base_transport import BaseTransport, TransportParams +from pipecat.transports.network.fastapi_websocket import FastAPIWebsocketParams +from pipecat.transports.services.daily import DailyParams load_dotenv(override=True) +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( + audio_in_enabled=True, + audio_out_enabled=True, + vad_analyzer=SileroVADAnalyzer(), + ), +} -async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespace): + +async def run_example(transport: BaseTransport, _: argparse.Namespace, handle_sigint: bool): logger.info(f"Starting bot") - transport = SmallWebRTCTransport( - webrtc_connection=webrtc_connection, - params=TransportParams( - audio_in_enabled=True, - audio_out_enabled=True, - vad_analyzer=SileroVADAnalyzer(), - ), - ) - stt = SonioxSTTService( api_key=os.getenv("SONIOX_API_KEY"), - params=SonioxInputParams( - # Add language hints to improve transcription accuracy. Variants are ignored. - # For example "en-GB" will be treated same as "en". - # List of supported languages: https://soniox.com/docs/speech-to-text/core-concepts/supported-languages - language_hints=[Language.EN, Language.ES, Language.JA, Language.ZH], - ), ) tts = CartesiaTTSService( @@ -77,14 +80,11 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespac context_aggregator.assistant(), # Assistant spoken responses ] ) - task = PipelineTask( pipeline, params=PipelineParams( - allow_interruptions=True, enable_metrics=True, enable_usage_metrics=True, - report_only_initial_ttfb=True, ), ) @@ -98,18 +98,14 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespac @transport.event_handler("on_client_disconnected") async def on_client_disconnected(transport, client): logger.info(f"Client disconnected") - - @transport.event_handler("on_client_closed") - async def on_client_closed(transport, client): - logger.info(f"Client closed connection") await task.cancel() - runner = PipelineRunner(handle_sigint=False) + runner = PipelineRunner(handle_sigint=handle_sigint) await runner.run(task) if __name__ == "__main__": - from run import main + from pipecat.examples.run import main - main() + main(run_example, transport_params=transport_params) diff --git a/examples/foundational/13f-soniox-transcription.py b/examples/foundational/13i-soniox-transcription.py similarity index 67% rename from examples/foundational/13f-soniox-transcription.py rename to examples/foundational/13i-soniox-transcription.py index 6e9d356f3..12760846c 100644 --- a/examples/foundational/13f-soniox-transcription.py +++ b/examples/foundational/13i-soniox-transcription.py @@ -10,6 +10,7 @@ import os from dotenv import load_dotenv from loguru import logger +from pipecat.audio.vad.silero import SileroVADAnalyzer from pipecat.frames.frames import Frame, TranscriptionFrame from pipecat.pipeline.pipeline import Pipeline from pipecat.pipeline.runner import PipelineRunner @@ -18,9 +19,11 @@ from pipecat.processors.frame_processor import FrameDirection, FrameProcessor from pipecat.services.soniox.config import SonioxInputParams from pipecat.services.soniox.stt import SonioxSTTService from pipecat.transcriptions.language import Language -from pipecat.transports.base_transport import TransportParams +from pipecat.transports.base_transport import BaseTransport, TransportParams +from pipecat.transports.network.fastapi_websocket import FastAPIWebsocketParams from pipecat.transports.network.small_webrtc import SmallWebRTCTransport from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection +from pipecat.transports.services.daily import DailyParams load_dotenv(override=True) @@ -33,22 +36,27 @@ class TranscriptionLogger(FrameProcessor): print(f"Transcription: {frame.text}") -async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespace): - logger.info(f"Starting bot") +transport_params = { + "daily": lambda: DailyParams( + audio_in_enabled=True, + vad_analyzer=SileroVADAnalyzer(), + ), + "twilio": lambda: FastAPIWebsocketParams( + audio_in_enabled=True, + vad_analyzer=SileroVADAnalyzer(), + ), + "webrtc": lambda: TransportParams( + audio_in_enabled=True, + vad_analyzer=SileroVADAnalyzer(), + ), +} - transport = SmallWebRTCTransport( - webrtc_connection=webrtc_connection, - params=TransportParams(audio_in_enabled=True), - ) + +async def run_example(transport: BaseTransport, _: argparse.Namespace, handle_sigint: bool): + logger.info(f"Starting bot") stt = SonioxSTTService( api_key=os.getenv("SONIOX_API_KEY"), - params=SonioxInputParams( - # Add language hints to improve transcription accuracy. Variants are ignored. - # For example "en-GB" will be treated same as "en". - # List of supported languages: https://soniox.com/docs/speech-to-text/core-concepts/supported-languages - language_hints=[Language.EN, Language.ES, Language.JA, Language.ZH], - ), ) tl = TranscriptionLogger() @@ -72,6 +80,6 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespac if __name__ == "__main__": - from run import main + from pipecat.examples.run import main - main() + main(run_example, transport_params=transport_params) diff --git a/src/pipecat/services/soniox/stt.py b/src/pipecat/services/soniox/stt.py index e598422a9..85b755d7e 100644 --- a/src/pipecat/services/soniox/stt.py +++ b/src/pipecat/services/soniox/stt.py @@ -88,7 +88,7 @@ class SonioxSTTService(STTService): url: str = "wss://stt-rt.soniox.com/transcribe-websocket", sample_rate: Optional[int] = None, params: Optional[SonioxInputParams] = None, - enable_vad: bool = True, + enable_vad: bool = False, auto_finalize_delay_ms: Optional[int] = 3000, **kwargs, ): @@ -108,7 +108,7 @@ class SonioxSTTService(STTService): to `None`, the auto finalize feature is disabled. **kwargs: Additional arguments passed to the STTService. """ - sample_rate = sample_rate or (params.sample_rate if params.sample_rate else None) + sample_rate = sample_rate or (params.sample_rate if params and params.sample_rate else None) super().__init__(sample_rate=sample_rate, **kwargs) params = params or SonioxInputParams()