services: don't use image_size in ImageGenService
This commit is contained in:
@@ -82,13 +82,12 @@ class TTSService(AIService):
|
|||||||
|
|
||||||
|
|
||||||
class ImageGenService(AIService):
|
class ImageGenService(AIService):
|
||||||
def __init__(self, image_size, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
self.image_size = image_size
|
|
||||||
|
|
||||||
# Renders the image. Returns an Image object.
|
# Renders the image. Returns an Image object.
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def run_image_gen(self, sentence: str) -> tuple[str, bytes, tuple[int, int]]:
|
async def run_image_gen(self, prompt: str) -> tuple[str, bytes, tuple[int, int]]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def process_frame(self, frame: Frame) -> AsyncGenerator[Frame, None]:
|
async def process_frame(self, frame: Frame) -> AsyncGenerator[Frame, None]:
|
||||||
|
|||||||
@@ -97,23 +97,24 @@ class AzureImageGenServiceREST(ImageGenService):
|
|||||||
endpoint,
|
endpoint,
|
||||||
model,
|
model,
|
||||||
):
|
):
|
||||||
super().__init__(image_size=image_size)
|
super().__init__()
|
||||||
|
|
||||||
self._api_key = api_key
|
self._api_key = api_key
|
||||||
self._azure_endpoint = endpoint
|
self._azure_endpoint = endpoint
|
||||||
self._api_version = api_version
|
self._api_version = api_version
|
||||||
self._model = model
|
self._model = model
|
||||||
self._aiohttp_session = aiohttp_session
|
self._aiohttp_session = aiohttp_session
|
||||||
|
self._image_size = image_size
|
||||||
|
|
||||||
async def run_image_gen(self, sentence) -> tuple[str, bytes, tuple[int, int]]:
|
async def run_image_gen(self, prompt: str) -> tuple[str, bytes, tuple[int, int]]:
|
||||||
url = f"{self._azure_endpoint}openai/images/generations:submit?api-version={self._api_version}"
|
url = f"{self._azure_endpoint}openai/images/generations:submit?api-version={self._api_version}"
|
||||||
headers = {
|
headers = {
|
||||||
"api-key": self._api_key,
|
"api-key": self._api_key,
|
||||||
"Content-Type": "application/json"}
|
"Content-Type": "application/json"}
|
||||||
body = {
|
body = {
|
||||||
# Enter your prompt text here
|
# Enter your prompt text here
|
||||||
"prompt": sentence,
|
"prompt": prompt,
|
||||||
"size": self.image_size,
|
"size": self._image_size,
|
||||||
"n": 1,
|
"n": 1,
|
||||||
}
|
}
|
||||||
async with self._aiohttp_session.post(
|
async with self._aiohttp_session.post(
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class FalImageGenService(ImageGenService):
|
|||||||
if key_secret:
|
if key_secret:
|
||||||
os.environ["FAL_KEY_SECRET"] = key_secret
|
os.environ["FAL_KEY_SECRET"] = key_secret
|
||||||
|
|
||||||
async def run_image_gen(self, prompt) -> tuple[str, bytes]:
|
async def run_image_gen(self, prompt: str) -> tuple[str, bytes, tuple[int, int]]:
|
||||||
def get_image_url(prompt):
|
def get_image_url(prompt):
|
||||||
handler = fal.apps.submit( # type: ignore
|
handler = fal.apps.submit( # type: ignore
|
||||||
self._model,
|
self._model,
|
||||||
@@ -72,4 +72,4 @@ class FalImageGenService(ImageGenService):
|
|||||||
async with self._aiohttp_session.get(image_url) as response:
|
async with self._aiohttp_session.get(image_url) as response:
|
||||||
image_stream = io.BytesIO(await response.content.read())
|
image_stream = io.BytesIO(await response.content.read())
|
||||||
image = Image.open(image_stream)
|
image = Image.open(image_stream)
|
||||||
return (image_url, image.tobytes())
|
return (image_url, image.tobytes(), image.size)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from typing import Literal
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import io
|
import io
|
||||||
@@ -26,24 +27,25 @@ class OpenAIImageGenService(ImageGenService):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
image_size: str,
|
image_size: Literal["256x256", "512x512", "1024x1024", "1792x1024", "1024x1792"],
|
||||||
aiohttp_session: aiohttp.ClientSession,
|
aiohttp_session: aiohttp.ClientSession,
|
||||||
api_key,
|
api_key,
|
||||||
model="dall-e-3",
|
model="dall-e-3",
|
||||||
):
|
):
|
||||||
super().__init__(image_size=image_size)
|
super().__init__()
|
||||||
self._model = model
|
self._model = model
|
||||||
|
self._image_size = image_size
|
||||||
self._client = AsyncOpenAI(api_key=api_key)
|
self._client = AsyncOpenAI(api_key=api_key)
|
||||||
self._aiohttp_session = aiohttp_session
|
self._aiohttp_session = aiohttp_session
|
||||||
|
|
||||||
async def run_image_gen(self, sentence) -> tuple[str, bytes, tuple[int, int]]:
|
async def run_image_gen(self, prompt: str) -> tuple[str, bytes, tuple[int, int]]:
|
||||||
self.logger.info("Generating OpenAI image", sentence)
|
self.logger.info("Generating OpenAI image", prompt)
|
||||||
|
|
||||||
image = await self._client.images.generate(
|
image = await self._client.images.generate(
|
||||||
prompt=sentence,
|
prompt=prompt,
|
||||||
model=self._model,
|
model=self._model,
|
||||||
n=1,
|
n=1,
|
||||||
size=self.image_size
|
size=self._image_size
|
||||||
)
|
)
|
||||||
image_url = image.data[0].url
|
image_url = image.data[0].url
|
||||||
if not image_url:
|
if not image_url:
|
||||||
|
|||||||
Reference in New Issue
Block a user