examples(foundational): support multiple transports

This commit is contained in:
Aleix Conchillo Flaqué
2025-05-24 21:00:24 -07:00
parent ecf878e14d
commit 2cdfaa0a82
128 changed files with 3282 additions and 2716 deletions

View File

@@ -10,6 +10,7 @@ from typing import Optional
from dotenv import load_dotenv
from loguru import logger
from run import get_transport_client_id, maybe_capture_participant_video
from pipecat.audio.vad.silero import SileroVADAnalyzer
from pipecat.frames.frames import Frame, TextFrame, UserImageRequestFrame
@@ -22,9 +23,8 @@ from pipecat.processors.frame_processor import FrameDirection, FrameProcessor
from pipecat.services.cartesia.tts import CartesiaTTSService
from pipecat.services.deepgram.stt import DeepgramSTTService
from pipecat.services.moondream.vision import MoondreamService
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.services.daily import DailyParams
load_dotenv(override=True)
@@ -47,21 +47,27 @@ class UserImageRequester(FrameProcessor):
await self.push_frame(frame, direction)
async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespace):
# Get WebRTC peer connection ID
webrtc_peer_id = webrtc_connection.pc_id
# 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,
video_in_enabled=True,
vad_analyzer=SileroVADAnalyzer(),
),
"webrtc": lambda: TransportParams(
audio_in_enabled=True,
audio_out_enabled=True,
video_in_enabled=True,
vad_analyzer=SileroVADAnalyzer(),
),
}
logger.info(f"Starting bot with peer_id: {webrtc_peer_id}")
transport = SmallWebRTCTransport(
webrtc_connection=webrtc_connection,
params=TransportParams(
audio_in_enabled=True,
audio_out_enabled=True,
video_in_enabled=True,
vad_analyzer=SileroVADAnalyzer(),
),
)
async def run_example(transport: BaseTransport, _: argparse.Namespace):
logger.info(f"Starting bot")
user_response = UserResponseAggregator()
@@ -99,15 +105,19 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespac
async def on_client_connected(transport, client):
logger.info(f"Client connected: {client}")
# Welcome message
await tts.say("Hi there! Feel free to ask me what I see.")
await maybe_capture_participant_video(transport, client)
# Set the participant ID in the image requester
image_requester.set_participant_id(webrtc_peer_id)
client_id = get_transport_client_id(transport, client)
image_requester.set_participant_id(client_id)
# Welcome message
await tts.say("Hi there! Feel free to ask me what I see.")
@transport.event_handler("on_client_disconnected")
async def on_client_disconnected(transport, client):
logger.info(f"Client disconnected")
await task.cancel()
@transport.event_handler("on_client_closed")
async def on_client_closed(transport, client):
@@ -122,4 +132,4 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection, _: argparse.Namespac
if __name__ == "__main__":
from run import main
main()
main(run_example, transport_params=transport_params)