Fix example 5
This commit is contained in:
@@ -29,7 +29,6 @@ class AIService:
|
|||||||
|
|
||||||
async def run_to_queue(self, queue: asyncio.Queue, frames, add_end_of_stream=False) -> None:
|
async def run_to_queue(self, queue: asyncio.Queue, frames, add_end_of_stream=False) -> None:
|
||||||
async for frame in self.run(frames):
|
async for frame in self.run(frames):
|
||||||
print("got frame", frame.frame_type)
|
|
||||||
await queue.put(frame)
|
await queue.put(frame)
|
||||||
|
|
||||||
if add_end_of_stream:
|
if add_end_of_stream:
|
||||||
@@ -48,29 +47,18 @@ class AIService:
|
|||||||
if not requested_frame_types:
|
if not requested_frame_types:
|
||||||
requested_frame_types = self.possible_output_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:
|
||||||
async for output_frame in self.process_frame(requested_frame_types, frame):
|
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
|
yield output_frame
|
||||||
elif isinstance(frames, Iterable):
|
elif isinstance(frames, Iterable):
|
||||||
for frame in frames:
|
for frame in frames:
|
||||||
async for output_frame in self.process_frame(requested_frame_types, frame):
|
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
|
yield output_frame
|
||||||
elif isinstance(frames, asyncio.Queue):
|
elif isinstance(frames, asyncio.Queue):
|
||||||
while True:
|
while True:
|
||||||
frame = await frames.get()
|
frame = await frames.get()
|
||||||
async for output_frame in self.process_frame(requested_frame_types, frame):
|
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
|
yield output_frame
|
||||||
if frame.frame_type == FrameType.END_STREAM:
|
if frame.frame_type == FrameType.END_STREAM:
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -200,10 +200,10 @@ class DailyTransportService(EventHandler):
|
|||||||
|
|
||||||
async def marshal_frames(self):
|
async def marshal_frames(self):
|
||||||
while True:
|
while True:
|
||||||
frame = await self.send_queue.get()
|
frame: QueueFrame | list = await self.send_queue.get()
|
||||||
self.threadsafe_send_queue.put(frame)
|
self.threadsafe_send_queue.put(frame)
|
||||||
self.send_queue.task_done()
|
self.send_queue.task_done()
|
||||||
if frame.frame_type == FrameType.END_STREAM:
|
if type(frame) == QueueFrame and frame.frame_type == FrameType.END_STREAM:
|
||||||
break
|
break
|
||||||
|
|
||||||
def wait_for_send_queue_to_empty(self):
|
def wait_for_send_queue_to_empty(self):
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ class HuggingFaceAIService(AIService):
|
|||||||
# available models at https://huggingface.co/Helsinki-NLP (**not all models use 2-character language codes**)
|
# available models at https://huggingface.co/Helsinki-NLP (**not all models use 2-character language codes**)
|
||||||
def run_text_translation(self, sentence, source_language, target_language):
|
def run_text_translation(self, sentence, source_language, target_language):
|
||||||
translator = pipeline(f"translation", model=f"Helsinki-NLP/opus-mt-{source_language}-{target_language}")
|
translator = pipeline(f"translation", model=f"Helsinki-NLP/opus-mt-{source_language}-{target_language}")
|
||||||
print(translator(sentence))
|
|
||||||
|
|
||||||
return translator(sentence)[0]["translation_text"]
|
return translator(sentence)[0]["translation_text"]
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from asyncio.queues import Queue
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
from dailyai.queue_frame import QueueFrame, FrameType
|
from dailyai.queue_frame import QueueFrame, FrameType
|
||||||
|
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
|
||||||
from dailyai.services.open_ai_services import OpenAIImageGenService
|
from dailyai.services.open_ai_services import OpenAIImageGenService
|
||||||
@@ -31,7 +32,7 @@ async def main(room_url):
|
|||||||
#dalle = OpenAIImageGenService(image_size="1024x1024")
|
#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 send queue.
|
||||||
async def get_all_audio(text):
|
async def get_all_audio(text):
|
||||||
all_audio = bytearray()
|
all_audio = bytearray()
|
||||||
async for audio in tts.run_tts(text):
|
async for audio in tts.run_tts(text):
|
||||||
@@ -43,14 +44,18 @@ async def main(room_url):
|
|||||||
image_text = ""
|
image_text = ""
|
||||||
tts_tasks = []
|
tts_tasks = []
|
||||||
first_sentence = True
|
first_sentence = True
|
||||||
async for sentence in llm.run_llm_async_sentences(
|
messages = [
|
||||||
[
|
|
||||||
{
|
{
|
||||||
"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. Limit the description to one sentence, please."
|
"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 the description to one sentence, please.",
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
):
|
|
||||||
|
async for frame in SentenceAggregator().run(llm.run([QueueFrame(FrameType.LLM_MESSAGE, messages)])):
|
||||||
|
if type(frame.frame_data) != str:
|
||||||
|
raise Exception("LLM service requires a string for the data field")
|
||||||
|
|
||||||
|
sentence: str = frame.frame_data
|
||||||
image_text += sentence
|
image_text += sentence
|
||||||
|
|
||||||
if first_sentence:
|
if first_sentence:
|
||||||
@@ -100,18 +105,17 @@ async def main(room_url):
|
|||||||
# likely no delay between months, but the months won't display in order.
|
# likely no delay between months, but the months won't display in order.
|
||||||
for month_data_task in asyncio.as_completed(month_tasks):
|
for month_data_task in asyncio.as_completed(month_tasks):
|
||||||
data = await month_data_task
|
data = await month_data_task
|
||||||
print(f"got data, queueing frames...")
|
await transport.send_queue.put(
|
||||||
transport.output_queue.put(
|
|
||||||
[
|
[
|
||||||
QueueFrame(FrameType.IMAGE, data["image"]),
|
QueueFrame(FrameType.IMAGE, data["image"]),
|
||||||
QueueFrame(FrameType.AUDIO, data["audio"][0]),
|
QueueFrame(FrameType.AUDIO, data["audio"][0]),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
for audio in data["audio"][1:]:
|
for audio in data["audio"][1:]:
|
||||||
transport.output_queue.put(QueueFrame(FrameType.AUDIO, audio))
|
await transport.send_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()
|
||||||
|
|
||||||
month_tasks = [asyncio.create_task(get_month_data(month)) for month in months]
|
month_tasks = [asyncio.create_task(get_month_data(month)) for month in months]
|
||||||
|
|||||||
Reference in New Issue
Block a user