refactor party tonight
This commit is contained in:
@@ -2,6 +2,8 @@ import asyncio
|
|||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from httpx import request
|
||||||
|
|
||||||
from dailyai.queue_frame import QueueFrame, FrameType
|
from dailyai.queue_frame import QueueFrame, FrameType
|
||||||
|
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
@@ -13,9 +15,7 @@ from collections.abc import Iterable, AsyncIterable
|
|||||||
|
|
||||||
class AIService:
|
class AIService:
|
||||||
|
|
||||||
def __init__(
|
def __init__(self):
|
||||||
self
|
|
||||||
):
|
|
||||||
self.logger = logging.getLogger("dailyai")
|
self.logger = logging.getLogger("dailyai")
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
@@ -27,30 +27,60 @@ class AIService:
|
|||||||
def possible_output_frame_types(self) -> set[FrameType]:
|
def possible_output_frame_types(self) -> set[FrameType]:
|
||||||
return set()
|
return set()
|
||||||
|
|
||||||
|
async def run_to_queue(self, queue: asyncio.Queue, frames, add_end_of_stream=False) -> None:
|
||||||
|
async for frame in self.run(frames):
|
||||||
|
print("got frame", frame.frame_type)
|
||||||
|
await queue.put(frame)
|
||||||
|
|
||||||
|
if add_end_of_stream:
|
||||||
|
await queue.put(QueueFrame(FrameType.END_STREAM, None))
|
||||||
|
|
||||||
async def run(
|
async def run(
|
||||||
self,
|
self,
|
||||||
requested_frame_types:set[FrameType],
|
frames: Iterable[QueueFrame]
|
||||||
frames:Iterable[QueueFrame] | AsyncIterable[QueueFrame]
|
| AsyncIterable[QueueFrame]
|
||||||
) -> AsyncGenerator[QueueFrame, None]:
|
| asyncio.Queue[QueueFrame],
|
||||||
if self.possible_output_frame_types().intersection(requested_frame_types) == set():
|
requested_frame_types: set[FrameType] | None=None,
|
||||||
|
) -> AsyncGenerator[QueueFrame, None]:
|
||||||
|
if requested_frame_types and self.possible_output_frame_types().intersection(requested_frame_types) == set():
|
||||||
raise Exception(f"Requested frame types {requested_frame_types} are not supported by this service.")
|
raise Exception(f"Requested frame types {requested_frame_types} are not supported by this service.")
|
||||||
|
|
||||||
|
if not requested_frame_types:
|
||||||
|
requested_frame_types = self.possible_output_frame_types()
|
||||||
|
|
||||||
|
print("running", self.__class__.__name__, "with frame types", requested_frame_types)
|
||||||
|
|
||||||
if isinstance(frames, AsyncIterable):
|
if isinstance(frames, AsyncIterable):
|
||||||
async for frame in frames:
|
async for frame in frames:
|
||||||
output_frame: QueueFrame | None = await self.process_frame(requested_frame_types, frame)
|
async for output_frame in self.process_frame(requested_frame_types, frame):
|
||||||
if output_frame:
|
print(
|
||||||
|
"yielding frame", self.__class__.__name__, output_frame.frame_type
|
||||||
|
)
|
||||||
yield output_frame
|
yield output_frame
|
||||||
elif isinstance(frames, Iterable):
|
elif isinstance(frames, Iterable):
|
||||||
for frame in frames:
|
for frame in frames:
|
||||||
output_frame = await self.process_frame(requested_frame_types, frame)
|
async for output_frame in self.process_frame(requested_frame_types, frame):
|
||||||
if output_frame:
|
print(
|
||||||
|
"yielding frame", self.__class__.__name__, output_frame.frame_type
|
||||||
|
)
|
||||||
yield output_frame
|
yield output_frame
|
||||||
|
elif isinstance(frames, asyncio.Queue):
|
||||||
|
while True:
|
||||||
|
frame = await frames.get()
|
||||||
|
async for output_frame in self.process_frame(requested_frame_types, frame):
|
||||||
|
print(
|
||||||
|
"yielding frame", self.__class__.__name__, output_frame.frame_type
|
||||||
|
)
|
||||||
|
yield output_frame
|
||||||
|
if frame.frame_type == FrameType.END_STREAM:
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
raise Exception("Frames must be an iterable or async iterable")
|
raise Exception("Frames must be an iterable or async iterable")
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def process_frame(self, requested_frame_types:set[FrameType], frame:QueueFrame) -> QueueFrame | None:
|
async def process_frame(self, requested_frame_types:set[FrameType], frame:QueueFrame) -> AsyncGenerator[QueueFrame, None]:
|
||||||
pass
|
# Yield something so the linter can deduce what should happen here.
|
||||||
|
yield QueueFrame(FrameType.END_STREAM, None)
|
||||||
|
|
||||||
class SentenceAggregator(AIService):
|
class SentenceAggregator(AIService):
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
@@ -63,29 +93,26 @@ class SentenceAggregator(AIService):
|
|||||||
def possible_output_frame_types(self) -> set[FrameType]:
|
def possible_output_frame_types(self) -> set[FrameType]:
|
||||||
return set([FrameType.SENTENCE])
|
return set([FrameType.SENTENCE])
|
||||||
|
|
||||||
async def process_frame(self, requested_frame_types: set[FrameType], frame: QueueFrame) -> QueueFrame | None:
|
async def process_frame(self, requested_frame_types: set[FrameType], frame: QueueFrame) -> AsyncGenerator[QueueFrame, None]:
|
||||||
if not FrameType.SENTENCE in requested_frame_types:
|
if not FrameType.SENTENCE in requested_frame_types:
|
||||||
return None
|
return
|
||||||
|
|
||||||
if frame.frame_type == FrameType.TEXT_CHUNK:
|
if frame.frame_type == FrameType.TEXT_CHUNK:
|
||||||
if type(frame.frame_data) != str:
|
if type(frame.frame_data) != str:
|
||||||
raise Exception("Sentence aggregator requires a string for the data field")
|
raise Exception(
|
||||||
|
"Sentence aggregator requires a string for the data field"
|
||||||
|
)
|
||||||
|
|
||||||
self.current_sentence += frame.frame_data
|
self.current_sentence += frame.frame_data
|
||||||
if self.current_sentence.endswith((".", "?", "!")):
|
if self.current_sentence.endswith((".", "?", "!")):
|
||||||
sentence = self.current_sentence
|
sentence = self.current_sentence
|
||||||
self.current_sentence = ""
|
self.current_sentence = ""
|
||||||
return QueueFrame(FrameType.SENTENCE, sentence)
|
yield QueueFrame(FrameType.SENTENCE, sentence)
|
||||||
return None
|
|
||||||
elif frame.frame_type == FrameType.END_STREAM:
|
elif frame.frame_type == FrameType.END_STREAM:
|
||||||
if self.current_sentence:
|
if self.current_sentence:
|
||||||
return QueueFrame(FrameType.SENTENCE, self.current_sentence)
|
yield QueueFrame(FrameType.SENTENCE, self.current_sentence)
|
||||||
else:
|
|
||||||
return None
|
|
||||||
elif frame.frame_type == FrameType.SENTENCE:
|
elif frame.frame_type == FrameType.SENTENCE:
|
||||||
return frame
|
yield frame
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
class LLMService(AIService):
|
class LLMService(AIService):
|
||||||
@@ -93,30 +120,29 @@ class LLMService(AIService):
|
|||||||
return set([FrameType.LLM_MESSAGE, FrameType.SENTENCE, FrameType.TRANSCRIPTION])
|
return set([FrameType.LLM_MESSAGE, FrameType.SENTENCE, FrameType.TRANSCRIPTION])
|
||||||
|
|
||||||
def allowed_output_frame_types(self) -> set[FrameType]:
|
def allowed_output_frame_types(self) -> set[FrameType]:
|
||||||
return set([FrameType.SENTENCE, FrameType.SENTENCE, FrameType.TEXT_CHUNK])
|
return set([FrameType.SENTENCE, FrameType.TEXT_CHUNK])
|
||||||
|
|
||||||
async def run_llm_async_sentences(self, messages) -> AsyncGenerator[str, None]:
|
@abstractmethod
|
||||||
current_text = ""
|
async def run_llm_async(self, messages) -> AsyncGenerator[str, None]:
|
||||||
async for text in self.run_llm_async(messages):
|
yield ""
|
||||||
current_text += text
|
|
||||||
if re.match(r"^.*[.!?]$", text):
|
|
||||||
yield current_text
|
|
||||||
current_text = ""
|
|
||||||
|
|
||||||
if current_text:
|
@abstractmethod
|
||||||
yield current_text
|
async def run_llm(self, messages) -> str:
|
||||||
|
pass
|
||||||
async def process_frame(self, frame:QueueFrame) -> QueueFrame | None:
|
|
||||||
if not self.output_queue:
|
|
||||||
raise Exception("Output queue must be set before using the run method.")
|
|
||||||
|
|
||||||
|
async def process_frame(self, requested_frame_types: set[FrameType], frame: QueueFrame) -> AsyncGenerator[QueueFrame, None]:
|
||||||
if frame.frame_type == FrameType.LLM_MESSAGE:
|
if frame.frame_type == FrameType.LLM_MESSAGE:
|
||||||
if type(frame.frame_data) != list:
|
if type(frame.frame_data) != list:
|
||||||
raise Exception("LLM service requires a dict for the data field")
|
raise Exception("LLM service requires a dict for the data field")
|
||||||
|
|
||||||
messages: list[dict[str, str]] = frame.frame_data
|
messages: list[dict[str, str]] = frame.frame_data
|
||||||
async for message in self.run_llm_async_sentences(messages):
|
if FrameType.SENTENCE in requested_frame_types:
|
||||||
await self.output_queue.put(QueueFrame(FrameType.SENTENCE, message))
|
yield QueueFrame(FrameType.SENTENCE, await self.run_llm(messages))
|
||||||
|
else:
|
||||||
|
async for text_chunk in self.run_llm_async(messages):
|
||||||
|
yield QueueFrame(FrameType.TEXT_CHUNK, text_chunk)
|
||||||
|
|
||||||
|
# TODO: handle other frame types! Need to aggregate into messages
|
||||||
|
|
||||||
|
|
||||||
class TTSService(AIService):
|
class TTSService(AIService):
|
||||||
@@ -124,6 +150,12 @@ class TTSService(AIService):
|
|||||||
def get_mic_sample_rate(self):
|
def get_mic_sample_rate(self):
|
||||||
return 16000
|
return 16000
|
||||||
|
|
||||||
|
def allowed_input_frame_types(self) -> set[FrameType]:
|
||||||
|
return set([FrameType.SENTENCE, FrameType.TRANSCRIPTION, FrameType.TEXT_CHUNK])
|
||||||
|
|
||||||
|
def possible_output_frame_types(self) -> set[FrameType]:
|
||||||
|
return set([FrameType.AUDIO])
|
||||||
|
|
||||||
# Converts the sentence to audio. Yields a list of audio frames that can
|
# Converts the sentence to audio. Yields a list of audio frames that can
|
||||||
# be sent to the microphone device
|
# be sent to the microphone device
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
@@ -131,25 +163,48 @@ class TTSService(AIService):
|
|||||||
# yield empty bytes here, so linting can infer what this method does
|
# yield empty bytes here, so linting can infer what this method does
|
||||||
yield bytes()
|
yield bytes()
|
||||||
|
|
||||||
async def process_frame(self, frame:QueueFrame) -> QueueFrame | None:
|
async def process_frame(self, requested_frame_types: set[FrameType], frame: QueueFrame) -> AsyncGenerator[QueueFrame, None]:
|
||||||
if not self.output_queue:
|
if not FrameType.AUDIO in requested_frame_types:
|
||||||
raise Exception("Output queue must be set before using the run method.")
|
return
|
||||||
|
|
||||||
if frame.frame_type == FrameType.SENTENCE:
|
if type(frame.frame_data) != str:
|
||||||
if type(frame.frame_data) != str:
|
raise Exception("TTS service requires a string for the data field")
|
||||||
raise Exception("TTS service requires a string for the data field")
|
|
||||||
|
|
||||||
text = frame.frame_data
|
async for audio_chunk in self.run_tts(frame.frame_data):
|
||||||
async for audio in self.run_tts(text):
|
yield QueueFrame(FrameType.AUDIO, audio_chunk)
|
||||||
await self.output_queue.put(QueueFrame(FrameType.AUDIO, audio))
|
|
||||||
|
# Convenience function to send the audio for a sentence to the given queue
|
||||||
|
async def say(self, sentence, queue: asyncio.Queue):
|
||||||
|
async for audio_chunk in self.run_tts(sentence):
|
||||||
|
await queue.put(QueueFrame(FrameType.AUDIO, audio_chunk))
|
||||||
|
|
||||||
|
|
||||||
class ImageGenService(AIService):
|
class ImageGenService(AIService):
|
||||||
|
def __init__(self, image_size, **kwargs):
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
self.image_size = image_size
|
||||||
|
|
||||||
|
def allowed_input_frame_types(self) -> set[FrameType]:
|
||||||
|
return set([FrameType.SENTENCE, FrameType.TRANSCRIPTION, FrameType.TEXT_CHUNK, FrameType.IMAGE_DESCRIPTION])
|
||||||
|
|
||||||
|
def possible_output_frame_types(self) -> set[FrameType]:
|
||||||
|
return set([FrameType.IMAGE])
|
||||||
|
|
||||||
# Renders the image. Returns an Image object.
|
# Renders the image. Returns an Image object.
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def run_image_gen(self, sentence, size) -> tuple[str, bytes]:
|
async def run_image_gen(self, sentence) -> tuple[str, bytes]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
async def process_frame(self, requested_frame_types: set[FrameType], frame: QueueFrame) -> AsyncGenerator[QueueFrame, None]:
|
||||||
|
if not FrameType.IMAGE in requested_frame_types:
|
||||||
|
return
|
||||||
|
|
||||||
|
if type(frame.frame_data) != str:
|
||||||
|
raise Exception("Image service requires a string for the data field")
|
||||||
|
|
||||||
|
(_, image_data) = await self.run_image_gen(frame.frame_data)
|
||||||
|
yield QueueFrame(FrameType.IMAGE, image_data)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class AIServiceConfig:
|
class AIServiceConfig:
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ from PIL import Image
|
|||||||
from azure.cognitiveservices.speech import SpeechSynthesizer, SpeechConfig, ResultReason, CancellationReason
|
from azure.cognitiveservices.speech import SpeechSynthesizer, SpeechConfig, ResultReason, CancellationReason
|
||||||
|
|
||||||
class AzureTTSService(TTSService):
|
class AzureTTSService(TTSService):
|
||||||
def __init__(self, input_queue=None, output_queue=None, speech_key=None, speech_region=None):
|
def __init__(self, speech_key=None, speech_region=None):
|
||||||
super().__init__(input_queue, output_queue)
|
super().__init__()
|
||||||
|
|
||||||
speech_key = speech_key or os.getenv("AZURE_SPEECH_SERVICE_KEY")
|
speech_key = speech_key or os.getenv("AZURE_SPEECH_SERVICE_KEY")
|
||||||
speech_region = speech_region or os.getenv("AZURE_SPEECH_SERVICE_REGION")
|
speech_region = speech_region or os.getenv("AZURE_SPEECH_SERVICE_REGION")
|
||||||
@@ -35,7 +35,10 @@ class AzureTTSService(TTSService):
|
|||||||
"<prosody rate='1.05'>" \
|
"<prosody rate='1.05'>" \
|
||||||
f"{sentence}" \
|
f"{sentence}" \
|
||||||
"</prosody></mstts:express-as></voice></speak> "
|
"</prosody></mstts:express-as></voice></speak> "
|
||||||
result = await asyncio.to_thread(self.speech_synthesizer.speak_ssml, (ssml))
|
try:
|
||||||
|
result = await asyncio.to_thread(self.speech_synthesizer.speak_ssml, (ssml))
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error("Error in azure tts", e)
|
||||||
self.logger.info("Got azure tts result")
|
self.logger.info("Got azure tts result")
|
||||||
if result.reason == ResultReason.SynthesizingAudioCompleted:
|
if result.reason == ResultReason.SynthesizingAudioCompleted:
|
||||||
self.logger.info("Returning result")
|
self.logger.info("Returning result")
|
||||||
@@ -48,8 +51,8 @@ class AzureTTSService(TTSService):
|
|||||||
self.logger.info("Error details: {}".format(cancellation_details.error_details))
|
self.logger.info("Error details: {}".format(cancellation_details.error_details))
|
||||||
|
|
||||||
class AzureLLMService(LLMService):
|
class AzureLLMService(LLMService):
|
||||||
def __init__(self, input_queue=None, output_queue=None, api_key=None, azure_endpoint=None, api_version=None, model=None):
|
def __init__(self, api_key=None, azure_endpoint=None, api_version=None, model=None):
|
||||||
super().__init__(input_queue, output_queue)
|
super().__init__()
|
||||||
api_key = api_key or os.getenv("AZURE_CHATGPT_KEY")
|
api_key = api_key or os.getenv("AZURE_CHATGPT_KEY")
|
||||||
|
|
||||||
azure_endpoint = azure_endpoint or os.getenv("AZURE_CHATGPT_ENDPOINT")
|
azure_endpoint = azure_endpoint or os.getenv("AZURE_CHATGPT_ENDPOINT")
|
||||||
@@ -92,14 +95,14 @@ class AzureLLMService(LLMService):
|
|||||||
|
|
||||||
class AzureImageGenServiceREST(ImageGenService):
|
class AzureImageGenServiceREST(ImageGenService):
|
||||||
|
|
||||||
def __init__(self, api_key=None, azure_endpoint=None, api_version=None, model=None):
|
def __init__(self, image_size:str, api_key=None, azure_endpoint=None, api_version=None, model=None):
|
||||||
super().__init__()
|
super().__init__(image_size=image_size)
|
||||||
self.api_key = api_key or os.getenv("AZURE_DALLE_KEY")
|
self.api_key = api_key or os.getenv("AZURE_DALLE_KEY")
|
||||||
self.azure_endpoint = azure_endpoint or os.getenv("AZURE_DALLE_ENDPOINT")
|
self.azure_endpoint = azure_endpoint or os.getenv("AZURE_DALLE_ENDPOINT")
|
||||||
self.api_version = api_version or "2023-06-01-preview"
|
self.api_version = api_version or "2023-06-01-preview"
|
||||||
self.model = model or os.getenv("AZURE_DALLE_DEPLOYMENT_ID")
|
self.model = model or os.getenv("AZURE_DALLE_DEPLOYMENT_ID")
|
||||||
|
|
||||||
async def run_image_gen(self, sentence, size) -> tuple[str, bytes]:
|
async def run_image_gen(self, sentence) -> tuple[str, bytes]:
|
||||||
# TODO hoist the session to app-level
|
# TODO hoist the session to app-level
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
url = f"{self.azure_endpoint}openai/images/generations:submit?api-version={self.api_version}"
|
url = f"{self.azure_endpoint}openai/images/generations:submit?api-version={self.api_version}"
|
||||||
@@ -107,7 +110,7 @@ class AzureImageGenServiceREST(ImageGenService):
|
|||||||
body = {
|
body = {
|
||||||
# Enter your prompt text here
|
# Enter your prompt text here
|
||||||
"prompt": sentence,
|
"prompt": sentence,
|
||||||
"size": size,
|
"size": self.image_size,
|
||||||
"n": 1,
|
"n": 1,
|
||||||
}
|
}
|
||||||
async with session.post(url, headers=headers, json=body) as submission:
|
async with session.post(url, headers=headers, json=body) as submission:
|
||||||
@@ -153,14 +156,14 @@ class AzureImageGenService(ImageGenService):
|
|||||||
api_version=api_version,
|
api_version=api_version,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def run_image_gen(self, sentence, size) -> tuple[str, bytes]:
|
async def run_image_gen(self, sentence) -> tuple[str, bytes]:
|
||||||
self.logger.info("Generating azure image", sentence)
|
self.logger.info("Generating azure image", sentence)
|
||||||
|
|
||||||
image = self.client.images.generate(
|
image = self.client.images.generate(
|
||||||
model=self.model,
|
model=self.model,
|
||||||
prompt=sentence,
|
prompt=sentence,
|
||||||
n=1,
|
n=1,
|
||||||
size=size,
|
size=self.image_size,
|
||||||
)
|
)
|
||||||
|
|
||||||
url = image["data"][0]["url"]
|
url = image["data"][0]["url"]
|
||||||
|
|||||||
@@ -206,6 +206,9 @@ class DailyTransportService(EventHandler):
|
|||||||
if frame.frame_type == FrameType.END_STREAM:
|
if frame.frame_type == FrameType.END_STREAM:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
def wait_for_send_queue_to_empty(self):
|
||||||
|
self.threadsafe_send_queue.join()
|
||||||
|
|
||||||
async def run(self) -> None:
|
async def run(self) -> None:
|
||||||
self.configure_daily()
|
self.configure_daily()
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ from dailyai.services.ai_services import TTSService
|
|||||||
|
|
||||||
|
|
||||||
class ElevenLabsTTSService(TTSService):
|
class ElevenLabsTTSService(TTSService):
|
||||||
def __init__(self, input_queue=None, output_queue=None, api_key=None, voice_id=None):
|
def __init__(self, api_key=None, voice_id=None):
|
||||||
super().__init__(input_queue, output_queue)
|
super().__init__()
|
||||||
|
|
||||||
self.api_key = api_key or os.getenv("ELEVENLABS_API_KEY")
|
self.api_key = api_key or os.getenv("ELEVENLABS_API_KEY")
|
||||||
self.voice_id = voice_id or os.getenv("ELEVENLABS_VOICE_ID")
|
self.voice_id = voice_id or os.getenv("ELEVENLABS_VOICE_ID")
|
||||||
|
|||||||
@@ -50,20 +50,20 @@ class OpenAILLMService(LLMService):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
class OpenAIImageGenService(ImageGenService):
|
class OpenAIImageGenService(ImageGenService):
|
||||||
def __init__(self, api_key=None, model=None):
|
def __init__(self, image_size:str, api_key=None, model=None):
|
||||||
super().__init__()
|
super().__init__(image_size=image_size)
|
||||||
api_key = api_key or os.getenv("OPEN_AI_KEY")
|
api_key = api_key or os.getenv("OPEN_AI_KEY")
|
||||||
self.model = model or os.getenv("OPEN_AI_IMAGE_MODEL") or "dall-e-3"
|
self.model = model or os.getenv("OPEN_AI_IMAGE_MODEL") or "dall-e-3"
|
||||||
self.client = AsyncOpenAI(api_key=api_key)
|
self.client = AsyncOpenAI(api_key=api_key)
|
||||||
|
|
||||||
async def run_image_gen(self, sentence, size) -> tuple[str, bytes]:
|
async def run_image_gen(self, sentence) -> tuple[str, bytes]:
|
||||||
self.logger.info("Generating OpenAI image", sentence)
|
self.logger.info("Generating OpenAI image", sentence)
|
||||||
|
|
||||||
image = await self.client.images.generate(
|
image = await self.client.images.generate(
|
||||||
prompt=sentence,
|
prompt=sentence,
|
||||||
model=self.model,
|
model=self.model,
|
||||||
n=1,
|
n=1,
|
||||||
size=size
|
size=self.image_size
|
||||||
)
|
)
|
||||||
image_url = image.data[0].url
|
image_url = image.data[0].url
|
||||||
if not image_url:
|
if not image_url:
|
||||||
|
|||||||
@@ -27,21 +27,16 @@ async def main(room_url):
|
|||||||
# similarly, create a tts service
|
# similarly, create a tts service
|
||||||
tts = AzureTTSService()
|
tts = AzureTTSService()
|
||||||
|
|
||||||
# Get the generator for the audio. This will start running in the background,
|
|
||||||
# and when we ask the generator for its items, we'll get what it's generated.
|
|
||||||
audio_generator: AsyncGenerator[bytes, None] = tts.run_tts("hello world")
|
|
||||||
|
|
||||||
# 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_participant_joined")
|
@transport.event_handler("on_participant_joined")
|
||||||
async def on_participant_joined(transport, participant):
|
async def on_participant_joined(transport, participant):
|
||||||
if participant["info"]["isLocal"]:
|
if participant["info"]["isLocal"]:
|
||||||
return
|
return
|
||||||
|
|
||||||
async for audio in audio_generator:
|
await tts.say("Hello there, " + participant["info"]["userName"] + "!", transport.send_queue)
|
||||||
transport.output_queue.put(QueueFrame(FrameType.AUDIO, audio))
|
|
||||||
|
|
||||||
# wait for the output queue to be empty, then leave the meeting
|
# wait for the output queue to be empty, then leave the meeting
|
||||||
transport.output_queue.join()
|
transport.wait_for_send_queue_to_empty()
|
||||||
transport.stop()
|
transport.stop()
|
||||||
|
|
||||||
await transport.run()
|
await transport.run()
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ from typing import AsyncGenerator
|
|||||||
|
|
||||||
from dailyai.queue_frame import QueueFrame, FrameType
|
from dailyai.queue_frame import QueueFrame, FrameType
|
||||||
from dailyai.services.daily_transport_service import DailyTransportService
|
from dailyai.services.daily_transport_service import DailyTransportService
|
||||||
|
from dailyai.services.ai_services import SentenceAggregator
|
||||||
from dailyai.services.azure_ai_services import AzureLLMService
|
from dailyai.services.azure_ai_services import AzureLLMService
|
||||||
from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService
|
from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService
|
||||||
|
|
||||||
@@ -17,29 +18,27 @@ async def main(room_url):
|
|||||||
)
|
)
|
||||||
transport.mic_enabled = True
|
transport.mic_enabled = True
|
||||||
|
|
||||||
text_to_llm_queue = asyncio.Queue()
|
tts = ElevenLabsTTSService(voice_id="29vD33N1CtxCmqQRPOHJ")
|
||||||
llm_to_tts_queue = asyncio.Queue()
|
llm = AzureLLMService()
|
||||||
|
|
||||||
tts = ElevenLabsTTSService(
|
|
||||||
llm_to_tts_queue, transport.get_async_send_queue(), voice_id="29vD33N1CtxCmqQRPOHJ"
|
|
||||||
)
|
|
||||||
llm = AzureLLMService(text_to_llm_queue, llm_to_tts_queue)
|
|
||||||
|
|
||||||
messages = [{
|
messages = [{
|
||||||
"role": "system",
|
"role": "system",
|
||||||
"content": "You are an LLM in a WebRTC session, and this is a 'hello world' demo. Say hello to the world."
|
"content": "You are an LLM in a WebRTC session, and this is a 'hello world' demo. Say hello to the world."
|
||||||
}]
|
}]
|
||||||
await text_to_llm_queue.put(QueueFrame(FrameType.LLM_MESSAGE, messages))
|
tts_task = asyncio.create_task(
|
||||||
await text_to_llm_queue.put(QueueFrame(FrameType.END_STREAM, None))
|
tts.run_to_queue(
|
||||||
|
transport.send_queue,
|
||||||
llm_task = asyncio.create_task(llm.run())
|
SentenceAggregator().run(
|
||||||
|
llm.run([QueueFrame(FrameType.LLM_MESSAGE, messages)])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
@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):
|
||||||
await asyncio.gather(llm_task, tts.run())
|
await tts_task
|
||||||
|
|
||||||
# wait for the output queue to be empty, then leave the meeting
|
transport.wait_for_send_queue_to_empty()
|
||||||
transport.output_queue.join()
|
|
||||||
transport.stop()
|
transport.stop()
|
||||||
|
|
||||||
await transport.run()
|
await transport.run()
|
||||||
|
|||||||
@@ -21,13 +21,14 @@ async def main(room_url):
|
|||||||
transport.camera_width = 1024
|
transport.camera_width = 1024
|
||||||
transport.camera_height = 1024
|
transport.camera_height = 1024
|
||||||
|
|
||||||
imagegen = OpenAIImageGenService()
|
imagegen = OpenAIImageGenService(image_size="1024x1024")
|
||||||
image_task = asyncio.create_task(imagegen.run_image_gen("a cat in the style of picasso", "1024x1024"))
|
image_task = asyncio.create_task(
|
||||||
|
imagegen.run_to_queue(transport.send_queue, [QueueFrame(FrameType.IMAGE_DESCRIPTION, "a cat in the style of picasso")])
|
||||||
|
)
|
||||||
|
|
||||||
@transport.event_handler("on_participant_joined")
|
@transport.event_handler("on_participant_joined")
|
||||||
async def on_participant_joined(transport, participant):
|
async def on_participant_joined(transport, participant):
|
||||||
(_, image_bytes) = await image_task
|
await image_task
|
||||||
transport.output_queue.put(QueueFrame(FrameType.IMAGE, image_bytes))
|
|
||||||
|
|
||||||
await transport.run()
|
await transport.run()
|
||||||
|
|
||||||
@@ -38,6 +39,6 @@ if __name__ == "__main__":
|
|||||||
"-u", "--url", type=str, required=True, help="URL of the Daily room to join"
|
"-u", "--url", type=str, required=True, help="URL of the Daily room to join"
|
||||||
)
|
)
|
||||||
|
|
||||||
args: argparse.Namespace = parser.parse_args()
|
args, unknown = parser.parse_known_args()
|
||||||
|
|
||||||
asyncio.run(main(args.url))
|
asyncio.run(main(args.url))
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ import argparse
|
|||||||
import asyncio
|
import asyncio
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from dailyai.services.ai_services import SentenceAggregator
|
||||||
from dailyai.services.daily_transport_service import DailyTransportService
|
from dailyai.services.daily_transport_service import DailyTransportService
|
||||||
from dailyai.services.azure_ai_services import AzureLLMService, AzureTTSService
|
from dailyai.services.azure_ai_services import AzureLLMService, AzureTTSService
|
||||||
from dailyai.queue_frame import QueueFrame, FrameType
|
from dailyai.queue_frame import QueueFrame, FrameType
|
||||||
|
from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService
|
||||||
|
|
||||||
async def main(room_url:str):
|
async def main(room_url:str):
|
||||||
global transport
|
global transport
|
||||||
@@ -22,34 +24,46 @@ async def main(room_url:str):
|
|||||||
transport.camera_enabled = False
|
transport.camera_enabled = False
|
||||||
|
|
||||||
llm = AzureLLMService()
|
llm = AzureLLMService()
|
||||||
tts = AzureTTSService()
|
azure_tts = AzureTTSService()
|
||||||
|
elevenlabs_tts = ElevenLabsTTSService(voice_id="ErXwobaYiN019PkySvjV")
|
||||||
|
|
||||||
|
messages = [{"role": "system", "content": "tell the user a joke about llamas"}]
|
||||||
|
|
||||||
|
# Start a task to run the LLM to create a joke, and convert the LLM output to audio frames. This task
|
||||||
|
# will run in parallel with generating and speaking the audio for static text, so there's no delay to
|
||||||
|
# speak the LLM response.
|
||||||
|
buffer_queue = asyncio.Queue()
|
||||||
|
llm_response_task = asyncio.create_task(
|
||||||
|
elevenlabs_tts.run_to_queue(
|
||||||
|
buffer_queue,
|
||||||
|
SentenceAggregator().run(
|
||||||
|
llm.run([QueueFrame(FrameType.LLM_MESSAGE, messages)])
|
||||||
|
),
|
||||||
|
True,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
@transport.event_handler("on_participant_joined")
|
@transport.event_handler("on_participant_joined")
|
||||||
async def on_joined(transport, participant):
|
async def on_joined(transport, participant):
|
||||||
if participant["id"] == transport.my_participant_id:
|
if participant["id"] == transport.my_participant_id:
|
||||||
return
|
return
|
||||||
|
|
||||||
# queue two pieces of speech: one specified as a text literal,
|
await azure_tts.run_to_queue(
|
||||||
# and one generated by an llm. We'll kick off the llm first, and let
|
transport.send_queue,
|
||||||
# it generate a response while we're speaking the literal string.
|
[QueueFrame(FrameType.SENTENCE, "My friend the LLM is now going to tell a joke about llamas.")]
|
||||||
#
|
)
|
||||||
# Note that in this case, we don't use `run_llm_async` because we're
|
|
||||||
# taking advantage of the time spent speaking the first phrase to generate
|
|
||||||
# the entire LLM response, and this happens asynchronously in a task.
|
|
||||||
llm_response_task = asyncio.create_task(llm.run_llm(
|
|
||||||
[{"role": "system", "content": "tell the user a joke about llamas"}]
|
|
||||||
))
|
|
||||||
|
|
||||||
async for audio_chunk in tts.run_tts("My friend the LLM is now going to tell a joke about llamas."):
|
async def buffer_to_send_queue():
|
||||||
transport.output_queue.put(QueueFrame(FrameType.AUDIO, audio_chunk))
|
while True:
|
||||||
|
frame = await buffer_queue.get()
|
||||||
|
await transport.send_queue.put(frame)
|
||||||
|
buffer_queue.task_done()
|
||||||
|
if frame.frame_type == FrameType.END_STREAM:
|
||||||
|
break
|
||||||
|
|
||||||
llm_response = await llm_response_task
|
await asyncio.gather(llm_response_task, buffer_to_send_queue())
|
||||||
async for audio_chunk in tts.run_tts(llm_response):
|
|
||||||
transport.output_queue.put(QueueFrame(FrameType.AUDIO, audio_chunk))
|
|
||||||
|
|
||||||
|
transport.wait_for_send_queue_to_empty()
|
||||||
# wait for the output queue to be empty, then leave the meeting
|
|
||||||
transport.output_queue.join()
|
|
||||||
transport.stop()
|
transport.stop()
|
||||||
|
|
||||||
await transport.run()
|
await transport.run()
|
||||||
@@ -61,6 +75,6 @@ if __name__ == "__main__":
|
|||||||
"-u", "--url", type=str, required=True, help="URL of the Daily room to join"
|
"-u", "--url", type=str, required=True, help="URL of the Daily room to join"
|
||||||
)
|
)
|
||||||
|
|
||||||
args: argparse.Namespace = parser.parse_args()
|
args, unknown = parser.parse_known_args()
|
||||||
|
|
||||||
asyncio.run(main(args.url))
|
asyncio.run(main(args.url))
|
||||||
|
|||||||
@@ -26,10 +26,9 @@ async def main(room_url):
|
|||||||
transport.camera_height = 1024
|
transport.camera_height = 1024
|
||||||
|
|
||||||
llm = AzureLLMService()
|
llm = AzureLLMService()
|
||||||
#tts = ElevenLabsTTSService(voice_id="ErXwobaYiN019PkySvjV")
|
|
||||||
tts = ElevenLabsTTSService()
|
|
||||||
dalle = FalImageGenService()
|
dalle = FalImageGenService()
|
||||||
# dalle = OpenAIImageGenService()
|
tts = ElevenLabsTTSService(voice_id="ErXwobaYiN019PkySvjV")
|
||||||
|
#dalle = OpenAIImageGenService(image_size="1024x1024")
|
||||||
|
|
||||||
# Get a complete audio chunk from the given text. Splitting this into its own
|
# Get a complete audio chunk from the given text. Splitting this into its own
|
||||||
# coroutine lets us ensure proper ordering of the audio chunks on the output queue.
|
# coroutine lets us ensure proper ordering of the audio chunks on the output queue.
|
||||||
@@ -61,7 +60,7 @@ async def main(room_url):
|
|||||||
|
|
||||||
tts_tasks.append(get_all_audio(sentence))
|
tts_tasks.append(get_all_audio(sentence))
|
||||||
|
|
||||||
tts_tasks.insert(0, dalle.run_image_gen(image_text, "1024x1024"))
|
tts_tasks.insert(0, dalle.run_image_gen(image_text))
|
||||||
|
|
||||||
print(f"waiting for tasks to finish for {month}")
|
print(f"waiting for tasks to finish for {month}")
|
||||||
data = await asyncio.gather(
|
data = await asyncio.gather(
|
||||||
|
|||||||
Reference in New Issue
Block a user