example 02
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import os
|
import os
|
||||||
from dailyai.queue_frame import EndStreamQueueFrame
|
from dailyai.queue_frame import EndStreamQueueFrame, TextQueueFrame
|
||||||
|
|
||||||
from dailyai.services.daily_transport_service import DailyTransportService
|
from dailyai.services.daily_transport_service import DailyTransportService
|
||||||
from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService
|
from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService
|
||||||
@@ -30,6 +30,7 @@ async def main(room_url):
|
|||||||
)
|
)
|
||||||
|
|
||||||
tts = ElevenLabsTTSService(
|
tts = ElevenLabsTTSService(
|
||||||
|
source_queue=asyncio.Queue(),
|
||||||
aiohttp_session=session,
|
aiohttp_session=session,
|
||||||
api_key=os.getenv("ELEVENLABS_API_KEY"),
|
api_key=os.getenv("ELEVENLABS_API_KEY"),
|
||||||
voice_id=os.getenv("ELEVENLABS_VOICE_ID"))
|
voice_id=os.getenv("ELEVENLABS_VOICE_ID"))
|
||||||
@@ -42,22 +43,26 @@ async def main(room_url):
|
|||||||
)
|
)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
tts.sink = transport.send_queue
|
tts.sink_queue = transport.send_queue
|
||||||
|
|
||||||
# Register an event handler so we can play the audio when the participant joins.
|
# Register an event handler so we can play the audio when the participant joins.
|
||||||
@transport.event_handler("on_first_other_participant_joined")
|
@transport.event_handler("on_first_other_participant_joined")
|
||||||
async def on_first_other_participant_joined(transport, participant):
|
async def on_first_other_participant_joined(transport, participant):
|
||||||
nonlocal tts
|
nonlocal tts
|
||||||
|
|
||||||
await tts.say(
|
# todo: update the tts.say() convenience method to use the new queue architecture
|
||||||
"Hello there, " + participant["info"]["userName"] + "!"
|
await tts.source_queue.put(
|
||||||
|
TextQueueFrame("Hello there, " +
|
||||||
|
participant["info"]["userName"] + "!")
|
||||||
)
|
)
|
||||||
|
await tts.source_queue.put(EndStreamQueueFrame())
|
||||||
|
|
||||||
# wait for the output queue to be empty, then leave the meeting
|
# wait for the output queue to be empty, then leave the meeting
|
||||||
await transport.stop_when_done()
|
# todo: commented out because it seems to exit a little early, before
|
||||||
# question: how do we exit the script?
|
# the audio is finished playing
|
||||||
|
# await transport.stop_when_done()
|
||||||
|
|
||||||
await transport.run()
|
await asyncio.gather(transport.run(), tts.process_queue())
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
(url, token) = configure()
|
(url, token) = configure()
|
||||||
|
|||||||
@@ -24,33 +24,34 @@ async def main(room_url):
|
|||||||
)
|
)
|
||||||
|
|
||||||
llm = AzureLLMService(
|
llm = AzureLLMService(
|
||||||
|
source_queue=asyncio.Queue(),
|
||||||
api_key=os.getenv("AZURE_CHATGPT_API_KEY"),
|
api_key=os.getenv("AZURE_CHATGPT_API_KEY"),
|
||||||
endpoint=os.getenv("AZURE_CHATGPT_ENDPOINT"),
|
endpoint=os.getenv("AZURE_CHATGPT_ENDPOINT"),
|
||||||
model=os.getenv("AZURE_CHATGPT_MODEL"),
|
model=os.getenv("AZURE_CHATGPT_MODEL"),
|
||||||
)
|
)
|
||||||
|
|
||||||
tts = ElevenLabsTTSService(
|
tts = ElevenLabsTTSService(
|
||||||
sink=transport.send_queue,
|
|
||||||
|
source_queue=llm.sink_queue, # this should really be a sentence aggregator?
|
||||||
aiohttp_session=session,
|
aiohttp_session=session,
|
||||||
api_key=os.getenv("ELEVENLABS_API_KEY"),
|
api_key=os.getenv("ELEVENLABS_API_KEY"),
|
||||||
voice_id=os.getenv("ELEVENLABS_VOICE_ID"),
|
voice_id=os.getenv("ELEVENLABS_VOICE_ID"),
|
||||||
).chain(llm)
|
)
|
||||||
|
|
||||||
messages = [{
|
tts.sink_queue = transport.send_queue
|
||||||
"role": "system",
|
|
||||||
"content": "You are an LLM in a WebRTC session, and this is a 'hello world' demo. Say hello to the world."
|
|
||||||
}]
|
|
||||||
await llm.source.put(LLMMessagesQueueFrame(messages))
|
|
||||||
await llm.source.put(EndStreamQueueFrame())
|
|
||||||
|
|
||||||
tts_task = asyncio.gather(llm.process_queue(), tts.process_queue())
|
|
||||||
|
|
||||||
@transport.event_handler("on_first_other_participant_joined")
|
@transport.event_handler("on_first_other_participant_joined")
|
||||||
async def on_first_other_participant_joined(transport):
|
async def on_first_other_participant_joined(transport, participant):
|
||||||
await tts_task
|
messages = [{
|
||||||
await transport.stop_when_done()
|
"role": "system",
|
||||||
|
"content": "You are an LLM in a WebRTC session, and this is a 'hello world' demo. Say hello to the world."
|
||||||
|
}]
|
||||||
|
await llm.source_queue.put(LLMMessagesQueueFrame(messages))
|
||||||
|
await llm.source_queue.put(EndStreamQueueFrame())
|
||||||
|
# todo: commented out because it exits before audio plays
|
||||||
|
# await transport.stop_when_done()
|
||||||
|
|
||||||
await transport.run()
|
await asyncio.gather(transport.run(), llm.process_queue(), tts.process_queue())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user