diff --git a/src/dailyai/services/deepgram_ai_service.py b/src/dailyai/services/deepgram_ai_service.py new file mode 100644 index 000000000..1ede1c35d --- /dev/null +++ b/src/dailyai/services/deepgram_ai_service.py @@ -0,0 +1,36 @@ +import os +import aiohttp +import requests + +from dailyai.services.ai_services import TTSService + + +class DeepgramAIService(TTSService): + def __init__( + self, + *, + aiohttp_session: aiohttp.ClientSession, + api_key, + voice, + sample_rate=16000 + ): + super().__init__() + + self._api_key = api_key + self._voice = voice + self._sample_rate = sample_rate + self._aiohttp_session = aiohttp_session + + async def run_tts(self, sentence): + self.logger.info(f"Running deepgram tts for {sentence}") + base_url = "https://api.beta.deepgram.com/v1/speak" + request_url = f"{base_url}?model={self._voice}&encoding=linear16&container=none&sample_rate={self._sample_rate}" + headers = {"authorization": f"token {self._api_key}", "Content-Type": "application/json"} + data = {"text": sentence} + + async with self._aiohttp_session.post( + request_url, headers=headers, json=data + ) as r: + async for chunk in r.content: + if chunk: + yield chunk diff --git a/src/dailyai/services/to_be_updated/deepgram_ai_service.py b/src/dailyai/services/to_be_updated/deepgram_ai_service.py deleted file mode 100644 index b4569e6cd..000000000 --- a/src/dailyai/services/to_be_updated/deepgram_ai_service.py +++ /dev/null @@ -1,29 +0,0 @@ -import os -import requests - -from services.ai_service import AIService -from PIL import Image - - -class DeepgramAIService(AIService): - def __init__(self, **kwargs): - super().__init__(**kwargs) - - self.api_key = os.getenv("DEEPGRAM_API_KEY") - - def get_mic_sample_rate(self): - return 24000 - - def run_tts(self, sentence): - self.logger.info(f"Running deepgram tts for {sentence}") - base_url = "https://api.beta.deepgram.com/v1/speak" - # move this to an environment variable - voice = os.getenv("DEEPGRAM_VOICE") or "alpha-apollo-en-v1" - request_url = f"{base_url}?model={voice}&encoding=linear16&container=none" - headers = {"authorization": f"token {self.api_key}"} - - r = requests.post(request_url, headers=headers, data=sentence) - self.logger.info( - f"audio fetch status code: {r.status_code}, content length: {len(r.content)}" - ) - yield r.content