diff --git a/vocode/streaming/agent/restful_user_implemented_agent.py b/vocode/streaming/agent/restful_user_implemented_agent.py index 212b8c919..126087dd0 100644 --- a/vocode/streaming/agent/restful_user_implemented_agent.py +++ b/vocode/streaming/agent/restful_user_implemented_agent.py @@ -33,7 +33,7 @@ async def respond( ) -> Tuple[Optional[str], bool]: config = self.agent_config.respond try: - # todo: cache session + # TODO: cache session async with aiohttp.ClientSession() as session: payload = RESTfulAgentInput( human_input=human_input, conversation_id=conversation_id diff --git a/vocode/streaming/synthesizer/azure_synthesizer.py b/vocode/streaming/synthesizer/azure_synthesizer.py index 9712d0ca8..23f81a64f 100644 --- a/vocode/streaming/synthesizer/azure_synthesizer.py +++ b/vocode/streaming/synthesizer/azure_synthesizer.py @@ -5,6 +5,7 @@ import re from typing import Any, List, Optional, Tuple from xml.etree import ElementTree +import aiohttp from vocode import getenv from opentelemetry.context.context import Context @@ -62,8 +63,9 @@ def __init__( logger: Optional[logging.Logger] = None, azure_speech_key: Optional[str] = None, azure_speech_region: Optional[str] = None, + aiohttp_session: Optional[aiohttp.ClientSession] = None, ): - super().__init__(synthesizer_config) + super().__init__(synthesizer_config, aiohttp_session) # Instantiates a client azure_speech_key = azure_speech_key or getenv("AZURE_SPEECH_KEY") azure_speech_region = azure_speech_region or getenv("AZURE_SPEECH_REGION") diff --git a/vocode/streaming/synthesizer/coqui_tts_synthesizer.py b/vocode/streaming/synthesizer/coqui_tts_synthesizer.py index c852a3570..d01c2df90 100644 --- a/vocode/streaming/synthesizer/coqui_tts_synthesizer.py +++ b/vocode/streaming/synthesizer/coqui_tts_synthesizer.py @@ -2,6 +2,7 @@ from concurrent.futures import ThreadPoolExecutor import logging from typing import Optional +import aiohttp from pydub import AudioSegment import numpy as np import io @@ -28,8 +29,9 @@ def __init__( self, synthesizer_config: CoquiTTSSynthesizerConfig, logger: Optional[logging.Logger] = None, + aiohttp_session: Optional[aiohttp.ClientSession] = None, ): - super().__init__(synthesizer_config) + super().__init__(synthesizer_config, aiohttp_session) from TTS.api import TTS diff --git a/vocode/streaming/synthesizer/eleven_labs_synthesizer.py b/vocode/streaming/synthesizer/eleven_labs_synthesizer.py index b7196a6e9..6de8261ee 100644 --- a/vocode/streaming/synthesizer/eleven_labs_synthesizer.py +++ b/vocode/streaming/synthesizer/eleven_labs_synthesizer.py @@ -29,8 +29,9 @@ def __init__( self, synthesizer_config: ElevenLabsSynthesizerConfig, logger: Optional[logging.Logger] = None, + aiohttp_session: Optional[aiohttp.ClientSession] = None, ): - super().__init__(synthesizer_config) + super().__init__(synthesizer_config, aiohttp_session) import elevenlabs diff --git a/vocode/streaming/synthesizer/factory.py b/vocode/streaming/synthesizer/factory.py index 729bb3e67..3c19c40ed 100644 --- a/vocode/streaming/synthesizer/factory.py +++ b/vocode/streaming/synthesizer/factory.py @@ -1,6 +1,7 @@ import logging from typing import Optional import typing +import aiohttp from vocode.streaming.models.synthesizer import ( AzureSynthesizerConfig, @@ -31,22 +32,39 @@ def create_synthesizer( self, synthesizer_config: SynthesizerConfig, logger: Optional[logging.Logger] = None, + aiohttp_session: Optional[aiohttp.ClientSession] = None, ): if isinstance(synthesizer_config, GoogleSynthesizerConfig): - return GoogleSynthesizer(synthesizer_config, logger=logger) + return GoogleSynthesizer( + synthesizer_config, logger=logger, aiohttp_session=aiohttp_session + ) elif isinstance(synthesizer_config, AzureSynthesizerConfig): - return AzureSynthesizer(synthesizer_config, logger=logger) + return AzureSynthesizer( + synthesizer_config, logger=logger, aiohttp_session=aiohttp_session + ) elif isinstance(synthesizer_config, ElevenLabsSynthesizerConfig): - return ElevenLabsSynthesizer(synthesizer_config, logger=logger) + return ElevenLabsSynthesizer( + synthesizer_config, logger=logger, aiohttp_session=aiohttp_session + ) elif isinstance(synthesizer_config, PlayHtSynthesizerConfig): - return PlayHtSynthesizer(synthesizer_config, logger=logger) + return PlayHtSynthesizer( + synthesizer_config, logger=logger, aiohttp_session=aiohttp_session + ) elif isinstance(synthesizer_config, RimeSynthesizerConfig): - return RimeSynthesizer(synthesizer_config, logger=logger) + return RimeSynthesizer( + synthesizer_config, logger=logger, aiohttp_session=aiohttp_session + ) elif isinstance(synthesizer_config, GTTSSynthesizerConfig): - return GTTSSynthesizer(synthesizer_config, logger=logger) + return GTTSSynthesizer( + synthesizer_config, logger=logger, aiohttp_session=aiohttp_session + ) elif isinstance(synthesizer_config, StreamElementsSynthesizerConfig): - return StreamElementsSynthesizer(synthesizer_config, logger=logger) + return StreamElementsSynthesizer( + synthesizer_config, logger=logger, aiohttp_session=aiohttp_session + ) elif isinstance(synthesizer_config, CoquiTTSSynthesizerConfig): - return CoquiTTSSynthesizer(synthesizer_config, logger=logger) + return CoquiTTSSynthesizer( + synthesizer_config, logger=logger, aiohttp_session=aiohttp_session + ) else: raise Exception("Invalid synthesizer config") diff --git a/vocode/streaming/synthesizer/google_synthesizer.py b/vocode/streaming/synthesizer/google_synthesizer.py index f7fba81ce..d0e438495 100644 --- a/vocode/streaming/synthesizer/google_synthesizer.py +++ b/vocode/streaming/synthesizer/google_synthesizer.py @@ -5,6 +5,7 @@ import os import wave from typing import Any, Optional +import aiohttp from vocode import getenv @@ -24,13 +25,13 @@ class GoogleSynthesizer(BaseSynthesizer[GoogleSynthesizerConfig]): - def __init__( self, synthesizer_config: GoogleSynthesizerConfig, logger: Optional[logging.Logger] = None, + aiohttp_session: Optional[aiohttp.ClientSession] = None, ): - super().__init__(synthesizer_config) + super().__init__(synthesizer_config, aiohttp_session) from google.cloud import texttospeech_v1beta1 as tts import google.auth diff --git a/vocode/streaming/synthesizer/gtts_synthesizer.py b/vocode/streaming/synthesizer/gtts_synthesizer.py index 598454839..a58c79cbe 100644 --- a/vocode/streaming/synthesizer/gtts_synthesizer.py +++ b/vocode/streaming/synthesizer/gtts_synthesizer.py @@ -1,6 +1,7 @@ import asyncio from concurrent.futures import ThreadPoolExecutor import logging +import aiohttp from pydub import AudioSegment from typing import Optional from io import BytesIO @@ -21,8 +22,9 @@ def __init__( self, synthesizer_config: GTTSSynthesizerConfig, logger: Optional[logging.Logger] = None, + aiohttp_session: Optional[aiohttp.ClientSession] = None, ): - super().__init__(synthesizer_config) + super().__init__(synthesizer_config, aiohttp_session) from gtts import gTTS diff --git a/vocode/streaming/synthesizer/play_ht_synthesizer.py b/vocode/streaming/synthesizer/play_ht_synthesizer.py index 6082fe882..a6b370cd7 100644 --- a/vocode/streaming/synthesizer/play_ht_synthesizer.py +++ b/vocode/streaming/synthesizer/play_ht_synthesizer.py @@ -27,8 +27,9 @@ def __init__( api_key: Optional[str] = None, user_id: Optional[str] = None, logger: Optional[logging.Logger] = None, + aiohttp_session: Optional[aiohttp.ClientSession] = None, ): - super().__init__(synthesizer_config) + super().__init__(synthesizer_config, aiohttp_session) self.synthesizer_config = synthesizer_config self.api_key = api_key or getenv("PLAY_HT_API_KEY") self.user_id = user_id or getenv("PLAY_HT_USER_ID") diff --git a/vocode/streaming/synthesizer/rime_synthesizer.py b/vocode/streaming/synthesizer/rime_synthesizer.py index 69d788737..4409c70ac 100644 --- a/vocode/streaming/synthesizer/rime_synthesizer.py +++ b/vocode/streaming/synthesizer/rime_synthesizer.py @@ -31,8 +31,9 @@ def __init__( self, synthesizer_config: RimeSynthesizerConfig, logger: Optional[logging.Logger] = None, + aiohttp_session: Optional[aiohttp.ClientSession] = None, ): - super().__init__(synthesizer_config) + super().__init__(synthesizer_config, aiohttp_session) self.api_key = getenv("RIME_API_KEY") self.speaker = synthesizer_config.speaker self.sampling_rate = synthesizer_config.sampling_rate diff --git a/vocode/streaming/synthesizer/stream_elements_synthesizer.py b/vocode/streaming/synthesizer/stream_elements_synthesizer.py index ba6627081..b95a27ee2 100644 --- a/vocode/streaming/synthesizer/stream_elements_synthesizer.py +++ b/vocode/streaming/synthesizer/stream_elements_synthesizer.py @@ -27,8 +27,9 @@ def __init__( self, synthesizer_config: StreamElementsSynthesizerConfig, logger: Optional[logging.Logger] = None, + aiohttp_session: Optional[aiohttp.ClientSession] = None, ): - super().__init__(synthesizer_config) + super().__init__(synthesizer_config, aiohttp_session) self.voice = synthesizer_config.voice async def create_speech(