Still phasing out requests module

This commit is contained in:
phixxy 2023-07-13 18:02:57 -07:00
parent a34bd10fba
commit 484caf1f74

View file

@ -190,31 +190,34 @@ async def look_at(ctx, look=False):
url = os.getenv('stablediffusion_url') url = os.getenv('stablediffusion_url')
if url == "disabled": if url == "disabled":
return return
for attachment in ctx.attachments: async with aiohttp.ClientSession() as session:
if attachment.url.endswith(('.jpg', '.png')): for attachment in ctx.attachments:
print("image seen") if attachment.url.endswith(('.jpg', '.png')):
print("image seen")
# see http://127.0.0.1:7860/docs for info, must be running stable diffusion with --api
r = requests.get(attachment.url, stream=True) async with session.get(attachment.url) as response:
imageName = "tmp/" + str(len(os.listdir('tmp/'))) + '.png'
imageName = "tmp/" + str(len(os.listdir('tmp/'))) + '.png'
with open(imageName, 'wb') as out_file:
with open(imageName, 'wb') as out_file: print('Saving image: ' + imageName)
print('Saving image: ' + imageName) while True:
shutil.copyfileobj(r.raw, out_file) chunk = await response.content.read(1024)
if not chunk:
break
out_file.write(chunk)
img_link = my_open_img_file(imageName) img_link = my_open_img_file(imageName)
try: try:
payload = {"image": img_link} payload = {"image": img_link}
response = requests.post(url=f'{url}/sdapi/v1/interrogate', json=payload) async with session.post(f'{url}/sdapi/v1/interrogate', json=payload) as response:
r = response.json() data = await response.json()
description = r.get("caption") description = data.get("caption")
description = description.split(',')[0] description = description.split(',')[0]
metadata += f"<image:{description}>\n" metadata += f"<image:{description}>\n"
except Exception as error: except aiohttp.ClientError as error:
await handle_error(error) await handle_error(error)
return "ERROR: CLIP may not be running. Could not look at image." return "ERROR: CLIP may not be running. Could not look at image."
return metadata return metadata