From 97a4cb8b7f07d9b202e80af29e45527e8fc2d185 Mon Sep 17 00:00:00 2001 From: Moishe Lettvin Date: Wed, 14 Feb 2024 12:16:48 -0500 Subject: [PATCH 1/2] Update playht tts service --- src/dailyai/services/playht_ai_service.py | 26 +++++++++++-------- src/examples/foundational/01-say-one-thing.py | 8 ++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/dailyai/services/playht_ai_service.py b/src/dailyai/services/playht_ai_service.py index 4ba9ddc86..ed09f8679 100644 --- a/src/dailyai/services/playht_ai_service.py +++ b/src/dailyai/services/playht_ai_service.py @@ -1,36 +1,40 @@ import io -import os import struct from pyht import Client -from dotenv import load_dotenv from pyht.client import TTSOptions from pyht.protos.api_pb2 import Format -from services.ai_service import AIService +from dailyai.services.ai_services import TTSService -class PlayHTAIService(AIService): - def __init__(self, **kwargs): - super().__init__(**kwargs) +class PlayHTAIService(TTSService): - self.speech_key = os.getenv("PLAY_HT_KEY") or '' - self.user_id = os.getenv("PLAY_HT_USER_ID") or '' + def __init__( + self, + *, + api_key, + user_id, + voice_url + ): + super().__init__() + + self.speech_key = api_key + self.user_id = user_id self.client = Client( user_id=self.user_id, api_key=self.speech_key, ) self.options = TTSOptions( - voice="s3://voice-cloning-zero-shot/820da3d2-3a3b-42e7-844d-e68db835a206/sarah/manifest.json", + voice=voice_url, sample_rate=16000, quality="higher", format=Format.FORMAT_WAV) def close(self): - super().close() self.client.close() - def run_tts(self, sentence): + async def run_tts(self, sentence): b = bytearray() in_header = True for chunk in self.client.tts(sentence, self.options): diff --git a/src/examples/foundational/01-say-one-thing.py b/src/examples/foundational/01-say-one-thing.py index 37136facf..cb81f023e 100644 --- a/src/examples/foundational/01-say-one-thing.py +++ b/src/examples/foundational/01-say-one-thing.py @@ -4,6 +4,7 @@ import os from dailyai.services.daily_transport_service import DailyTransportService from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService +from dailyai.services.playht_ai_service import PlayHTAIService from examples.foundational.support.runner import configure @@ -26,10 +27,17 @@ async def main(room_url): meeting_duration_minutes, mic_enabled=True ) + """ tts = ElevenLabsTTSService( aiohttp_session=session, api_key=os.getenv("ELEVENLABS_API_KEY"), voice_id=os.getenv("ELEVENLABS_VOICE_ID")) + """ + tts = PlayHTAIService( + api_key=os.getenv("PLAY_HT_API_KEY"), + user_id=os.getenv("PLAY_HT_USER_ID"), + voice_url=os.getenv("PLAY_HT_VOICE_URL"), + ) # Register an event handler so we can play the audio when the participant joins. @transport.event_handler("on_participant_joined") From dcbd79333abef1abc896bf8798874ecfd4014995 Mon Sep 17 00:00:00 2001 From: Moishe Lettvin Date: Wed, 14 Feb 2024 12:53:20 -0500 Subject: [PATCH 2/2] make destructor call client.close in PlayHT service --- src/dailyai/services/playht_ai_service.py | 2 +- src/examples/foundational/01-say-one-thing.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/dailyai/services/playht_ai_service.py b/src/dailyai/services/playht_ai_service.py index ed09f8679..350e5cb88 100644 --- a/src/dailyai/services/playht_ai_service.py +++ b/src/dailyai/services/playht_ai_service.py @@ -31,7 +31,7 @@ class PlayHTAIService(TTSService): quality="higher", format=Format.FORMAT_WAV) - def close(self): + def __del__(self): self.client.close() async def run_tts(self, sentence): diff --git a/src/examples/foundational/01-say-one-thing.py b/src/examples/foundational/01-say-one-thing.py index cb81f023e..9df54d708 100644 --- a/src/examples/foundational/01-say-one-thing.py +++ b/src/examples/foundational/01-say-one-thing.py @@ -27,6 +27,7 @@ async def main(room_url): meeting_duration_minutes, mic_enabled=True ) + """ tts = ElevenLabsTTSService( aiohttp_session=session, @@ -42,6 +43,7 @@ async def main(room_url): # Register an event handler so we can play the audio when the participant joins. @transport.event_handler("on_participant_joined") async def on_participant_joined(transport, participant): + nonlocal tts if participant["info"]["isLocal"]: return @@ -54,6 +56,7 @@ async def main(room_url): await transport.stop_when_done() await transport.run() + del(tts) if __name__ == "__main__":