From 06396a70da52539559db847a51ec0c4fa43f2130 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Wed, 10 Apr 2024 18:21:26 -0600 Subject: [PATCH] make process_image async --- matrix_gpt/generate_clients/anthropic.py | 2 +- matrix_gpt/generate_clients/openai.py | 2 +- matrix_gpt/image.py | 10 ++++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/matrix_gpt/generate_clients/anthropic.py b/matrix_gpt/generate_clients/anthropic.py index ee85498..b0a666e 100644 --- a/matrix_gpt/generate_clients/anthropic.py +++ b/matrix_gpt/generate_clients/anthropic.py @@ -51,7 +51,7 @@ class AnthropicApiClient(ApiClient): async def append_img(self, img_event: RoomMessageImage, role: str): assert role in [self._HUMAN_NAME, self._BOT_NAME] img_bytes = await download_mxc(img_event.url, self._client_helper.client) - encoded_image = process_image(img_bytes, resize_px=784) + encoded_image = await process_image(img_bytes, resize_px=784) self._context.append({ "role": role, 'content': [{ diff --git a/matrix_gpt/generate_clients/openai.py b/matrix_gpt/generate_clients/openai.py index d0cc1c2..d99c4ae 100644 --- a/matrix_gpt/generate_clients/openai.py +++ b/matrix_gpt/generate_clients/openai.py @@ -27,7 +27,7 @@ class OpenAIClient(ApiClient): async def append_img(self, img_event: RoomMessageImage, role: str): assert role in [self._HUMAN_NAME, self._BOT_NAME] img_bytes = await download_mxc(img_event.url, self._client_helper.client) - encoded_image = process_image(img_bytes, resize_px=512) + encoded_image = await process_image(img_bytes, resize_px=512) self._context.append({ "role": role, 'content': [{ diff --git a/matrix_gpt/image.py b/matrix_gpt/image.py index 4796982..478e431 100644 --- a/matrix_gpt/image.py +++ b/matrix_gpt/image.py @@ -1,11 +1,13 @@ +import asyncio import base64 import io from PIL import Image -def process_image(source_bytes: bytes, resize_px: int): - image = Image.open(io.BytesIO(source_bytes)) +async def process_image(source_bytes: bytes, resize_px: int): + loop = asyncio.get_event_loop() + image = await loop.run_in_executor(None, Image.open, io.BytesIO(source_bytes)) width, height = image.size if min(width, height) > resize_px: @@ -15,9 +17,9 @@ def process_image(source_bytes: bytes, resize_px: int): else: new_height = resize_px new_width = int((width / height) * new_height) - image = image.resize((new_width, new_height)) + image = await loop.run_in_executor(None, image.resize, (new_width, new_height)) byte_arr = io.BytesIO() - image.save(byte_arr, format='PNG') + await loop.run_in_executor(None, image.save, byte_arr, 'PNG') image_bytes = byte_arr.getvalue() return base64.b64encode(image_bytes).decode('utf-8')