From 4e9586595db59b7f43e56b9f37354de6ef2c1252 Mon Sep 17 00:00:00 2001 From: Moishe Lettvin Date: Mon, 29 Jan 2024 15:06:39 -0500 Subject: [PATCH] minor cleanup --- src/dailyai/queue_aggregators.py | 3 ++- .../services/daily_transport_service.py | 11 +++++----- .../foundational/02-llm-say-one-thing.py | 1 - src/samples/foundational/03-still-frame.py | 8 +++---- .../foundational/04-utterance-and-speech.py | 7 ++---- src/samples/foundational/08-bots-arguing.py | 14 +++++------- .../foundational/08a-characters-arguing.py | 21 ++++++++---------- .../foundational/08b-debate-generator.py | 22 +++++++++---------- src/samples/foundational/10-wake-word.py | 2 +- 9 files changed, 40 insertions(+), 49 deletions(-) diff --git a/src/dailyai/queue_aggregators.py b/src/dailyai/queue_aggregators.py index 55461e29c..07d6da7c0 100644 --- a/src/dailyai/queue_aggregators.py +++ b/src/dailyai/queue_aggregators.py @@ -34,6 +34,7 @@ class LLMContextAggregator(AIService): bot_participant_id=None, complete_sentences=True, pass_through=True): + super().__init__() self.messages = messages self.bot_participant_id = bot_participant_id self.role = role @@ -82,5 +83,5 @@ class LLMAssistantContextAggregator(LLMContextAggregator): self, messages: list[dict], bot_participant_id=None, complete_sentences=True ): super().__init__( - messages, "assistan", bot_participant_id, complete_sentences, pass_through=True + messages, "assistant", bot_participant_id, complete_sentences, pass_through=True ) diff --git a/src/dailyai/services/daily_transport_service.py b/src/dailyai/services/daily_transport_service.py index 828067353..258bd7da2 100644 --- a/src/dailyai/services/daily_transport_service.py +++ b/src/dailyai/services/daily_transport_service.py @@ -391,10 +391,10 @@ class DailyTransportService(EventHandler): all_audio_frames.extend(chunk) b.extend(chunk) - l = len(b) - (len(b) % smallest_write_size) - if l: - self.mic.write_frames(bytes(b[:l])) - b = b[l:] + truncated_length: int = len(b) - (len(b) % smallest_write_size) + if truncated_length: + self.mic.write_frames(bytes(b[:truncated_length])) + b = b[truncated_length:] elif isinstance(frame, ImageQueueFrame): self._set_image(frame.image) elif isinstance(frame, SpriteQueueFrame): @@ -406,7 +406,8 @@ class DailyTransportService(EventHandler): # if there are leftover audio bytes, write them now; failing to do so # can cause static in the audio stream. if len(b): - self.mic.write_frames(bytes(b)) + truncated_length = len(b) - (len(b) % 160) + self.mic.write_frames(bytes(b[:truncated_length])) b = bytearray() if isinstance(frame, StartStreamQueueFrame): diff --git a/src/samples/foundational/02-llm-say-one-thing.py b/src/samples/foundational/02-llm-say-one-thing.py index d18d5cf01..05c72e687 100644 --- a/src/samples/foundational/02-llm-say-one-thing.py +++ b/src/samples/foundational/02-llm-say-one-thing.py @@ -1,6 +1,5 @@ import argparse import asyncio -import logging import aiohttp diff --git a/src/samples/foundational/03-still-frame.py b/src/samples/foundational/03-still-frame.py index 5ff8dc186..6aa390095 100644 --- a/src/samples/foundational/03-still-frame.py +++ b/src/samples/foundational/03-still-frame.py @@ -5,7 +5,7 @@ import aiohttp from dailyai.queue_frame import TextQueueFrame from dailyai.services.daily_transport_service import DailyTransportService -from dailyai.services.open_ai_services import OpenAIImageGenService +from dailyai.services.fal_ai_services import FalImageGenService local_joined = False participant_joined = False @@ -25,14 +25,14 @@ async def main(room_url): transport.camera_width = 1024 transport.camera_height = 1024 - imagegen = OpenAIImageGenService(image_size="1024x1024", aiohttp_session=session) + imagegen = FalImageGenService(image_size="1024x1024", aiohttp_session=session) image_task = asyncio.create_task( imagegen.run_to_queue( transport.send_queue, [ TextQueueFrame("a cat in the style of picasso")])) - @transport.event_handler("on_participant_joined") - async def on_participant_joined(transport, participant): + @transport.event_handler("on_first_other_participant_joined") + async def on_first_other_participant_joined(transport): await image_task await transport.run() diff --git a/src/samples/foundational/04-utterance-and-speech.py b/src/samples/foundational/04-utterance-and-speech.py index cde9629b9..8e6b8ff47 100644 --- a/src/samples/foundational/04-utterance-and-speech.py +++ b/src/samples/foundational/04-utterance-and-speech.py @@ -40,11 +40,8 @@ async def main(room_url: str): ) ) - @transport.event_handler("on_participant_joined") - async def on_joined(transport, participant): - if participant["id"] == transport.my_participant_id: - return - + @transport.event_handler("on_first_other_participant_joined") + async def on_first_other_participant_joined(transport): await azure_tts.say("My friend the LLM is now going to tell a joke about llamas.", transport.send_queue) async def buffer_to_send_queue(): diff --git a/src/samples/foundational/08-bots-arguing.py b/src/samples/foundational/08-bots-arguing.py index 8d8863192..ef1384260 100644 --- a/src/samples/foundational/08-bots-arguing.py +++ b/src/samples/foundational/08-bots-arguing.py @@ -1,14 +1,10 @@ import aiohttp import argparse import asyncio -import requests -import time -import urllib.parse from dailyai.services.daily_transport_service import DailyTransportService from dailyai.services.azure_ai_services import AzureLLMService, AzureTTSService from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService -from dailyai.queue_frame import QueueFrame async def main(room_url:str): async with aiohttp.ClientSession() as session: @@ -43,15 +39,17 @@ async def main(room_url:str): # Run the LLMs synchronously for the back-and-forth bot1_msg = await llm.run_llm(bot1_messages) print(f"bot1_msg: {bot1_msg}") - bot1_messages.append({"role": "assistant", "content": bot1_msg}) - bot2_messages.append({"role": "user", "content": bot1_msg}) + if bot1_msg: + bot1_messages.append({"role": "assistant", "content": bot1_msg}) + bot2_messages.append({"role": "user", "content": bot1_msg}) await tts1.say(bot1_msg, transport.send_queue) bot2_msg = await llm.run_llm(bot2_messages) print(f"bot2_msg: {bot2_msg}") - bot2_messages.append({"role": "assistant", "content": bot2_msg}) - bot1_messages.append({"role": "user", "content": bot2_msg}) + if bot2_msg: + bot2_messages.append({"role": "assistant", "content": bot2_msg}) + bot1_messages.append({"role": "user", "content": bot2_msg}) await tts2.say(bot2_msg, transport.send_queue) diff --git a/src/samples/foundational/08a-characters-arguing.py b/src/samples/foundational/08a-characters-arguing.py index 4e5968e3a..6f247f9f9 100644 --- a/src/samples/foundational/08a-characters-arguing.py +++ b/src/samples/foundational/08a-characters-arguing.py @@ -1,16 +1,12 @@ import aiohttp import argparse import asyncio -import requests -import time -import urllib.parse from dailyai.services.daily_transport_service import DailyTransportService from dailyai.services.azure_ai_services import AzureLLMService, AzureTTSService from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService from dailyai.services.fal_ai_services import FalImageGenService -from dailyai.services.open_ai_services import OpenAIImageGenService -from dailyai.queue_frame import QueueFrame, AudioQueueFrame, ImageQueueFrame +from dailyai.queue_frame import AudioQueueFrame, ImageQueueFrame async def main(room_url:str): async with aiohttp.ClientSession() as session: @@ -34,7 +30,6 @@ async def main(room_url:str): tts1 = AzureTTSService() tts2 = ElevenLabsTTSService(session) dalle = FalImageGenService(image_size="1024x1024", aiohttp_session=session) - # dalle = OpenAIImageGenService(image_size="1024x1024") bot1_messages = [ {"role": "system", "content": "You are a stern librarian. You strongly believe that a hot dog is a sandwich. Start by stating this fact in a few sentences, then be prepared to debate this with the user. You shouldn't ever compromise on the fundamental truth that a hot dog is a sandwich. Your responses should only be a few sentences long."}, @@ -42,26 +37,28 @@ async def main(room_url:str): bot2_messages = [ {"role": "system", "content": "You are a silly cat, and you strongly believe that a hot dog is not a sandwich. Debate this with the user, only responding with a few sentences. Don't ever accept that a hot dog is a sandwich."}, ] - + async def get_bot1_statement(): # Run the LLMs synchronously for the back-and-forth bot1_msg = await llm.run_llm(bot1_messages) print(f"bot1_msg: {bot1_msg}") - bot1_messages.append({"role": "assistant", "content": bot1_msg}) - bot2_messages.append({"role": "user", "content": bot1_msg}) + if bot1_msg: + bot1_messages.append({"role": "assistant", "content": bot1_msg}) + bot2_messages.append({"role": "user", "content": bot1_msg}) all_audio = bytearray() async for audio in tts1.run_tts(bot1_msg): all_audio.extend(audio) return all_audio - + async def get_bot2_statement(): # Run the LLMs synchronously for the back-and-forth bot2_msg = await llm.run_llm(bot2_messages) print(f"bot2_msg: {bot2_msg}") - bot2_messages.append({"role": "assistant", "content": bot2_msg}) - bot1_messages.append({"role": "user", "content": bot2_msg}) + if bot2_msg: + bot2_messages.append({"role": "assistant", "content": bot2_msg}) + bot1_messages.append({"role": "user", "content": bot2_msg}) all_audio = bytearray() async for audio in tts2.run_tts(bot2_msg): diff --git a/src/samples/foundational/08b-debate-generator.py b/src/samples/foundational/08b-debate-generator.py index 66dae1147..2e74ac90c 100644 --- a/src/samples/foundational/08b-debate-generator.py +++ b/src/samples/foundational/08b-debate-generator.py @@ -1,16 +1,12 @@ import aiohttp import argparse import asyncio -import requests -import time -import urllib.parse from dailyai.services.daily_transport_service import DailyTransportService from dailyai.services.azure_ai_services import AzureLLMService, AzureTTSService from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService from dailyai.services.fal_ai_services import FalImageGenService -from dailyai.services.open_ai_services import OpenAIImageGenService -from dailyai.queue_frame import QueueFrame, AudioQueueFrame, ImageQueueFrame +from dailyai.queue_frame import AudioQueueFrame, ImageQueueFrame async def main(room_url:str): async with aiohttp.ClientSession() as session: @@ -39,7 +35,7 @@ async def main(room_url:str): topic = "Are pokemon edible?" affirmative = "A woman dressed as a cowboy, outside on a ranch" negative = "Pikachu in a business suit" - + topic = "Is a hot dog a sandwich?" affirmative = "A woman conservatively dressed as a librarian in a library surrounded by books" negative = "A cat dressed in a hot dog costume" @@ -52,26 +48,28 @@ async def main(room_url:str): bot2_messages = [ {"role": "system", "content": f"You are {negative}. You're in a debate, and the topic is: '{topic}'. You're arguing the negative. Debate this with the user, only responding with a few sentences. Don't ever agree with the user."}, ] - + async def get_bot1_statement(): # Run the LLMs synchronously for the back-and-forth bot1_msg = await llm.run_llm(bot1_messages) print(f"bot1_msg: {bot1_msg}") - bot1_messages.append({"role": "assistant", "content": bot1_msg}) - bot2_messages.append({"role": "user", "content": bot1_msg}) + if bot1_msg: + bot1_messages.append({"role": "assistant", "content": bot1_msg}) + bot2_messages.append({"role": "user", "content": bot1_msg}) all_audio = bytearray() async for audio in tts1.run_tts(bot1_msg): all_audio.extend(audio) return all_audio - + async def get_bot2_statement(): # Run the LLMs synchronously for the back-and-forth bot2_msg = await llm.run_llm(bot2_messages) print(f"bot2_msg: {bot2_msg}") - bot2_messages.append({"role": "assistant", "content": bot2_msg}) - bot1_messages.append({"role": "user", "content": bot2_msg}) + if bot2_msg: + bot2_messages.append({"role": "assistant", "content": bot2_msg}) + bot1_messages.append({"role": "user", "content": bot2_msg}) all_audio = bytearray() async for audio in tts2.run_tts(bot2_msg): diff --git a/src/samples/foundational/10-wake-word.py b/src/samples/foundational/10-wake-word.py index 07d30d35b..54575091f 100644 --- a/src/samples/foundational/10-wake-word.py +++ b/src/samples/foundational/10-wake-word.py @@ -73,7 +73,7 @@ class TranscriptFilter(AIService): class NameCheckFilter(AIService): - def __init__(self, names=None): + def __init__(self, names:list[str]): self.names = names self.sentence = ""