Single session aiohttp all the stuff for real this time
This commit is contained in:
parent
5aa35c48ee
commit
d259ee4228
1 changed files with 114 additions and 130 deletions
|
|
@ -142,8 +142,7 @@ async def answer_question(topic, model="gpt-3.5-turbo"):
|
|||
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:
|
||||
async with bot.http_session.post(url, headers=headers, json=data) as resp:
|
||||
response_data = await resp.json()
|
||||
response = response_data['choices'][0]['message']['content']
|
||||
return response
|
||||
|
|
@ -189,12 +188,11 @@ async def look_at(ctx, look=False):
|
|||
url = os.getenv('stablediffusion_url')
|
||||
if url == "disabled":
|
||||
return
|
||||
async with aiohttp.ClientSession() as session:
|
||||
for attachment in ctx.attachments:
|
||||
if attachment.url.endswith(('.jpg', '.png')):
|
||||
print("image seen")
|
||||
|
||||
async with session.get(attachment.url) as response:
|
||||
async with bot.http_session.get(attachment.url) as response:
|
||||
imageName = "tmp/" + str(len(os.listdir('tmp/'))) + '.png'
|
||||
|
||||
with open(imageName, 'wb') as out_file:
|
||||
|
|
@ -209,7 +207,7 @@ async def look_at(ctx, look=False):
|
|||
|
||||
try:
|
||||
payload = {"image": img_link}
|
||||
async with session.post(f'{url}/sdapi/v1/interrogate', json=payload) as response:
|
||||
async with bot.http_session.post(f'{url}/sdapi/v1/interrogate', json=payload) as response:
|
||||
data = await response.json()
|
||||
description = data.get("caption")
|
||||
description = description.split(',')[0]
|
||||
|
|
@ -250,8 +248,7 @@ async def react_to_msg(ctx, react):
|
|||
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:
|
||||
async with bot.http_session.post(url, headers=headers, json=data) as resp:
|
||||
response_data = await resp.json()
|
||||
reaction = response_data['choices'][0]['message']['content']
|
||||
await ctx.add_reaction(reaction.strip())
|
||||
|
|
@ -291,8 +288,7 @@ async def chat_response(ctx, channel_vars, chat_history_string):
|
|||
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:
|
||||
async with bot.http_session.post(url, headers=headers, json=data) as resp:
|
||||
response_data = await resp.json()
|
||||
response = response_data['choices'][0]['message']['content']
|
||||
if "Sparkytron 3000:" in response[0:17]:
|
||||
|
|
@ -352,7 +348,8 @@ async def task_loop():
|
|||
await bot_stuff.send("All daily tasks successfully ran!")
|
||||
|
||||
async def create_session():
|
||||
return aiohttp.ClientSession()
|
||||
timeout = aiohttp.ClientTimeout(total=30)
|
||||
return aiohttp.ClientSession(timeout=timeout)
|
||||
|
||||
async def close_session(http_session):
|
||||
await http_session.close()
|
||||
|
|
@ -628,8 +625,7 @@ async def meme(ctx):
|
|||
|
||||
|
||||
async def generate_random_meme(topic):
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get('https://api.imgflip.com/get_memes') as resp:
|
||||
async with bot.http_session.get('https://api.imgflip.com/get_memes') as resp:
|
||||
response_data = await resp.json()
|
||||
response = response_data['data']['memes']
|
||||
memepics = [{'name':image['name'],'url':image['url'],'id':image['id']} for image in response]
|
||||
|
|
@ -665,8 +661,7 @@ async def meme(ctx):
|
|||
URL = 'https://api.imgflip.com/caption_image'
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(URL, params=params) as resp:
|
||||
async with bot.http_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']}")
|
||||
image_link = response['data']['url']
|
||||
|
|
@ -983,16 +978,15 @@ async def website(ctx):
|
|||
if url == "disabled":
|
||||
return
|
||||
file_list = []
|
||||
async with aiohttp.ClientSession() as session:
|
||||
for image in image_list:
|
||||
filename = image.replace(" ", "").lower() + ".png"
|
||||
payload = {"prompt": image, "steps": 25}
|
||||
response = await session.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
|
||||
response = await bot.http_session.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
|
||||
r = await response.json()
|
||||
for i in r['images']:
|
||||
image = Image.open(io.BytesIO(base64.b64decode(i.split(",", 1)[0])))
|
||||
png_payload = {"image": "data:image/png;base64," + i}
|
||||
response2 = await session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload)
|
||||
response2 = await bot.http_session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload)
|
||||
pnginfo = PngImagePlugin.PngInfo()
|
||||
json_response = await response2.json()
|
||||
pnginfo.add_text("parameters", json_response.get("info"))
|
||||
|
|
@ -1085,7 +1079,7 @@ async def feature(ctx):
|
|||
help="Generates a picture using stable diffusion and gpt 3.5. It generates a list of 10 random artistic words and feeds them into stable diffusion. Usage: !draw (amount of pictures)",
|
||||
brief="Generate a random image"
|
||||
)
|
||||
async def draw(ctx, http_session = bot.http_session):
|
||||
async def draw(ctx):
|
||||
url = os.getenv('stablediffusion_url')
|
||||
if url == "disabled":
|
||||
return
|
||||
|
|
@ -1115,14 +1109,12 @@ async def draw(ctx, http_session = bot.http_session):
|
|||
negative_prompt = "easynegative verybadimagenegative_v1.3"
|
||||
payload = {"prompt": prompt,"steps": 25, "negative_prompt": negative_prompt,"batch_size": amount}
|
||||
try:
|
||||
#async with aiohttp.ClientSession() as session:
|
||||
async with http_session.post(url=f'{url}/sdapi/v1/txt2img', json=payload) as resp:
|
||||
async with bot.http_session.post(url=f'{url}/sdapi/v1/txt2img', json=payload) as resp:
|
||||
r = await resp.json()
|
||||
for i in r['images']:
|
||||
image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0])))
|
||||
png_payload = {"image": "data:image/png;base64," + i}
|
||||
#async with aiohttp.ClientSession() as session:
|
||||
async with http_session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload) as resp2:
|
||||
async with bot.http_session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload) as resp2:
|
||||
response2 = await resp2.json()
|
||||
pnginfo = PngImagePlugin.PngInfo()
|
||||
pnginfo.add_text("parameters", response2.get("info"))
|
||||
|
|
@ -1296,8 +1288,7 @@ async def change_model(ctx, model_choice='0'):
|
|||
await ctx.send("This command is currently disabled")
|
||||
return
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url=f'{url}/sdapi/v1/options') as response:
|
||||
async with bot.http_session.get(url=f'{url}/sdapi/v1/options') as response:
|
||||
config_json = await response.json()
|
||||
|
||||
current_model = config_json["sd_model_checkpoint"]
|
||||
|
|
@ -1307,8 +1298,7 @@ async def change_model(ctx, model_choice='0'):
|
|||
model_id, model_name = model_choices[model_choice]
|
||||
if current_model != model_id:
|
||||
payload = {"sd_model_checkpoint": model_id}
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(url=f'{url}/sdapi/v1/options', json=payload) as response:
|
||||
async with bot.http_session.post(url=f'{url}/sdapi/v1/options', json=payload) as response:
|
||||
output = "Changed model to: " + model_name
|
||||
await ctx.send(output)
|
||||
return
|
||||
|
|
@ -1350,8 +1340,7 @@ async def imagine(ctx):
|
|||
payload = combine_dicts(payload, key_value_pairs)
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(url, headers=headers, json=payload) as resp:
|
||||
async with bot.http_session.post(url, headers=headers, json=payload) as resp:
|
||||
r = await resp.json()
|
||||
except Exception as error:
|
||||
await ctx.send("My image generation service may not be running.")
|
||||
|
|
@ -1365,8 +1354,7 @@ async def imagine(ctx):
|
|||
png_payload = {"image": "data:image/png;base64," + i}
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(url, json=png_payload) as resp:
|
||||
async with bot.http_session.post(url, json=png_payload) as resp:
|
||||
response2 = await resp.json()
|
||||
except Exception as error:
|
||||
await ctx.send("My image generation service may not be running.")
|
||||
|
|
@ -1413,8 +1401,7 @@ async def describe(ctx):
|
|||
print("Couldn't find image.")
|
||||
return
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(file_url) as response:
|
||||
async with bot.http_session.get(file_url) as response:
|
||||
imageName = "tmp/" + str(len(os.listdir("tmp/"))) + ".png"
|
||||
with open(imageName, 'wb') as out_file:
|
||||
print(f"Saving image: {imageName}")
|
||||
|
|
@ -1427,8 +1414,7 @@ async def describe(ctx):
|
|||
img_link = my_open_img_file(imageName)
|
||||
try:
|
||||
payload = {"image": img_link}
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(url, json=payload) as response:
|
||||
async with bot.http_session.post(url, json=payload) as response:
|
||||
r = await response.json()
|
||||
print(r)
|
||||
await ctx.send(r.get("caption"))
|
||||
|
|
@ -1464,8 +1450,7 @@ async def reimagine(ctx):
|
|||
key_value_pairs, prompt = extract_key_value_pairs(prompt)
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(file_url) as response:
|
||||
async with bot.http_session.get(file_url) as response:
|
||||
imageName = "tmp/" + str(len(os.listdir("tmp/"))) + ".png"
|
||||
with open(imageName, 'wb') as out_file:
|
||||
print(f"Saving image: {imageName}")
|
||||
|
|
@ -1490,15 +1475,14 @@ async def reimagine(ctx):
|
|||
payload = combine_dicts(payload, key_value_pairs)
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(url=f'{url}/sdapi/v1/img2img', json=payload) as response:
|
||||
async with bot.http_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)):
|
||||
os.makedirs("tmp/reimagined/"+ str(ctx.author.id))
|
||||
image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0])))
|
||||
png_payload = {"image": "data:image/png;base64," + i}
|
||||
async with session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload) as resp2:
|
||||
async with bot.http_session.post(url=f'{url}/sdapi/v1/png-info', json=png_payload) as resp2:
|
||||
response2 = await resp2.json()
|
||||
pnginfo = PngImagePlugin.PngInfo()
|
||||
pnginfo.add_text("parameters", response2.get("info"))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue