Update examples to new format

This commit is contained in:
Matej Marinko
2025-07-09 09:28:43 +02:00
parent 8daaea5969
commit 3cdaeb719a
3 changed files with 50 additions and 46 deletions

View File

@@ -20,33 +20,36 @@ from pipecat.services.openai.llm import OpenAILLMService
from pipecat.services.soniox.config import SonioxInputParams from pipecat.services.soniox.config import SonioxInputParams
from pipecat.services.soniox.stt import SonioxSTTService from pipecat.services.soniox.stt import SonioxSTTService
from pipecat.transcriptions.language import Language 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.small_webrtc import SmallWebRTCTransport from pipecat.transports.network.fastapi_websocket import FastAPIWebsocketParams
from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection from pipecat.transports.services.daily import DailyParams
load_dotenv(override=True) 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") 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( stt = SonioxSTTService(
api_key=os.getenv("SONIOX_API_KEY"), 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( tts = CartesiaTTSService(
@@ -77,14 +80,11 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespac
context_aggregator.assistant(), # Assistant spoken responses context_aggregator.assistant(), # Assistant spoken responses
] ]
) )
task = PipelineTask( task = PipelineTask(
pipeline, pipeline,
params=PipelineParams( params=PipelineParams(
allow_interruptions=True,
enable_metrics=True, enable_metrics=True,
enable_usage_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") @transport.event_handler("on_client_disconnected")
async def on_client_disconnected(transport, client): async def on_client_disconnected(transport, client):
logger.info(f"Client disconnected") 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() await task.cancel()
runner = PipelineRunner(handle_sigint=False) runner = PipelineRunner(handle_sigint=handle_sigint)
await runner.run(task) await runner.run(task)
if __name__ == "__main__": if __name__ == "__main__":
from run import main from pipecat.examples.run import main
main() main(run_example, transport_params=transport_params)

View File

@@ -10,6 +10,7 @@ import os
from dotenv import load_dotenv from dotenv import load_dotenv
from loguru import logger from loguru import logger
from pipecat.audio.vad.silero import SileroVADAnalyzer
from pipecat.frames.frames import Frame, TranscriptionFrame from pipecat.frames.frames import Frame, TranscriptionFrame
from pipecat.pipeline.pipeline import Pipeline from pipecat.pipeline.pipeline import Pipeline
from pipecat.pipeline.runner import PipelineRunner 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.config import SonioxInputParams
from pipecat.services.soniox.stt import SonioxSTTService from pipecat.services.soniox.stt import SonioxSTTService
from pipecat.transcriptions.language import Language 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.small_webrtc import SmallWebRTCTransport
from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection
from pipecat.transports.services.daily import DailyParams
load_dotenv(override=True) load_dotenv(override=True)
@@ -33,22 +36,27 @@ class TranscriptionLogger(FrameProcessor):
print(f"Transcription: {frame.text}") print(f"Transcription: {frame.text}")
async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespace): transport_params = {
logger.info(f"Starting bot") "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, async def run_example(transport: BaseTransport, _: argparse.Namespace, handle_sigint: bool):
params=TransportParams(audio_in_enabled=True), logger.info(f"Starting bot")
)
stt = SonioxSTTService( stt = SonioxSTTService(
api_key=os.getenv("SONIOX_API_KEY"), 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() tl = TranscriptionLogger()
@@ -72,6 +80,6 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespac
if __name__ == "__main__": if __name__ == "__main__":
from run import main from pipecat.examples.run import main
main() main(run_example, transport_params=transport_params)

View File

@@ -88,7 +88,7 @@ class SonioxSTTService(STTService):
url: str = "wss://stt-rt.soniox.com/transcribe-websocket", url: str = "wss://stt-rt.soniox.com/transcribe-websocket",
sample_rate: Optional[int] = None, sample_rate: Optional[int] = None,
params: Optional[SonioxInputParams] = None, params: Optional[SonioxInputParams] = None,
enable_vad: bool = True, enable_vad: bool = False,
auto_finalize_delay_ms: Optional[int] = 3000, auto_finalize_delay_ms: Optional[int] = 3000,
**kwargs, **kwargs,
): ):
@@ -108,7 +108,7 @@ class SonioxSTTService(STTService):
to `None`, the auto finalize feature is disabled. to `None`, the auto finalize feature is disabled.
**kwargs: Additional arguments passed to the STTService. **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) super().__init__(sample_rate=sample_rate, **kwargs)
params = params or SonioxInputParams() params = params or SonioxInputParams()