Send message to client

This commit is contained in:
James Hush
2025-05-25 15:17:05 +08:00
parent d4de9cbf34
commit b71b6fa487

View File

@@ -26,6 +26,12 @@ from pipecat.pipeline.runner import PipelineRunner
from pipecat.pipeline.task import PipelineTask from pipecat.pipeline.task import PipelineTask
from pipecat.processors.aggregators.vision_image_frame import VisionImageFrameAggregator from pipecat.processors.aggregators.vision_image_frame import VisionImageFrameAggregator
from pipecat.processors.frame_processor import FrameDirection, FrameProcessor from pipecat.processors.frame_processor import FrameDirection, FrameProcessor
from pipecat.processors.frameworks.rtvi import (
RTVIConfig,
RTVIObserver,
RTVIProcessor,
RTVIServerMessageFrame,
)
from pipecat.processors.gstreamer.pipeline_source import GStreamerPipelineSource from pipecat.processors.gstreamer.pipeline_source import GStreamerPipelineSource
from pipecat.services.moondream.vision import MoondreamService from pipecat.services.moondream.vision import MoondreamService
from pipecat.transports.base_input import BaseInputTransport from pipecat.transports.base_input import BaseInputTransport
@@ -38,8 +44,9 @@ load_dotenv(override=True)
class AlertProcessor(FrameProcessor): class AlertProcessor(FrameProcessor):
def __init__(self): def __init__(self, connection: SmallWebRTCConnection):
super().__init__() super().__init__()
self._connection = connection
async def process_frame(self, frame: Frame, direction: FrameDirection): async def process_frame(self, frame: Frame, direction: FrameDirection):
await super().process_frame(frame, direction) await super().process_frame(frame, direction)
@@ -47,11 +54,8 @@ class AlertProcessor(FrameProcessor):
if isinstance(frame, TextFrame): if isinstance(frame, TextFrame):
logger.info(f"Alert Processor received text: {frame.text}") logger.info(f"Alert Processor received text: {frame.text}")
text = frame.text.strip().upper() text = frame.text.strip().upper()
if text == "YES": message_frame = RTVIServerMessageFrame(data=text)
# SEND AN EMAIL HERE await self.push_frame(message_frame)
logger.info("Alert: YES")
else:
logger.info("Alert: NO")
await self.push_frame(frame, direction) await self.push_frame(frame, direction)
@@ -102,16 +106,19 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection, args: argparse.Names
), ),
) )
rtvi = RTVIProcessor(config=RTVIConfig(config=[]))
# If you run into weird description, try with use_cpu=True # If you run into weird description, try with use_cpu=True
moondream = MoondreamService() moondream = MoondreamService()
ir = UserImageRequester() ir = UserImageRequester()
va = VisionImageFrameAggregator() va = VisionImageFrameAggregator()
alert = AlertProcessor() alert = AlertProcessor(connection=webrtc_connection)
pipeline = Pipeline( pipeline = Pipeline(
[ [
gst, # GStreamer file source gst, # GStreamer file source
rtvi,
ir, ir,
# debug, # debug,
va, va,
@@ -124,6 +131,7 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection, args: argparse.Names
task = PipelineTask( task = PipelineTask(
pipeline, pipeline,
observers=[ observers=[
RTVIObserver(rtvi),
DebugLogObserver( DebugLogObserver(
frame_types={ frame_types={
# TextFrame: None, # TextFrame: None,
@@ -135,18 +143,15 @@ async def run_bot(webrtc_connection: SmallWebRTCConnection, args: argparse.Names
], ],
) )
@rtvi.event_handler("on_client_ready")
async def on_client_ready(rtvi):
logger.info(f"Bot ready: {rtvi}")
await rtvi.set_bot_ready()
@transport.event_handler("on_client_connected") @transport.event_handler("on_client_connected")
async def on_client_connected(transport, client): async def on_client_connected(transport, client):
logger.info(f"Client connected: {client}") logger.info(f"Client connected: {client}")
await task.queue_frames(
[
TextFrame(
"Are there people in the bottom right corner of the image? Only answer with YES or NO."
)
]
)
runner = PipelineRunner(handle_sigint=False) runner = PipelineRunner(handle_sigint=False)
await runner.run(task) await runner.run(task)