README: update example

This commit is contained in:
Aleix Conchillo Flaqué
2024-10-18 11:16:40 -07:00
parent 4550545528
commit 8c006c24a3

View File

@@ -51,10 +51,7 @@ Your project may or may not need these, so they're made available as optional re
Here is a very basic Pipecat bot that greets a user when they join a real-time session. We'll use [Daily](https://daily.co) for real-time media transport, and [Cartesia](https://cartesia.ai/) for text-to-speech. Here is a very basic Pipecat bot that greets a user when they join a real-time session. We'll use [Daily](https://daily.co) for real-time media transport, and [Cartesia](https://cartesia.ai/) for text-to-speech.
```python ```python
#app.py
import asyncio import asyncio
import aiohttp
from pipecat.frames.frames import EndFrame, TextFrame from pipecat.frames.frames import EndFrame, TextFrame
from pipecat.pipeline.pipeline import Pipeline from pipecat.pipeline.pipeline import Pipeline
@@ -64,7 +61,6 @@ from pipecat.services.cartesia import CartesiaTTSService
from pipecat.transports.services.daily import DailyParams, DailyTransport from pipecat.transports.services.daily import DailyParams, DailyTransport
async def main(): async def main():
async with aiohttp.ClientSession() as session:
# Use Daily as a real-time media transport (WebRTC) # Use Daily as a real-time media transport (WebRTC)
transport = DailyTransport( transport = DailyTransport(
room_url=..., room_url=...,
@@ -89,11 +85,16 @@ async def main():
# Register an event handler to play audio when a # Register an event handler to play audio when a
# participant joins the transport WebRTC session # participant joins the transport WebRTC session
@transport.event_handler("on_participant_joined") @transport.event_handler("on_first_participant_joined")
async def on_new_participant_joined(transport, participant): async def on_first_participant_joined(transport, participant):
participant_name = participant["info"]["userName"] or '' participant_name = participant.get("info", {}).get("userName", "")
# Queue a TextFrame that will get spoken by the TTS service (Cartesia) # Queue a TextFrame that will get spoken by the TTS service (Cartesia)
await task.queue_frames([TextFrame(f"Hello there, {participant_name}!"), EndFrame()]) await task.queue_frame(TextFrame(f"Hello there, {participant_name}!"))
# Register an event handler to exit the application when the user leaves.
@transport.event_handler("on_participant_left")
async def on_participant_left(transport, participant, reason):
await task.queue_frame(EndFrame())
# Run the pipeline task # Run the pipeline task
await runner.run(task) await runner.run(task)