FalImageGenService: load image async to not block the event loop
This commit is contained in:
@@ -105,6 +105,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
- Fixed a `FalImageGenService` issue that was causing the event loop to be
|
||||||
|
blocked while loading the downloadded image.
|
||||||
|
|
||||||
- Fixed a `CartesiaTTSService` service issue that would cause audio overlapping
|
- Fixed a `CartesiaTTSService` service issue that would cause audio overlapping
|
||||||
in some cases.
|
in some cases.
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
# SPDX-License-Identifier: BSD 2-Clause License
|
# SPDX-License-Identifier: BSD 2-Clause License
|
||||||
#
|
#
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
from typing import AsyncGenerator, Dict, Optional, Union
|
from typing import AsyncGenerator, Dict, Optional, Union
|
||||||
@@ -53,6 +54,11 @@ class FalImageGenService(ImageGenService):
|
|||||||
os.environ["FAL_KEY"] = key
|
os.environ["FAL_KEY"] = key
|
||||||
|
|
||||||
async def run_image_gen(self, prompt: str) -> AsyncGenerator[Frame, None]:
|
async def run_image_gen(self, prompt: str) -> AsyncGenerator[Frame, None]:
|
||||||
|
def load_image_bytes(encoded_image: bytes):
|
||||||
|
buffer = io.BytesIO(encoded_image)
|
||||||
|
image = Image.open(buffer)
|
||||||
|
return (image.tobytes(), image.size, image.format)
|
||||||
|
|
||||||
logger.debug(f"Generating image from prompt: {prompt}")
|
logger.debug(f"Generating image from prompt: {prompt}")
|
||||||
|
|
||||||
response = await fal_client.run_async(
|
response = await fal_client.run_async(
|
||||||
@@ -73,10 +79,8 @@ class FalImageGenService(ImageGenService):
|
|||||||
logger.debug(f"Downloading image {image_url} ...")
|
logger.debug(f"Downloading image {image_url} ...")
|
||||||
async with self._aiohttp_session.get(image_url) as response:
|
async with self._aiohttp_session.get(image_url) as response:
|
||||||
logger.debug(f"Downloaded image {image_url}")
|
logger.debug(f"Downloaded image {image_url}")
|
||||||
image_stream = io.BytesIO(await response.content.read())
|
encoded_image = await response.content.read()
|
||||||
image = Image.open(image_stream)
|
(image_bytes, size, format) = await asyncio.to_thread(load_image_bytes, encoded_image)
|
||||||
|
|
||||||
frame = URLImageRawFrame(
|
frame = URLImageRawFrame(url=image_url, image=image_bytes, size=size, format=format)
|
||||||
url=image_url, image=image.tobytes(), size=image.size, format=image.format
|
|
||||||
)
|
|
||||||
yield frame
|
yield frame
|
||||||
|
|||||||
Reference in New Issue
Block a user