diff --git a/dalle.py b/dalle.py index 8eb2546..c81e889 100644 --- a/dalle.py +++ b/dalle.py @@ -1,10 +1,9 @@ import json import urllib -import requests import io import base64 import asyncio -import requests +import aiohttp import re from PIL import Image @@ -107,29 +106,29 @@ class Dalle(Plugin): "prompt": prompt } - response = requests.post("https://nexra.aryahcr.cc/api/image/complements", headers=headers, data=json.dumps(data)) + async with aiohttp.request("POST", "https://nexra.aryahcr.cc/api/image/complements", headers=headers, data=json.dumps(data)) as response: - if response.status_code != 200: - self.log.warning(f"Unexpected status sending request to nexra.aryahcr.cc: {response.status_code}") - return + if response.status != 200: + self.log.warning(f"Unexpected status sending request to nexra.aryahcr.cc: {response.status_code}") + return + + count = -1 + response_text = await response.text() + for i in range(len(response_text)): + if count <= -1: + if response_text[i] == "{": + count = i + else: + break - count = -1 - for i in range(len(response.text)): - if count <= -1: - if response.text[i] == "{": - count = i - else: - break - response_json = json.loads(response.text[count:]) + response_json = json.loads(response_text[count:]) + content = response_json['images'][0] - content = response_json['images'][0] - - base64_str = content.replace("data:image/jpeg;base64,", "") - mem_file = io.BytesIO(base64.decodebytes(bytes(base64_str, "utf-8"))) - - pil_image = Image.open(mem_file) - pil_image.save(mem_file, format=pil_image.format) - mem_file.seek(0) - - return mem_file + base64_str = content.replace("data:image/jpeg;base64,", "") + mem_file = io.BytesIO(base64.decodebytes(bytes(base64_str, "utf-8"))) + + pil_image = Image.open(mem_file) + pil_image.save(mem_file, format=pil_image.format) + mem_file.seek(0) + return mem_file diff --git a/maubot.yaml b/maubot.yaml index 88a283a..2714834 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -1,6 +1,6 @@ maubot: 0.1.0 id: sh.boehm.dalle -version: 0.0.029 +version: 0.0.03 license: MIT modules: - dalle