From e38ca3e4553ddc52d3a13d2d5b2232a0d7c6142d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Wed, 29 May 2024 23:48:39 -0700 Subject: [PATCH] serializers(protobuf): support id and name fields --- .../websocket-server/frames.proto | 30 +++++++++++-------- src/pipecat/frames/frames.proto | 30 +++++++++++-------- src/pipecat/frames/protobufs/frames_pb2.py | 16 +++++----- src/pipecat/serializers/protobuf.py | 23 ++++++++++++-- 4 files changed, 65 insertions(+), 34 deletions(-) diff --git a/examples/foundational/websocket-server/frames.proto b/examples/foundational/websocket-server/frames.proto index b6ff1a4d3..5c5d81d4d 100644 --- a/examples/foundational/websocket-server/frames.proto +++ b/examples/foundational/websocket-server/frames.proto @@ -13,25 +13,31 @@ syntax = "proto3"; package pipecat; message TextFrame { - string text = 1; + uint64 id = 1; + string name = 2; + string text = 3; } message AudioRawFrame { - bytes audio = 1; - uint32 sample_rate = 2; - uint32 num_channels = 3; + uint64 id = 1; + string name = 2; + bytes audio = 3; + uint32 sample_rate = 4; + uint32 num_channels = 5; } message TranscriptionFrame { - string text = 1; - string user_id = 2; - string timestamp = 3; + uint64 id = 1; + string name = 2; + string text = 3; + string user_id = 4; + string timestamp = 5; } message Frame { - oneof frame { - TextFrame text = 1; - AudioRawFrame audio = 2; - TranscriptionFrame transcription = 3; - } + oneof frame { + TextFrame text = 1; + AudioRawFrame audio = 2; + TranscriptionFrame transcription = 3; + } } diff --git a/src/pipecat/frames/frames.proto b/src/pipecat/frames/frames.proto index b6ff1a4d3..5c5d81d4d 100644 --- a/src/pipecat/frames/frames.proto +++ b/src/pipecat/frames/frames.proto @@ -13,25 +13,31 @@ syntax = "proto3"; package pipecat; message TextFrame { - string text = 1; + uint64 id = 1; + string name = 2; + string text = 3; } message AudioRawFrame { - bytes audio = 1; - uint32 sample_rate = 2; - uint32 num_channels = 3; + uint64 id = 1; + string name = 2; + bytes audio = 3; + uint32 sample_rate = 4; + uint32 num_channels = 5; } message TranscriptionFrame { - string text = 1; - string user_id = 2; - string timestamp = 3; + uint64 id = 1; + string name = 2; + string text = 3; + string user_id = 4; + string timestamp = 5; } message Frame { - oneof frame { - TextFrame text = 1; - AudioRawFrame audio = 2; - TranscriptionFrame transcription = 3; - } + oneof frame { + TextFrame text = 1; + AudioRawFrame audio = 2; + TranscriptionFrame transcription = 3; + } } diff --git a/src/pipecat/frames/protobufs/frames_pb2.py b/src/pipecat/frames/protobufs/frames_pb2.py index 32e1c3372..5040efc97 100644 --- a/src/pipecat/frames/protobufs/frames_pb2.py +++ b/src/pipecat/frames/protobufs/frames_pb2.py @@ -14,7 +14,7 @@ _sym_db = _symbol_database.Default() -DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x0c\x66rames.proto\x12\x07pipecat\"\x19\n\tTextFrame\x12\x0c\n\x04text\x18\x01 \x01(\t\"I\n\rAudioRawFrame\x12\r\n\x05\x61udio\x18\x01 \x01(\x0c\x12\x13\n\x0bsample_rate\x18\x02 \x01(\r\x12\x14\n\x0cnum_channels\x18\x03 \x01(\r\"F\n\x12TranscriptionFrame\x12\x0c\n\x04text\x18\x01 \x01(\t\x12\x0f\n\x07user_id\x18\x02 \x01(\t\x12\x11\n\ttimestamp\x18\x03 \x01(\t\"\x93\x01\n\x05\x46rame\x12\"\n\x04text\x18\x01 \x01(\x0b\x32\x12.pipecat.TextFrameH\x00\x12\'\n\x05\x61udio\x18\x02 \x01(\x0b\x32\x16.pipecat.AudioRawFrameH\x00\x12\x34\n\rtranscription\x18\x03 \x01(\x0b\x32\x1b.pipecat.TranscriptionFrameH\x00\x42\x07\n\x05\x66rameb\x06proto3') +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x0c\x66rames.proto\x12\x07pipecat\"3\n\tTextFrame\x12\n\n\x02id\x18\x01 \x01(\x04\x12\x0c\n\x04name\x18\x02 \x01(\t\x12\x0c\n\x04text\x18\x03 \x01(\t\"c\n\rAudioRawFrame\x12\n\n\x02id\x18\x01 \x01(\x04\x12\x0c\n\x04name\x18\x02 \x01(\t\x12\r\n\x05\x61udio\x18\x03 \x01(\x0c\x12\x13\n\x0bsample_rate\x18\x04 \x01(\r\x12\x14\n\x0cnum_channels\x18\x05 \x01(\r\"`\n\x12TranscriptionFrame\x12\n\n\x02id\x18\x01 \x01(\x04\x12\x0c\n\x04name\x18\x02 \x01(\t\x12\x0c\n\x04text\x18\x03 \x01(\t\x12\x0f\n\x07user_id\x18\x04 \x01(\t\x12\x11\n\ttimestamp\x18\x05 \x01(\t\"\x93\x01\n\x05\x46rame\x12\"\n\x04text\x18\x01 \x01(\x0b\x32\x12.pipecat.TextFrameH\x00\x12\'\n\x05\x61udio\x18\x02 \x01(\x0b\x32\x16.pipecat.AudioRawFrameH\x00\x12\x34\n\rtranscription\x18\x03 \x01(\x0b\x32\x1b.pipecat.TranscriptionFrameH\x00\x42\x07\n\x05\x66rameb\x06proto3') _globals = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) @@ -22,11 +22,11 @@ _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'frames_pb2', _globals) if _descriptor._USE_C_DESCRIPTORS == False: DESCRIPTOR._options = None _globals['_TEXTFRAME']._serialized_start=25 - _globals['_TEXTFRAME']._serialized_end=50 - _globals['_AUDIORAWFRAME']._serialized_start=52 - _globals['_AUDIORAWFRAME']._serialized_end=125 - _globals['_TRANSCRIPTIONFRAME']._serialized_start=127 - _globals['_TRANSCRIPTIONFRAME']._serialized_end=197 - _globals['_FRAME']._serialized_start=200 - _globals['_FRAME']._serialized_end=347 + _globals['_TEXTFRAME']._serialized_end=76 + _globals['_AUDIORAWFRAME']._serialized_start=78 + _globals['_AUDIORAWFRAME']._serialized_end=177 + _globals['_TRANSCRIPTIONFRAME']._serialized_start=179 + _globals['_TRANSCRIPTIONFRAME']._serialized_end=275 + _globals['_FRAME']._serialized_start=278 + _globals['_FRAME']._serialized_end=425 # @@protoc_insertion_point(module_scope) diff --git a/src/pipecat/serializers/protobuf.py b/src/pipecat/serializers/protobuf.py index 515a653e6..50692a51b 100644 --- a/src/pipecat/serializers/protobuf.py +++ b/src/pipecat/serializers/protobuf.py @@ -36,7 +36,8 @@ class ProtobufFrameSerializer(FrameSerializer): setattr(getattr(proto_frame, proto_optional_name), field.name, getattr(frame, field.name)) - return proto_frame.SerializeToString() + result = proto_frame.SerializeToString() + return result def deserialize(self, data: bytes) -> Frame: """Returns a Frame object from a Frame protobuf. Used to convert frames @@ -68,4 +69,22 @@ class ProtobufFrameSerializer(FrameSerializer): args_dict = {} for field in proto.DESCRIPTOR.fields_by_name[which].message_type.fields: args_dict[field.name] = getattr(args, field.name) - return class_name(**args_dict) + + # Remove special fields if needed + id = getattr(args, "id") + name = getattr(args, "name") + if not id: + del args_dict["id"] + if not name: + del args_dict["name"] + + # Create the instance + instance = class_name(**args_dict) + + # Set special fields + if id: + setattr(instance, "id", getattr(args, "id")) + if name: + setattr(instance, "name", getattr(args, "name")) + + return instance