Added clearer error notifications

This commit is contained in:
phixxy 2023-07-16 16:40:49 -07:00
parent d4cca24392
commit f02263a9b4

View file

@ -1255,6 +1255,7 @@ async def imagine(ctx):
async with session.post(url, headers=headers, json=payload) as resp: async with session.post(url, headers=headers, json=payload) as resp:
r = await resp.json() r = await resp.json()
except Exception as error: except Exception as error:
await ctx.send("My image generation service may not be running.")
await handle_error(error) await handle_error(error)
for i in r['images']: for i in r['images']:
@ -1269,6 +1270,7 @@ async def imagine(ctx):
async with session.post(url, json=png_payload) as resp: async with session.post(url, json=png_payload) as resp:
response2 = await resp.json() response2 = await resp.json()
except Exception as error: except Exception as error:
await ctx.send("My image generation service may not be running.")
await handle_error(error) await handle_error(error)
pnginfo = PngImagePlugin.PngInfo() pnginfo = PngImagePlugin.PngInfo()
@ -1354,16 +1356,21 @@ async def reimagine(ctx):
key_value_pairs, prompt = extract_key_value_pairs(prompt) key_value_pairs, prompt = extract_key_value_pairs(prompt)
async with aiohttp.ClientSession() as session: try:
async with session.get(file_url) as response: async with aiohttp.ClientSession() as session:
imageName = "tmp/" + str(len(os.listdir("tmp/"))) + ".png" async with session.get(file_url) as response:
with open(imageName, 'wb') as out_file: imageName = "tmp/" + str(len(os.listdir("tmp/"))) + ".png"
print(f"Saving image: {imageName}") with open(imageName, 'wb') as out_file:
while True: print(f"Saving image: {imageName}")
chunk = await response.content.read(1024) while True:
if not chunk: chunk = await response.content.read(1024)
break if not chunk:
out_file.write(chunk) break
out_file.write(chunk)
except Exception as error:
await ctx.send("My image generation service may not be running.")
await handle_error(error)
img_link = my_open_img_file(imageName) img_link = my_open_img_file(imageName)
@ -1375,23 +1382,27 @@ async def reimagine(ctx):
payload = {"init_images": [img_link], "prompt": prompt, "steps": 40, "negative_prompt": negative_prompt, "denoising_strength": 0.5} payload = {"init_images": [img_link], "prompt": prompt, "steps": 40, "negative_prompt": negative_prompt, "denoising_strength": 0.5}
payload = combine_dicts(payload, key_value_pairs) payload = combine_dicts(payload, key_value_pairs)
async with aiohttp.ClientSession() as session: try:
async with session.post(url=f'{url}/sdapi/v1/img2img', json=payload) as response: async with aiohttp.ClientSession() as session:
data = await response.json() async with session.post(url=f'{url}/sdapi/v1/img2img', json=payload) as response:
for i in data['images']: data = await response.json()
if not os.path.isdir("tmp/reimagined/"+ str(ctx.author.id)): for i in data['images']:
os.makedirs("tmp/reimagined/"+ str(ctx.author.id)) if not os.path.isdir("tmp/reimagined/"+ str(ctx.author.id)):
image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0]))) os.makedirs("tmp/reimagined/"+ str(ctx.author.id))
png_payload = {"image": "data:image/png;base64," + i} image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0])))
async with session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload) as resp2: png_payload = {"image": "data:image/png;base64," + i}
response2 = await resp2.json() async with session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload) as resp2:
pnginfo = PngImagePlugin.PngInfo() response2 = await resp2.json()
pnginfo.add_text("parameters", response2.get("info")) pnginfo = PngImagePlugin.PngInfo()
my_filename = "tmp/" + str(len(os.listdir("tmp/"))) + ".png" pnginfo.add_text("parameters", response2.get("info"))
image.save(my_filename, pnginfo=pnginfo) my_filename = "tmp/" + str(len(os.listdir("tmp/"))) + ".png"
with open(my_filename, "rb") as fh: image.save(my_filename, pnginfo=pnginfo)
f = discord.File(fh, filename=my_filename) with open(my_filename, "rb") as fh:
await ctx.send(file=f) f = discord.File(fh, filename=my_filename)
await ctx.send(file=f)
except Exception as error:
await ctx.send("My image generation service may not be running.")
await handle_error(error)
@bot.command() @bot.command()