made a few more functions use aiohttp

This commit is contained in:
phixxy 2023-07-13 17:29:40 -07:00
parent 26dc334db7
commit a34bd10fba

View file

@ -1184,64 +1184,38 @@ async def personality(ctx):
@bot.command() @bot.command()
async def change_model(ctx, model_choice='0'): async def change_model(ctx, model_choice='0'):
model_choices = {
'1': ("deliberate_v2.safetensors [9aba26abdf]", "DeliberateV2"),
'2': ("AnythingV5_v5PrtRE.safetensors [7f96a1a9ca]", "AnythingV5"),
'3': ("Anything-V3.0.ckpt [8712e20a5d]", "AnythingV3")
}
url = os.getenv('stablediffusion_url') url = os.getenv('stablediffusion_url')
if url == "disabled": if url == "disabled":
await ctx.send("This command is currently disabled")
return return
response = requests.get(url=f'{url}/sdapi/v1/options')
config_json = response.json() async with aiohttp.ClientSession() as session:
async with session.get(url=f'{url}/sdapi/v1/options') as response:
config_json = await response.json()
current_model = config_json["sd_model_checkpoint"] current_model = config_json["sd_model_checkpoint"]
output = 'Current Model: ' + current_model + '\n' output = 'Current Model: ' + current_model + '\n'
if model_choice == '1': if model_choice in model_choices:
if current_model != "deliberate_v2.safetensors [9aba26abdf]": model_id, model_name = model_choices[model_choice]
model_name = "deliberate_v2.safetensors [9aba26abdf]" if current_model != model_id:
else: payload = {"sd_model_checkpoint": model_id}
await ctx.send("Already set to use DeliberateV2") async with session.post(url=f'{url}/sdapi/v1/options', json=payload) as response:
return
elif model_choice == '2':
if current_model != "AnythingV5_v5PrtRE.safetensors [7f96a1a9ca]":
model_name = "AnythingV5_v5PrtRE.safetensors [7f96a1a9ca]"
else:
await ctx.send("Already set to use AnythingV5")
return
elif model_choice == '3':
if current_model != "Anything-V3.0.ckpt [8712e20a5d]":
model_name = "Anything-V3.0.ckpt [8712e20a5d]"
else:
await ctx.send("Already set to use AnythingV3")
return
else:
output += "1: DeliberateV2\n2: AnythingV5\n3: AnythingV3"
await ctx.send(output)
return
payload = {"sd_model_checkpoint": model_name}
response = requests.post(url=f'{url}/sdapi/v1/options', json=payload)
output = "Changed model to: " + model_name output = "Changed model to: " + model_name
await ctx.send(output) await ctx.send(output)
return
async def answer_question(topic, model="gpt-3.5-turbo"): else:
headers = { await ctx.send(f"Already set to use {model_name}")
'Content-Type': 'application/json', return
'Authorization': f'Bearer {os.getenv("openai.api_key")}', else:
} modeloptions = '\n'.join([f"{choice}: {name}" for choice, (, name) in model_choices.items()])
output += model_options
data = { await ctx.send(output)
"model": model,
"messages": [{"role": "user", "content": topic}]
}
url = "https://api.openai.com/v1/chat/completions"
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=data) as resp:
response_data = await resp.json()
response = response_data['choices'][0]['message']['content']
return response
except Exception as error:
return await handle_error(error)
@bot.command() @bot.command()
async def imagine(ctx): async def imagine(ctx):
@ -1271,11 +1245,6 @@ async def imagine(ctx):
except Exception as error: except Exception as error:
await handle_error(error) await handle_error(error)
#try:
# response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
# r = response.json()
for i in r['images']: for i in r['images']:
if not os.path.isdir("users/" + str(ctx.author.id)): if not os.path.isdir("users/" + str(ctx.author.id)):
os.makedirs("users/" + str(ctx.author.id)) os.makedirs("users/" + str(ctx.author.id))
@ -1285,11 +1254,11 @@ async def imagine(ctx):
try: try:
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=data) 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 handle_error(error) await handle_error(error)
#response2 = requests.post(url=f'{url}/sdapi/v1/png-info', json=png_payload)
pnginfo = PngImagePlugin.PngInfo() pnginfo = PngImagePlugin.PngInfo()
pnginfo.add_text("parameters", response2.json().get("info")) pnginfo.add_text("parameters", response2.json().get("info"))
@ -1305,9 +1274,6 @@ async def imagine(ctx):
f = discord.File(fh, filename=my_filename) f = discord.File(fh, filename=my_filename)
await ctx.send(file=f) await ctx.send(file=f)
#except Exception as error:
# await handle_error(error)
# await ctx.send("My image generation service may not be running.")
@bot.command() @bot.command()
async def describe(ctx): async def describe(ctx):
@ -1328,19 +1294,22 @@ async def describe(ctx):
print("Couldn't find image.") print("Couldn't find image.")
return return
r = requests.get(file_url, stream=True) async with aiohttp.ClientSession() as session:
async with session.get(file_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(f"Saving image: {imageName}") print(f"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(url=f"{url}/sdapi/v1/interrogate", json=payload) as response:
r = response.json() r = await response.json()
await ctx.send(r.get("caption")) await ctx.send(r.get("caption"))
except Exception as error: except Exception as error:
await handle_error(error) await handle_error(error)
@ -1348,50 +1317,51 @@ async def describe(ctx):
@bot.command() @bot.command()
async def reimagine(ctx): async def reimagine(ctx):
#see http://127.0.0.1:7860/docs for info, must be running stable diffusion with --api
url = os.getenv('stablediffusion_url') url = os.getenv('stablediffusion_url')
if url == "disabled": if url == "disabled":
await ctx.send("Command is currently disabled") await ctx.send("Command is currently disabled")
return return
try:
try:
file_url = ctx.message.content.split(" ", maxsplit=2)[1] file_url = ctx.message.content.split(" ", maxsplit=2)[1]
prompt = ctx.message.content.split(" ", maxsplit=2)[2] prompt = ctx.message.content.split(" ", maxsplit=2)[2]
except Exception as error:
await handle_error(error) await handle_error(error)
print("no linked image") print("no linked image")
try:
file_url = ctx.message.attachments[0].url file_url = ctx.message.attachments[0].url
prompt = ctx.message.content.split(" ", maxsplit=1)[1] prompt = ctx.message.content.split(" ", maxsplit=1)[1]
except Exception as error:
await handle_error(error)
print("no attached image")
except Exception as error:
await handle_error(error)
print("couldn't find image")
key_value_pairs, prompt = extract_key_value_pairs(prompt) key_value_pairs, prompt = extract_key_value_pairs(prompt)
r = requests.get(file_url, stream=True)
async with aiohttp.ClientSession() as session:
async with session.get(file_url, stream=True) 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)
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)
negative_prompt = "badhandsv4, worst quality, lowres, EasyNegative, hermaphrodite, cropped, not in the frame, additional faces, jpeg large artifacts, jpeg small artifacts, ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, extra limbs, disfigured, deformed, body out of frame, blurry, bad anatomy, blurred, watermark, grainy, signature, cut off, draft, not finished drawing, unfinished image, bad eyes, doll, 3d, cartoon, (bad eyes:1.2), (worst quality:1.2), (low quality:1.2), bad-image-v2-39000, (bad_prompt_version2:0.8), nude, badhandv4 By bad artist -neg easynegative ng_deepnegative_v1_75t verybadimagenegative_v1.3, (Worst Quality, Low Quality:1.4), Poorly Made Bad 3D, Lousy Bad Realistic, nsfw,(worst quality, low quality:1.4), (lip, nose, tooth, rouge, lipstick, eyeshadow:1.4), ( jpeg artifacts:1.4), (depth of field, bokeh, blurry, film grain, chromatic aberration, lens flare:1.0), (1boy, abs, muscular, rib:1.0), greyscale, monochrome, dusty sunbeams, trembling, motion lines, motion blur, emphasis lines, text, title, logo, signature, child, childlike, young, easynegative, (bad-hands-5:0.8), plain background, monochrome, poorly drawn face, poorly drawn hands, watermark, censored, (mutated hands and fingers), ugly, worst quality, low quality,, nsfw,(worst quality, low quality:1.4), (lip, nose, tooth, rouge, lipstick, eyeshadow:1.4), ( jpeg artifacts:1.4), (depth of field, bokeh, blurry, film grain, chromatic aberration, lens flare:1.0), (1boy, abs, muscular, rib:1.0), greyscale, monochrome, dusty sunbeams, trembling, motion lines, motion blur, emphasis lines, text, title, logo, signature, child, childlike, young"
#negative_prompt = "" #negative_prompt = ""
negative_prompt = "badhandsv4, worst quality, lowres, EasyNegative, hermaphrodite, cropped, not in the frame, additional faces, jpeg large artifacts, jpeg small artifacts, ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, extra limbs, disfigured, deformed, body out of frame, blurry, bad anatomy, blurred, watermark, grainy, signature, cut off, draft, not finished drawing, unfinished image, bad eyes, doll, 3d, cartoon, (bad eyes:1.2), (worst quality:1.2), (low quality:1.2), bad-image-v2-39000, (bad_prompt_version2:0.8), nude, badhandv4 By bad artist -neg easynegative ng_deepnegative_v1_75t verybadimagenegative_v1.3, (Worst Quality, Low Quality:1.4), Poorly Made Bad 3D, Lousy Bad Realistic, nsfw,(worst quality, low quality:1.4), (lip, nose, tooth, rouge, lipstick, eyeshadow:1.4), ( jpeg artifacts:1.4), (depth of field, bokeh, blurry, film grain, chromatic aberration, lens flare:1.0), (1boy, abs, muscular, rib:1.0), greyscale, monochrome, dusty sunbeams, trembling, motion lines, motion blur, emphasis lines, text, title, logo, signature, child, childlike, young, easynegative, (bad-hands-5:0.8), plain background, monochrome, poorly drawn face, poorly drawn hands, watermark, censored, (mutated hands and fingers), ugly, worst quality, low quality,, nsfw,(worst quality, low quality:1.4), (lip, nose, tooth, rouge, lipstick, eyeshadow:1.4), ( jpeg artifacts:1.4), (depth of field, bokeh, blurry, film grain, chromatic aberration, lens flare:1.0), (1boy, abs, muscular, rib:1.0), greyscale, monochrome, dusty sunbeams, trembling, motion lines, motion blur, emphasis lines, text, title, logo, signature, child, childlike, young"
await ctx.send("Please be patient this may take some time! Generating: " + prompt + ".") await ctx.send("Please be patient this may take some time! Generating: " + prompt + ".")
try:
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)
response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload)
r = response.json() async with aiohttp.ClientSession() as session:
for i in r['images']: async with session.post(url=f'{url}/sdapi/v1/img2img', json=payload) as response:
data = await response.json()
for i in data['images']:
if not os.path.isdir("tmp/reimagined/"+ str(ctx.author.id)): if not os.path.isdir("tmp/reimagined/"+ str(ctx.author.id)):
os.makedirs("tmp/reimagined/"+ str(ctx.author.id)) os.makedirs("tmp/reimagined/"+ str(ctx.author.id))
image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0]))) image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0])))
png_payload = {"image": "data:image/png;base64," + i} png_payload = {"image": "data:image/png;base64," + i}
response2 = requests.post(url=f'{url}/sdapi/v1/png-info', json=png_payload) async with session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload) as response2:
pnginfo = PngImagePlugin.PngInfo() pnginfo = PngImagePlugin.PngInfo()
pnginfo.add_text("parameters", response2.json().get("info")) pnginfo.add_text("parameters", response2.json().get("info"))
my_filename = "tmp/" + str(len(os.listdir("tmp/"))) + ".png" my_filename = "tmp/" + str(len(os.listdir("tmp/"))) + ".png"
@ -1399,9 +1369,6 @@ async def reimagine(ctx):
with open(my_filename, "rb") as fh: with open(my_filename, "rb") as fh:
f = discord.File(fh, filename=my_filename) f = discord.File(fh, filename=my_filename)
await ctx.send(file=f) await ctx.send(file=f)
except Exception as error:
await handle_error(error)
await ctx.send("My image generation service may not be running.")
@bot.command() @bot.command()