Updating 06- sample

This commit is contained in:
Moishe Lettvin
2024-01-12 11:40:49 -05:00
parent ad427bea3a
commit 9bbfc8ad05
2 changed files with 38 additions and 36 deletions

View File

@@ -50,6 +50,18 @@ class DailyTransportService(EventHandler):
self.camera_thread = None
self.frame_consumer_thread = None
self.transcription_settings = {
"language": "en",
"tier": "nova",
"model": "2-conversationalai",
"profanity_filter": True,
"redact": False,
"extra": {
"endpointing": True,
"punctuate": False,
},
}
# This queue is used to marshal frames from the async output queue to the sync output queue
# We need this to maintain the asynchronous behavior of asyncio queues -- to give async functions
# a chance to run while waiting for queue items -- but also to maintain thread safety for the
@@ -170,26 +182,14 @@ class DailyTransportService(EventHandler):
)
if self.token:
self.transcription_queue = Queue()
self.client.start_transcription(
{
"language": "en",
"tier": "nova",
"model": "2-conversationalai",
"profanity_filter": True,
"redact": False,
"extra": {
"endpointing": True,
"punctuate": False,
},
}
)
self.transcription_queue = asyncio.Queue()
self.client.start_transcription(self.transcription_settings)
self.my_participant_id = self.client.participants()["local"]["id"]
def get_transcriptions(self):
async def get_transcriptions(self):
while True:
transcript = self.transcription_queue.get()
transcript = await self.transcription_queue.get()
yield transcript
def get_async_output_queue(self):
@@ -259,8 +259,10 @@ class DailyTransportService(EventHandler):
pass
def on_transcription_message(self, message):
self.transcription_queue.put(message["text"])
pass
print("got transcription", message)
if self.loop:
asyncio.run_coroutine_threadsafe(self.transcription_queue.put(message["text"]), self.loop)
print("put transcription in queue", message)
def on_transcription_stopped(self, stopped_by, stopped_by_error):
pass

View File

@@ -26,28 +26,28 @@ async def main(room_url:str, token):
llm = AzureLLMService()
tts = AzureTTSService()
transcribed_message = ""
transcription_timeout = None
async def handle_transcriptions():
messages = [
{"role": "system", "content": "You are a helpful LLM in a WebRTC call. Your goal is to demonstrate your capabilities in a succinct way. Your output will be converted to audio. Respond to what the user said in a creative and helpful way."},
]
@transport.event_handler("on_participant_joined")
async def on_joined(transport, participant):
if participant["id"] == transport.my_participant_id:
return
sentence = ""
async for message in transport.get_transcriptions():
sentence += message
if sentence.endswith((".", "?", "!")):
messages.append({"role": "user", "content": sentence})
sentence = ''
async for audio_chunk in tts.run_tts("If you say something, I will respond."):
transport.output_queue.put(QueueFrame(FrameType.AUDIO_FRAME, audio_chunk))
full_response = ""
async for response in llm.run_llm_async_sentences(messages):
full_response += response
async for audio in tts.run_tts(response):
transport.output_queue.put(QueueFrame(FrameType.AUDIO_FRAME, audio))
@transport.event_handler("on_transcription_message")
async def on_transcription_message(transport, message) -> None:
nonlocal transcribed_message
nonlocal transcription_timeout
print(message)
if message["session_id"] != transport.my_participant_id:
transcribed_message += message['text']
messages.append({"role": "assistant", "content": full_response})
print("message received", transcribed_message)
await transport.run()
transport.transcription_settings["extra"]["punctuate"] = True
await asyncio.gather(transport.run(), handle_transcriptions())
if __name__ == "__main__":