services: revert optional aiohttp.ClientSession

This commit is contained in:
Aleix Conchillo Flaqué
2024-08-01 18:22:56 -07:00
parent 6376c2f6aa
commit c891168ffb
7 changed files with 13 additions and 53 deletions

View File

@@ -17,10 +17,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- A few services required an `aiohttp.ClientSession` to be passed to the
constructor. This is now option an a new session will be created if none is
given.
- `BotSpeakingFrame` is now a control frame.
- `StartFrame` is now a control frame similar to `EndFrame`.

View File

@@ -175,8 +175,8 @@ class AzureImageGenServiceREST(ImageGenService):
api_key: str,
endpoint: str,
model: str,
aiohttp_session: aiohttp.ClientSession,
api_version="2023-06-01-preview",
aiohttp_session: aiohttp.ClientSession | None = None,
):
super().__init__()
@@ -185,13 +185,7 @@ class AzureImageGenServiceREST(ImageGenService):
self._api_version = api_version
self._model = model
self._image_size = image_size
self._aiohttp_session = aiohttp_session or aiohttp.ClientSession()
self._close_aiohttp_session = aiohttp_session is None
async def cleanup(self):
await super().cleanup()
if self._close_aiohttp_session:
await self._aiohttp_session.close()
self._aiohttp_session = aiohttp_session
async def run_image_gen(self, prompt: str) -> AsyncGenerator[Frame, None]:
url = f"{self._azure_endpoint}openai/images/generations:submit?api-version={self._api_version}"

View File

@@ -46,11 +46,11 @@ class DeepgramTTSService(TTSService):
self,
*,
api_key: str,
aiohttp_session: aiohttp.ClientSession,
voice: str = "aura-helios-en",
base_url: str = "https://api.deepgram.com/v1/speak",
sample_rate: int = 16000,
encoding: str = "linear16",
aiohttp_session: aiohttp.ClientSession | None = None,
**kwargs):
super().__init__(**kwargs)
@@ -59,17 +59,11 @@ class DeepgramTTSService(TTSService):
self._base_url = base_url
self._sample_rate = sample_rate
self._encoding = encoding
self._aiohttp_session = aiohttp_session or aiohttp.ClientSession()
self._close_aiohttp_session = aiohttp_session is None
self._aiohttp_session = aiohttp_session
def can_generate_metrics(self) -> bool:
return True
async def cleanup(self):
await super().cleanup()
if self._close_aiohttp_session:
await self._aiohttp_session.close()
async def set_voice(self, voice: str):
logger.debug(f"Switching TTS voice to: [{voice}]")
self._voice = voice

View File

@@ -21,25 +21,19 @@ class ElevenLabsTTSService(TTSService):
*,
api_key: str,
voice_id: str,
aiohttp_session: aiohttp.ClientSession,
model: str = "eleven_turbo_v2",
aiohttp_session: aiohttp.ClientSession | None = None,
**kwargs):
super().__init__(**kwargs)
self._api_key = api_key
self._voice_id = voice_id
self._model = model
self._aiohttp_session = aiohttp_session or aiohttp.ClientSession()
self._close_aiohttp_session = aiohttp_session is None
self._aiohttp_session = aiohttp_session
def can_generate_metrics(self) -> bool:
return True
async def cleanup(self):
await super().cleanup()
if self._close_aiohttp_session:
await self._aiohttp_session.close()
async def set_voice(self, voice: str):
logger.debug(f"Switching TTS voice to: [{voice}]")
self._voice_id = voice

View File

@@ -40,23 +40,17 @@ class FalImageGenService(ImageGenService):
self,
*,
params: InputParams,
aiohttp_session: aiohttp.ClientSession,
model: str = "fal-ai/fast-sdxl",
key: str | None = None,
aiohttp_session: aiohttp.ClientSession | None = None,
):
super().__init__()
self._model = model
self._params = params
self._aiohttp_session = aiohttp_session or aiohttp.ClientSession()
self._close_aiohttp_session = aiohttp_session is None
self._aiohttp_session = aiohttp_session
if key:
os.environ["FAL_KEY"] = key
async def cleanup(self):
await super().cleanup()
if self._close_aiohttp_session:
await self._aiohttp_session.close()
async def run_image_gen(self, prompt: str) -> AsyncGenerator[Frame, None]:
logger.debug(f"Generating image from prompt: {prompt}")

View File

@@ -254,21 +254,15 @@ class OpenAIImageGenService(ImageGenService):
self,
*,
api_key: str,
model: str = "dall-e-3",
aiohttp_session: aiohttp.ClientSession,
image_size: Literal["256x256", "512x512", "1024x1024", "1792x1024", "1024x1792"],
aiohttp_session: aiohttp.ClientSession | None = None,
model: str = "dall-e-3",
):
super().__init__()
self._model = model
self._image_size = image_size
self._client = AsyncOpenAI(api_key=api_key)
self._aiohttp_session = aiohttp_session or aiohttp.ClientSession()
self._close_aiohttp_session = aiohttp_session is None
async def cleanup(self):
await super().cleanup()
if self._close_aiohttp_session:
await self._aiohttp_session.close()
self._aiohttp_session = aiohttp_session
async def run_image_gen(self, prompt: str) -> AsyncGenerator[Frame, None]:
logger.debug(f"Generating image from prompt: {prompt}")

View File

@@ -39,7 +39,7 @@ class XTTSService(TTSService):
voice_id: str,
language: str,
base_url: str,
aiohttp_session: aiohttp.ClientSession | None = None,
aiohttp_session: aiohttp.ClientSession,
**kwargs):
super().__init__(**kwargs)
@@ -47,8 +47,7 @@ class XTTSService(TTSService):
self._language = language
self._base_url = base_url
self._studio_speakers: Dict[str, Any] | None = None
self._aiohttp_session = aiohttp_session or aiohttp.ClientSession()
self._close_aiohttp_session = aiohttp_session is None
self._aiohttp_session = aiohttp_session
def can_generate_metrics(self) -> bool:
return True
@@ -65,11 +64,6 @@ class XTTSService(TTSService):
return
self._studio_speakers = await r.json()
async def cleanup(self):
await super().cleanup()
if self._close_aiohttp_session:
await self._aiohttp_session.close()
async def set_voice(self, voice: str):
logger.debug(f"Switching TTS voice to: [{voice}]")
self._voice_id = voice