Still phasing out requests module
This commit is contained in:
parent
a34bd10fba
commit
484caf1f74
1 changed files with 27 additions and 24 deletions
|
|
@ -190,29 +190,32 @@ 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
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
for attachment in ctx.attachments:
|
for attachment in ctx.attachments:
|
||||||
if attachment.url.endswith(('.jpg', '.png')):
|
if attachment.url.endswith(('.jpg', '.png')):
|
||||||
print("image seen")
|
print("image seen")
|
||||||
|
|
||||||
# see http://127.0.0.1:7860/docs for info, must be running stable diffusion with --api
|
async with session.get(attachment.url) as response:
|
||||||
r = requests.get(attachment.url, stream=True)
|
|
||||||
|
|
||||||
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)
|
||||||
shutil.copyfileobj(r.raw, out_file)
|
while True:
|
||||||
|
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."
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue