reverted changes for now
This commit is contained in:
parent
bfb06da556
commit
5f698b3757
1 changed files with 135 additions and 147 deletions
|
|
@ -142,10 +142,11 @@ async def answer_question(topic, model="gpt-3.5-turbo"):
|
||||||
url = "https://api.openai.com/v1/chat/completions"
|
url = "https://api.openai.com/v1/chat/completions"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with bot.http_session.post(url, headers=headers, json=data) as resp:
|
async with aiohttp.ClientSession() as session:
|
||||||
response_data = await resp.json()
|
async with session.post(url, headers=headers, json=data) as resp:
|
||||||
response = response_data['choices'][0]['message']['content']
|
response_data = await resp.json()
|
||||||
return response
|
response = response_data['choices'][0]['message']['content']
|
||||||
|
return response
|
||||||
|
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
return await handle_error(error)
|
return await handle_error(error)
|
||||||
|
|
@ -188,33 +189,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")
|
||||||
|
|
||||||
async with bot.http_session.get(attachment.url) as response:
|
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:
|
while True:
|
||||||
chunk = await response.content.read(1024)
|
chunk = await response.content.read(1024)
|
||||||
if not chunk:
|
if not chunk:
|
||||||
break
|
break
|
||||||
out_file.write(chunk)
|
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}
|
||||||
async with bot.http_session.post(f'{url}/sdapi/v1/interrogate', json=payload) as response:
|
async with session.post(f'{url}/sdapi/v1/interrogate', json=payload) as response:
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
description = data.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 aiohttp.ClientError 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
|
||||||
|
|
||||||
|
|
@ -248,9 +250,10 @@ async def react_to_msg(ctx, react):
|
||||||
url = "https://api.openai.com/v1/chat/completions"
|
url = "https://api.openai.com/v1/chat/completions"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with bot.http_session.post(url, headers=headers, json=data) as resp:
|
async with aiohttp.ClientSession() as session:
|
||||||
response_data = await resp.json()
|
async with session.post(url, headers=headers, json=data) as resp:
|
||||||
reaction = response_data['choices'][0]['message']['content']
|
response_data = await resp.json()
|
||||||
|
reaction = response_data['choices'][0]['message']['content']
|
||||||
await ctx.add_reaction(reaction.strip())
|
await ctx.add_reaction(reaction.strip())
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
print("Some error happened while trying to react to a message")
|
print("Some error happened while trying to react to a message")
|
||||||
|
|
@ -288,18 +291,19 @@ async def chat_response(ctx, channel_vars, chat_history_string):
|
||||||
url = "https://api.openai.com/v1/chat/completions"
|
url = "https://api.openai.com/v1/chat/completions"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with bot.http_session.post(url, headers=headers, json=data) as resp:
|
async with aiohttp.ClientSession() as session:
|
||||||
response_data = await resp.json()
|
async with session.post(url, headers=headers, json=data) as resp:
|
||||||
response = response_data['choices'][0]['message']['content']
|
response_data = await resp.json()
|
||||||
if "Sparkytron 3000:" in response[0:17]:
|
response = response_data['choices'][0]['message']['content']
|
||||||
response = response.replace("Sparkytron 3000:", "")
|
if "Sparkytron 3000:" in response[0:17]:
|
||||||
max_len = 1999
|
response = response.replace("Sparkytron 3000:", "")
|
||||||
if len(response) > max_len:
|
max_len = 1999
|
||||||
messages=[response[y-max_len:y] for y in range(max_len, len(response)+max_len,max_len)]
|
if len(response) > max_len:
|
||||||
else:
|
messages=[response[y-max_len:y] for y in range(max_len, len(response)+max_len,max_len)]
|
||||||
messages=[response]
|
else:
|
||||||
for message in messages:
|
messages=[response]
|
||||||
await ctx.channel.send(message)
|
for message in messages:
|
||||||
|
await ctx.channel.send(message)
|
||||||
|
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
await handle_error(error)
|
await handle_error(error)
|
||||||
|
|
@ -347,20 +351,6 @@ async def task_loop():
|
||||||
else:
|
else:
|
||||||
await bot_stuff.send("All daily tasks successfully ran!")
|
await bot_stuff.send("All daily tasks successfully ran!")
|
||||||
|
|
||||||
async def create_session():
|
|
||||||
return aiohttp.ClientSession(timeout=30)
|
|
||||||
|
|
||||||
async def close_session(session):
|
|
||||||
await session.close()
|
|
||||||
|
|
||||||
@bot.event
|
|
||||||
async def on_connect():
|
|
||||||
bot.http_session = await create_session()
|
|
||||||
|
|
||||||
@bot.event
|
|
||||||
async def on_disconnect():
|
|
||||||
await close_session(bot.http_session)
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print('We have logged in as {0.user}'.format(bot))
|
print('We have logged in as {0.user}'.format(bot))
|
||||||
|
|
@ -624,9 +614,10 @@ async def meme(ctx):
|
||||||
|
|
||||||
|
|
||||||
async def generate_random_meme(topic):
|
async def generate_random_meme(topic):
|
||||||
async with bot.http_session.get('https://api.imgflip.com/get_memes') as resp:
|
async with aiohttp.ClientSession() as session:
|
||||||
response_data = await resp.json()
|
async with session.get('https://api.imgflip.com/get_memes') as resp:
|
||||||
response = response_data['data']['memes']
|
response_data = await resp.json()
|
||||||
|
response = response_data['data']['memes']
|
||||||
memepics = [{'name':image['name'],'url':image['url'],'id':image['id']} for image in response]
|
memepics = [{'name':image['name'],'url':image['url'],'id':image['id']} for image in response]
|
||||||
|
|
||||||
#Pick a meme format
|
#Pick a meme format
|
||||||
|
|
@ -660,8 +651,9 @@ async def meme(ctx):
|
||||||
URL = 'https://api.imgflip.com/caption_image'
|
URL = 'https://api.imgflip.com/caption_image'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with bot.http_session.post(URL, params=params) as resp:
|
async with aiohttp.ClientSession() as session:
|
||||||
response = await resp.json()
|
async with session.post(URL, params=params) as resp:
|
||||||
|
response = await resp.json()
|
||||||
print(f"Generated Meme = {response['success']}\nImage Link = {response['data']['url']}\nPage Link = {response['data']['page_url']}")
|
print(f"Generated Meme = {response['success']}\nImage Link = {response['data']['url']}\nPage Link = {response['data']['page_url']}")
|
||||||
image_link = response['data']['url']
|
image_link = response['data']['url']
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
|
|
@ -702,24 +694,9 @@ async def meme(ctx):
|
||||||
brief="Runescape gold to usd"
|
brief="Runescape gold to usd"
|
||||||
)
|
)
|
||||||
async def rsgp(ctx, amount):
|
async def rsgp(ctx, amount):
|
||||||
output = ""
|
cost_per_bil = 27 #1b rsgp to usd
|
||||||
cost_per_bil = 25.50 #1b rsgp to usd
|
cost = (int(amount) * cost_per_bil / 1000000000)
|
||||||
cost_per_bil_os = 210
|
output = str(amount) + ' rsgp would cost: $' + str(cost)
|
||||||
gold_per_bond = 70000000
|
|
||||||
gold_per_bond_os = 7000000
|
|
||||||
cost_per_bond = 8 #dollars usd
|
|
||||||
bondcost = (int(amount)/gold_per_bond) * cost_per_bond
|
|
||||||
rwtcost = (int(amount) * cost_per_bil / 1000000000)
|
|
||||||
dollar_gp = (int(amount)*1000000000)/cost_per_bil
|
|
||||||
osbondcost = (int(amount)/gold_per_bond_os) * cost_per_bond
|
|
||||||
osrwtcost = (int(amount) * cost_per_bil_os / 1000000000)
|
|
||||||
osdollar_gp = (int(amount)*1000000000)/cost_per_bil_os
|
|
||||||
output += str(amount) + ' rs3 gp would cost: $' + str(rwtcost) + " (RWT)\n"
|
|
||||||
output += str(amount) + ' rs3 gp would cost: $' + str(bondcost) + " (Bonds)\n"
|
|
||||||
output += str(amount) + ' dollars spent on rs3 gp would be: ' + str(dollar_gp) + " (RS3 GP)\n"
|
|
||||||
output += str(amount) + ' osrs gp would cost: $' + str(rwtcost) + " (RWT)\n"
|
|
||||||
output += str(amount) + ' osrs gp would cost: $' + str(bondcost) + " (Bonds)\n"
|
|
||||||
output += str(amount) + ' dollars spent on osrs gp would be: ' + str(dollar_gp) + " (OSRS GP)\n"
|
|
||||||
await ctx.send(output)
|
await ctx.send(output)
|
||||||
|
|
||||||
@bot.command(
|
@bot.command(
|
||||||
|
|
@ -991,20 +968,21 @@ async def website(ctx):
|
||||||
if url == "disabled":
|
if url == "disabled":
|
||||||
return
|
return
|
||||||
file_list = []
|
file_list = []
|
||||||
for image in image_list:
|
async with aiohttp.ClientSession() as session:
|
||||||
filename = image.replace(" ", "").lower() + ".png"
|
for image in image_list:
|
||||||
payload = {"prompt": image, "steps": 25}
|
filename = image.replace(" ", "").lower() + ".png"
|
||||||
response = await session.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
|
payload = {"prompt": image, "steps": 25}
|
||||||
r = await response.json()
|
response = await session.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
|
||||||
for i in r['images']:
|
r = await response.json()
|
||||||
image = Image.open(io.BytesIO(base64.b64decode(i.split(",", 1)[0])))
|
for i in r['images']:
|
||||||
png_payload = {"image": "data:image/png;base64," + i}
|
image = Image.open(io.BytesIO(base64.b64decode(i.split(",", 1)[0])))
|
||||||
response2 = await session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload)
|
png_payload = {"image": "data:image/png;base64," + i}
|
||||||
pnginfo = PngImagePlugin.PngInfo()
|
response2 = await session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload)
|
||||||
json_response = await response2.json()
|
pnginfo = PngImagePlugin.PngInfo()
|
||||||
pnginfo.add_text("parameters", json_response.get("info"))
|
json_response = await response2.json()
|
||||||
image.save(local_folder + filename, pnginfo=pnginfo)
|
pnginfo.add_text("parameters", json_response.get("info"))
|
||||||
file_list.append(filename)
|
image.save(local_folder + filename, pnginfo=pnginfo)
|
||||||
|
file_list.append(filename)
|
||||||
return file_list
|
return file_list
|
||||||
|
|
||||||
async def add_image_filenames(code, file_list):
|
async def add_image_filenames(code, file_list):
|
||||||
|
|
@ -1116,13 +1094,15 @@ async def draw(ctx):
|
||||||
negative_prompt = ""
|
negative_prompt = ""
|
||||||
payload = {"prompt": prompt,"steps": 25, "negative_prompt": negative_prompt,"batch_size": amount}
|
payload = {"prompt": prompt,"steps": 25, "negative_prompt": negative_prompt,"batch_size": amount}
|
||||||
try:
|
try:
|
||||||
async with bot.http_session.post(url=f'{url}/sdapi/v1/txt2img', json=payload) as resp:
|
async with aiohttp.ClientSession() as session:
|
||||||
r = await resp.json()
|
async with session.post(url=f'{url}/sdapi/v1/txt2img', json=payload) as resp:
|
||||||
|
r = await resp.json()
|
||||||
for i in r['images']:
|
for i in r['images']:
|
||||||
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}
|
||||||
async with bot.http_session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload) as resp2:
|
async with aiohttp.ClientSession() as session:
|
||||||
response2 = await resp2.json()
|
async with session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload) as resp2:
|
||||||
|
response2 = await resp2.json()
|
||||||
pnginfo = PngImagePlugin.PngInfo()
|
pnginfo = PngImagePlugin.PngInfo()
|
||||||
pnginfo.add_text("parameters", response2.get("info"))
|
pnginfo.add_text("parameters", response2.get("info"))
|
||||||
my_filename = "tmp/" + str(len(os.listdir("tmp/"))) + ".png"
|
my_filename = "tmp/" + str(len(os.listdir("tmp/"))) + ".png"
|
||||||
|
|
@ -1294,8 +1274,9 @@ async def change_model(ctx, model_choice='0'):
|
||||||
await ctx.send("This command is currently disabled")
|
await ctx.send("This command is currently disabled")
|
||||||
return
|
return
|
||||||
|
|
||||||
async with bot.http_session.get(url=f'{url}/sdapi/v1/options') as response:
|
async with aiohttp.ClientSession() as session:
|
||||||
config_json = await response.json()
|
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'
|
||||||
|
|
@ -1304,10 +1285,11 @@ async def change_model(ctx, model_choice='0'):
|
||||||
model_id, model_name = model_choices[model_choice]
|
model_id, model_name = model_choices[model_choice]
|
||||||
if current_model != model_id:
|
if current_model != model_id:
|
||||||
payload = {"sd_model_checkpoint": model_id}
|
payload = {"sd_model_checkpoint": model_id}
|
||||||
async with bot.http_session.post(url=f'{url}/sdapi/v1/options', json=payload) as response:
|
async with aiohttp.ClientSession() as session:
|
||||||
output = "Changed model to: " + model_name
|
async with session.post(url=f'{url}/sdapi/v1/options', json=payload) as response:
|
||||||
await ctx.send(output)
|
output = "Changed model to: " + model_name
|
||||||
return
|
await ctx.send(output)
|
||||||
|
return
|
||||||
else:
|
else:
|
||||||
await ctx.send(f"Already set to use {model_name}")
|
await ctx.send(f"Already set to use {model_name}")
|
||||||
return
|
return
|
||||||
|
|
@ -1346,8 +1328,9 @@ async def imagine(ctx):
|
||||||
payload = combine_dicts(payload, key_value_pairs)
|
payload = combine_dicts(payload, key_value_pairs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with bot.http_session.post(url, headers=headers, json=payload) as resp:
|
async with aiohttp.ClientSession() as session:
|
||||||
r = await resp.json()
|
async with session.post(url, headers=headers, json=payload) as resp:
|
||||||
|
r = await resp.json()
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
await ctx.send("My image generation service may not be running.")
|
await ctx.send("My image generation service may not be running.")
|
||||||
await handle_error(error)
|
await handle_error(error)
|
||||||
|
|
@ -1360,8 +1343,9 @@ async def imagine(ctx):
|
||||||
png_payload = {"image": "data:image/png;base64," + i}
|
png_payload = {"image": "data:image/png;base64," + i}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with bot.http_session.post(url, json=png_payload) as resp:
|
async with aiohttp.ClientSession() as session:
|
||||||
response2 = await resp.json()
|
async with session.post(url, json=png_payload) as resp:
|
||||||
|
response2 = await resp.json()
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
await ctx.send("My image generation service may not be running.")
|
await ctx.send("My image generation service may not be running.")
|
||||||
await handle_error(error)
|
await handle_error(error)
|
||||||
|
|
@ -1407,21 +1391,23 @@ async def describe(ctx):
|
||||||
print("Couldn't find image.")
|
print("Couldn't find image.")
|
||||||
return
|
return
|
||||||
|
|
||||||
async with bot.http_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)
|
||||||
|
|
||||||
img_link = my_open_img_file(imageName)
|
img_link = my_open_img_file(imageName)
|
||||||
try:
|
try:
|
||||||
payload = {"image": img_link}
|
payload = {"image": img_link}
|
||||||
async with bot.http_session.post(url, json=payload) as response:
|
async with aiohttp.ClientSession() as session:
|
||||||
r = await response.json()
|
async with session.post(url, json=payload) as response:
|
||||||
|
r = await response.json()
|
||||||
print(r)
|
print(r)
|
||||||
await ctx.send(r.get("caption"))
|
await ctx.send(r.get("caption"))
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
|
|
@ -1456,15 +1442,16 @@ async def reimagine(ctx):
|
||||||
key_value_pairs, prompt = extract_key_value_pairs(prompt)
|
key_value_pairs, prompt = extract_key_value_pairs(prompt)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with bot.http_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:
|
except Exception as error:
|
||||||
await ctx.send("My image generation service may not be running.")
|
await ctx.send("My image generation service may not be running.")
|
||||||
|
|
@ -1481,22 +1468,23 @@ async def reimagine(ctx):
|
||||||
payload = combine_dicts(payload, key_value_pairs)
|
payload = combine_dicts(payload, key_value_pairs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with bot.http_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 bot.http_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:
|
except Exception as error:
|
||||||
await ctx.send("My image generation service may not be running.")
|
await ctx.send("My image generation service may not be running.")
|
||||||
await handle_error(error)
|
await handle_error(error)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue