add accumulate mobile

pull/256/head
Ben Xu 8 months ago
parent 3dea99470a
commit a7b160eec1

@ -227,15 +227,15 @@ async def listener(mobile: bool):
await asyncio.sleep(1)
if mobile:
message = accumulator_mobile.accumulate(chunk, mobile)
message = accumulator_mobile.accumulate_mobile(chunk)
else:
message = accumulator_global.accumulate(chunk, mobile)
message = accumulator_global.accumulate(chunk)
if message == None:
# Will be None until we have a full message ready
continue
# print(str(message)[:1000])
print(str(message)[:1000])
# At this point, we have our message

@ -3,7 +3,7 @@ class Accumulator:
self.template = {"role": None, "type": None, "format": None, "content": None}
self.message = self.template
def accumulate(self, chunk, mobile):
def accumulate(self, chunk):
# print(str(chunk)[:100])
if type(chunk) == dict:
if "format" in chunk and chunk["format"] == "active_line":
@ -44,10 +44,50 @@ class Accumulator:
if "content" not in self.message or type(self.message["content"]) != bytes:
self.message["content"] = b""
self.message["content"] += chunk
return None
if mobile:
self.message["type"] = "audio"
self.message["format"] = "bytes.wav"
return self.message
else:
def accumulate_mobile(self, chunk):
# print(str(chunk)[:100])
if type(chunk) == dict:
if "format" in chunk and chunk["format"] == "active_line":
# We don't do anything with these
return None
if "start" in chunk:
self.message = chunk
self.message.pop("start")
return None
if "content" in chunk:
if any(
self.message[key] != chunk[key]
for key in self.message
if key != "content"
):
self.message = chunk
if "content" not in self.message:
self.message["content"] = chunk["content"]
else:
if type(chunk["content"]) == dict:
# dict concatenation cannot happen, so we see if chunk is a dict
self.message["content"]["content"] += chunk["content"][
"content"
]
else:
self.message["content"] += chunk["content"]
return None
if "end" in chunk:
# We will proceed
message = self.message
self.message = self.template
return message
if type(chunk) == bytes:
if "content" not in self.message or type(self.message["content"]) != bytes:
self.message["content"] = b""
self.message["content"] += chunk
self.message["type"] = "audio"
self.message["format"] = "bytes.wav"
return self.message

Loading…
Cancel
Save