services: FalImageGenService now uses fal-client library

This commit is contained in:
Aleix Conchillo Flaqué
2024-04-11 14:09:01 -07:00
parent 1e83a405c0
commit 7b49c9ade3
9 changed files with 21 additions and 42 deletions

View File

@@ -22,8 +22,7 @@ ELEVENLABS_API_KEY=...
ELEVENLABS_VOICE_ID=...
# Fal
FAL_KEY_ID=...
FAL_KEY_SECRET=...
FAL_KEY=...
# PlayHT
PLAY_HT_USER_ID=...

View File

@@ -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])

View File

@@ -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])

View File

@@ -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(

View File

@@ -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

View File

@@ -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 = [

View File

@@ -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)

View File

@@ -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" ]

View File

@@ -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: