From e721c2086cf4759384becc3af4aad838303618c0 Mon Sep 17 00:00:00 2001 From: James Hush Date: Fri, 18 Apr 2025 14:14:22 +0800 Subject: [PATCH] Add banana processor --- examples/foundational/01-say-one-thing.py | 3 ++- .../foundational/06-listen-and-respond.py | 25 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/examples/foundational/01-say-one-thing.py b/examples/foundational/01-say-one-thing.py index 14bbe6a44..c07f3cd40 100644 --- a/examples/foundational/01-say-one-thing.py +++ b/examples/foundational/01-say-one-thing.py @@ -9,10 +9,11 @@ import os from dotenv import load_dotenv from loguru import logger -from pipecat.frames.frames import EndFrame, TTSSpeakFrame +from pipecat.frames.frames import EndFrame, TranscriptionFrame, TTSSpeakFrame from pipecat.pipeline.pipeline import Pipeline from pipecat.pipeline.runner import PipelineRunner from pipecat.pipeline.task import PipelineTask +from pipecat.processors.frame_processor import FrameProcessor from pipecat.services.cartesia.tts import CartesiaTTSService from pipecat.transports.base_transport import TransportParams from pipecat.transports.network.small_webrtc import SmallWebRTCTransport diff --git a/examples/foundational/06-listen-and-respond.py b/examples/foundational/06-listen-and-respond.py index c41c39748..53c235e77 100644 --- a/examples/foundational/06-listen-and-respond.py +++ b/examples/foundational/06-listen-and-respond.py @@ -10,7 +10,7 @@ from dotenv import load_dotenv from loguru import logger from pipecat.audio.vad.silero import SileroVADAnalyzer -from pipecat.frames.frames import Frame, MetricsFrame +from pipecat.frames.frames import Frame, MetricsFrame, TranscriptionFrame, TTSSpeakFrame from pipecat.metrics.metrics import ( LLMUsageMetricsData, ProcessingMetricsData, @@ -32,6 +32,26 @@ from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection load_dotenv(override=True) +# Custom processor that prints a message if it receives a TranscriptionFrame that says "banana" +class BananaProcessor(FrameProcessor): + """A custom processor that listens for transcription frames containing the word 'banana'.""" + + def __init__(self): + super().__init__() + + async def process_frame(self, frame: Frame, direction: FrameDirection): + # Ensure the super method is called first + await super().process_frame(frame, direction) + + if isinstance(frame, TranscriptionFrame): + logger.debug(f"Received transcription frame: {frame.text}") + if "banana" in frame.text.lower(): + logger.info("---- Received 'banana' in transcription frame") + + # Push the frame after processing + await self.push_frame(frame) + + class MetricsLogger(FrameProcessor): async def process_frame(self, frame: Frame, direction: FrameDirection): await super().process_frame(frame, direction) @@ -87,10 +107,13 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection): context = OpenAILLMContext(messages) context_aggregator = llm.create_context_aggregator(context) + hello = BananaProcessor() + pipeline = Pipeline( [ transport.input(), stt, + hello, context_aggregator.user(), llm, tts,