Updating 06- sample
This commit is contained in:
@@ -50,6 +50,18 @@ class DailyTransportService(EventHandler):
|
|||||||
self.camera_thread = None
|
self.camera_thread = None
|
||||||
self.frame_consumer_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
|
# 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
|
# 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
|
# 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:
|
if self.token:
|
||||||
self.transcription_queue = Queue()
|
self.transcription_queue = asyncio.Queue()
|
||||||
self.client.start_transcription(
|
self.client.start_transcription(self.transcription_settings)
|
||||||
{
|
|
||||||
"language": "en",
|
|
||||||
"tier": "nova",
|
|
||||||
"model": "2-conversationalai",
|
|
||||||
"profanity_filter": True,
|
|
||||||
"redact": False,
|
|
||||||
"extra": {
|
|
||||||
"endpointing": True,
|
|
||||||
"punctuate": False,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
self.my_participant_id = self.client.participants()["local"]["id"]
|
self.my_participant_id = self.client.participants()["local"]["id"]
|
||||||
|
|
||||||
def get_transcriptions(self):
|
async def get_transcriptions(self):
|
||||||
while True:
|
while True:
|
||||||
transcript = self.transcription_queue.get()
|
transcript = await self.transcription_queue.get()
|
||||||
yield transcript
|
yield transcript
|
||||||
|
|
||||||
def get_async_output_queue(self):
|
def get_async_output_queue(self):
|
||||||
@@ -259,8 +259,10 @@ class DailyTransportService(EventHandler):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def on_transcription_message(self, message):
|
def on_transcription_message(self, message):
|
||||||
self.transcription_queue.put(message["text"])
|
print("got transcription", message)
|
||||||
pass
|
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):
|
def on_transcription_stopped(self, stopped_by, stopped_by_error):
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -26,28 +26,28 @@ async def main(room_url:str, token):
|
|||||||
llm = AzureLLMService()
|
llm = AzureLLMService()
|
||||||
tts = AzureTTSService()
|
tts = AzureTTSService()
|
||||||
|
|
||||||
transcribed_message = ""
|
async def handle_transcriptions():
|
||||||
transcription_timeout = None
|
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")
|
sentence = ""
|
||||||
async def on_joined(transport, participant):
|
async for message in transport.get_transcriptions():
|
||||||
if participant["id"] == transport.my_participant_id:
|
sentence += message
|
||||||
return
|
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."):
|
full_response = ""
|
||||||
transport.output_queue.put(QueueFrame(FrameType.AUDIO_FRAME, audio_chunk))
|
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")
|
messages.append({"role": "assistant", "content": full_response})
|
||||||
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']
|
|
||||||
|
|
||||||
print("message received", transcribed_message)
|
transport.transcription_settings["extra"]["punctuate"] = True
|
||||||
|
await asyncio.gather(transport.run(), handle_transcriptions())
|
||||||
await transport.run()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user