From 7b49c9ade37189b1aba3daa45416d1f67e2ce916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Thu, 11 Apr 2024 14:09:01 -0700 Subject: [PATCH] services: FalImageGenService now uses fal-client library --- dot-env.template | 3 +- examples/foundational/03-still-frame.py | 3 +- examples/foundational/03a-image-local.py | 3 +- .../foundational/05-sync-speech-and-image.py | 3 +- .../05a-local-sync-speech-and-text.py | 3 +- examples/foundational/08-bots-arguing.py | 3 +- examples/starter-apps/storybot.py | 3 +- pyproject.toml | 2 +- src/dailyai/services/fal_ai_services.py | 40 ++++++------------- 9 files changed, 21 insertions(+), 42 deletions(-) diff --git a/dot-env.template b/dot-env.template index 06b8a36eb..b50d48b16 100644 --- a/dot-env.template +++ b/dot-env.template @@ -22,8 +22,7 @@ ELEVENLABS_API_KEY=... ELEVENLABS_VOICE_ID=... # Fal -FAL_KEY_ID=... -FAL_KEY_SECRET=... +FAL_KEY=... # PlayHT PLAY_HT_USER_ID=... diff --git a/examples/foundational/03-still-frame.py b/examples/foundational/03-still-frame.py index 3e371da44..51ef47de8 100644 --- a/examples/foundational/03-still-frame.py +++ b/examples/foundational/03-still-frame.py @@ -35,8 +35,7 @@ async def main(room_url): image_size="square_hd" ), aiohttp_session=session, - key_id=os.getenv("FAL_KEY_ID"), - key_secret=os.getenv("FAL_KEY_SECRET"), + key=os.getenv("FAL_KEY"), ) pipeline = Pipeline([imagegen]) diff --git a/examples/foundational/03a-image-local.py b/examples/foundational/03a-image-local.py index 1263ebd5f..f213f505b 100644 --- a/examples/foundational/03a-image-local.py +++ b/examples/foundational/03a-image-local.py @@ -39,8 +39,7 @@ async def main(): image_size="square_hd" ), aiohttp_session=session, - key_id=os.getenv("FAL_KEY_ID"), - key_secret=os.getenv("FAL_KEY_SECRET"), + key=os.getenv("FAL_KEY"), ) pipeline = Pipeline([imagegen]) diff --git a/examples/foundational/05-sync-speech-and-image.py b/examples/foundational/05-sync-speech-and-image.py index c036f06f6..377e8579b 100644 --- a/examples/foundational/05-sync-speech-and-image.py +++ b/examples/foundational/05-sync-speech-and-image.py @@ -89,8 +89,7 @@ async def main(room_url): image_size="square_hd" ), aiohttp_session=session, - key_id=os.getenv("FAL_KEY_ID"), - key_secret=os.getenv("FAL_KEY_SECRET"), + key=os.getenv("FAL_KEY"), ) gated_aggregator = GatedAggregator( diff --git a/examples/foundational/05a-local-sync-speech-and-text.py b/examples/foundational/05a-local-sync-speech-and-text.py index 9410f0601..7c4cf0186 100644 --- a/examples/foundational/05a-local-sync-speech-and-text.py +++ b/examples/foundational/05a-local-sync-speech-and-text.py @@ -49,8 +49,7 @@ async def main(): image_size="1024x1024" ), aiohttp_session=session, - key_id=os.getenv("FAL_KEY_ID"), - key_secret=os.getenv("FAL_KEY_SECRET"), + key=os.getenv("FAL_KEY"), ) # Get a complete audio chunk from the given text. Splitting this into its own diff --git a/examples/foundational/08-bots-arguing.py b/examples/foundational/08-bots-arguing.py index 82b7c0f81..ea6208827 100644 --- a/examples/foundational/08-bots-arguing.py +++ b/examples/foundational/08-bots-arguing.py @@ -55,8 +55,7 @@ async def main(room_url: str): image_size="1024x1024" ), aiohttp_session=session, - key_id=os.getenv("FAL_KEY_ID"), - key_secret=os.getenv("FAL_KEY_SECRET"), + key=os.getenv("FAL_KEY"), ) bot1_messages = [ diff --git a/examples/starter-apps/storybot.py b/examples/starter-apps/storybot.py index 3d54c4dd1..69be94095 100644 --- a/examples/starter-apps/storybot.py +++ b/examples/starter-apps/storybot.py @@ -208,8 +208,7 @@ async def main(room_url: str, token): image_size = "1024x1024", }, aiohttp_session=session, - key_id=os.getenv("FAL_KEY_ID"), - key_secret=os.getenv("FAL_KEY_SECRET"), + key=os.getenv("FAL_KEY"), ) lra = LLMAssistantResponseAggregator(messages) ura = LLMUserResponseAggregator(messages) diff --git a/pyproject.toml b/pyproject.toml index c42452d4d..920c9aa8c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ anthropic = [ "anthropic~=0.20.0" ] azure = [ "azure-cognitiveservices-speech~=1.36.0" ] daily = [ "daily-python~=0.7.0" ] examples = [ "python-dotenv~=1.0.0", "flask~=3.0.0", "flask_cors~=4.0.0" ] -fal = [ "fal~=0.12.0" ] +fal = [ "fal-client~=0.2.0" ] local = [ "pyaudio~=0.2.0" ] moondream = [ "einops~=0.7.0", "timm~=0.9.0", "transformers~=4.39.0" ] openai = [ "openai~=1.14.0" ] diff --git a/src/dailyai/services/fal_ai_services.py b/src/dailyai/services/fal_ai_services.py index 5c1d15151..a924607d2 100644 --- a/src/dailyai/services/fal_ai_services.py +++ b/src/dailyai/services/fal_ai_services.py @@ -6,14 +6,15 @@ from PIL import Image from pydantic import BaseModel from typing import Optional, Union, Dict + from dailyai.services.ai_services import ImageGenService try: - import fal + import fal_client except ModuleNotFoundError as e: print(f"Exception: {e}") print( - "In order to use Fal, you need to `pip install dailyai[fal]`. Also, set `FAL_KEY_ID` and `FAL_KEY_SECRET` environment variables.") + "In order to use Fal, you need to `pip install dailyai[fal]`. Also, set `FAL_KEY` environment variable.") raise Exception(f"Missing module: {e}") @@ -33,40 +34,25 @@ class FalImageGenService(ImageGenService): aiohttp_session: aiohttp.ClientSession, params: InputParams, model="fal-ai/fast-sdxl", - key_id=None, - key_secret=None + key=None, ): super().__init__() self._model = model self._params = params self._aiohttp_session = aiohttp_session - if key_id: - os.environ["FAL_KEY_ID"] = key_id - if key_secret: - os.environ["FAL_KEY_SECRET"] = key_secret + if key: + os.environ["FAL_KEY"] = key async def run_image_gen(self, prompt: str) -> tuple[str, bytes, tuple[int, int]]: - def get_image_url(prompt): - handler = fal.apps.submit( # type: ignore - self._model, - arguments={ - "prompt": prompt, - **self._params.dict(), - }, - ) - for event in handler.iter_events(): - if isinstance(event, fal.apps.InProgress): # type: ignore - pass + response = await fal_client.run_async( + self._model, + arguments={"prompt": prompt, **self._params.dict()} + ) - result = handler.get() + image_url = response["images"][0]["url"] if response else None - image_url = result["images"][0]["url"] if result else None - if not image_url: - raise Exception("Image generation failed") - - return image_url - - image_url = await asyncio.to_thread(get_image_url, prompt) + if not image_url: + raise Exception("Image generation failed") # Load the image from the url async with self._aiohttp_session.get(image_url) as response: