Update 05 sample to use decorators and exit correctly

This commit is contained in:
Moishe Lettvin
2024-01-09 06:40:48 -05:00
parent 20a9c6a938
commit a29cb1a5ad

View File

@@ -1,21 +1,18 @@
import argparse
import asyncio import asyncio
from dailyai.output_queue import OutputQueueFrame, FrameType from dailyai.output_queue import OutputQueueFrame, FrameType
from dailyai.services.azure_ai_services import AzureTTSService from dailyai.services.azure_ai_services import AzureTTSService
from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService
from dailyai.services.open_ai_services import OpenAILLMService, OpenAIImageGenService from dailyai.services.open_ai_services import OpenAILLMService, OpenAIImageGenService
from dailyai.services.deepgram_ai_services import DeepgramTTSService from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService
from dailyai.services.daily_transport_service import DailyTransportService from dailyai.services.daily_transport_service import DailyTransportService
async def main(room_url, token): async def main(room_url):
class Sample05Transport(DailyTransportService): meeting_duration_minutes = 5
def on_participant_joined(self, participant): transport = DailyTransportService(
super().on_participant_joined(participant)
asyncio.run(show_all_months())
meeting_duration_minutes = 4
transport = Sample05Transport(
room_url, room_url,
token, None,
"Month Narration Bot", "Month Narration Bot",
meeting_duration_minutes, meeting_duration_minutes,
) )
@@ -26,7 +23,7 @@ async def main(room_url, token):
transport.camera_height = 1024 transport.camera_height = 1024
llm = OpenAILLMService() llm = OpenAILLMService()
tts = DeepgramTTSService() tts = ElevenLabsTTSService()
dalle = OpenAIImageGenService() dalle = OpenAIImageGenService()
async def get_all_audio(text): async def get_all_audio(text):
@@ -37,21 +34,18 @@ async def main(room_url, token):
return all_audio return all_audio
async def show_month(month): async def show_month(month):
print(f"Running llm for {month}")
inference_text = await llm.run_llm( inference_text = await llm.run_llm(
[ [
{ {
"role": "system", "role": "system",
"content": f"Describe a nature photograph suitable for use in a calendar, for the month of {month}. Include only the image description with no preamble." "content": f"Describe a nature photograph suitable for use in a calendar, for the month of {month}. Include only the image description with no preamble. Limit your description to 1 sentence."
} }
] ]
) )
print(f"got llm for {month}, {inference_text}")
(image, audio) = await asyncio.gather( (image, audio) = await asyncio.gather(
*[dalle.run_image_gen(inference_text, "1024x1024"), get_all_audio(inference_text)] *[dalle.run_image_gen(inference_text, "1024x1024"), get_all_audio(inference_text)]
) )
print(f"Got audio and video for {month}", image[0])
transport.output_queue.put( transport.output_queue.put(
[ [
OutputQueueFrame(FrameType.IMAGE_FRAME, image[1]), OutputQueueFrame(FrameType.IMAGE_FRAME, image[1]),
@@ -61,10 +55,9 @@ async def main(room_url, token):
async def show_all_months(): async def show_all_months():
# for now just two to avoid 429s with Azure # for now just two to avoid 429s with Azure
months = [ months: list[str] = [
"January", "January",
"February",] "February",
"""
"March", "March",
"April", "April",
"May", "May",
@@ -76,20 +69,29 @@ async def main(room_url, token):
"November", "November",
"December", "December",
] ]
"""
print("gathering")
await asyncio.gather(*[show_month(month) for month in months])
print("done")
try: await asyncio.gather(*[show_month(month) for month in months])
transport.run()
await asyncio.sleep(meeting_duration_minutes * 60) @transport.event_handler("on_participant_joined")
except Exception as e: async def on_participant_joined(transport, participant):
print("Exception", e) if participant["id"] == transport.my_participant_id:
finally: return
print("finally")
await show_all_months()
# wait for the output queue to be empty, then leave the meeting
transport.output_queue.join()
transport.stop() transport.stop()
await transport.run()
print("Done") print("Done")
if __name__=="__main__": if __name__=="__main__":
asyncio.run(main("https://chad-hq.daily.co/howdy", None)) parser = argparse.ArgumentParser(description="Simple Daily Bot Sample")
parser.add_argument(
"-u", "--url", type=str, required=True, help="URL of the Daily room to join"
)
args: argparse.Namespace = parser.parse_args()
asyncio.run(main(args.url))