Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Kav-K/GPT3Discord
Browse files Browse the repository at this point in the history
  • Loading branch information
Kav-K committed Jan 1, 2023
2 parents 993913d + 17b7105 commit d9a7964
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions models/openai_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,10 @@ async def send_image_request(self, prompt, vary=None) -> tuple[File, list[Any]]:

# For each image url, open it as an image object using PIL
images = await asyncio.get_running_loop().run_in_executor(
None, lambda: [Image.open(requests.get(url, stream=True).raw) for url in image_urls]
None,
lambda: [
Image.open(requests.get(url, stream=True).raw) for url in image_urls
],
)

# Save all the images with a random name to self.IMAGE_SAVE_PATH
Expand Down Expand Up @@ -504,7 +507,9 @@ async def send_image_request(self, prompt, vary=None) -> tuple[File, list[Any]]:

# Save the new_im to a temporary file and return it as a discord.File
temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
await asyncio.get_running_loop().run_in_executor(None, new_im.save, temp_file.name)
await asyncio.get_running_loop().run_in_executor(
None, new_im.save, temp_file.name
)

# Print the filesize of new_im, in mega bytes
image_size = os.path.getsize(temp_file.name) / 1000000
Expand All @@ -522,11 +527,16 @@ async def send_image_request(self, prompt, vary=None) -> tuple[File, list[Any]]:
# We want to do this resizing asynchronously, so that it doesn't block the main thread during the resize.
# We can use the asyncio.run_in_executor method to do this
new_im = await asyncio.get_running_loop().run_in_executor(
None, functools.partial(new_im.resize, (int(new_im.width / 1.05), int(new_im.height / 1.05)))
None,
functools.partial(
new_im.resize, (int(new_im.width / 1.05), int(new_im.height / 1.05))
),
)

temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
await asyncio.get_running_loop().run_in_executor(None, new_im.save, temp_file.name)
await asyncio.get_running_loop().run_in_executor(
None, new_im.save, temp_file.name
)
image_size = os.path.getsize(temp_file.name) / 1000000
print(f"New image size is {image_size}MB")

Expand Down

0 comments on commit d9a7964

Please sign in to comment.