From 1fe7108256e61b07fa0cdb356255246fd1c96d49 Mon Sep 17 00:00:00 2001
From: phixxy
Date: Sat, 20 Jan 2024 01:14:56 -0800
Subject: [PATCH] removed unused task loop
---
extensions/async_openai.py | 198 +------------------------------------
1 file changed, 1 insertion(+), 197 deletions(-)
diff --git a/extensions/async_openai.py b/extensions/async_openai.py
index be88378..50ab570 100644
--- a/extensions/async_openai.py
+++ b/extensions/async_openai.py
@@ -27,12 +27,6 @@ class AsyncOpenAI(commands.Cog):
except:
print("AsyncOpenAI failed to make directories")
- '''async def upload_sftp(local_filename, server_folder, server_filename):
- remotepath = server_folder + server_filename
- async with asyncssh.connect(os.getenv('ftp_server'), username=os.getenv('ftp_username'), password=os.getenv('ftp_password')) as conn:
- async with conn.start_sftp_client() as sftp:
- await sftp.put(local_filename, remotepath=remotepath)'''
-
async def handle_error(self, error):
print(error)
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
@@ -81,70 +75,6 @@ class AsyncOpenAI(commands.Cog):
f.writelines(message+'\n')
await ctx.send("Saved suggestion!")
- @commands.command()
- async def generate_blog(self, ctx):
- start_time = time.time()
- topic = ''
- filename = "phixxy.com/ai-blog/index.html"
- with open(filename, 'r', encoding="utf-8") as f:
- html_data = f.read()
- current_time = time.time()
- current_struct_time = time.localtime(current_time)
- date = time.strftime("%B %d, %Y", current_struct_time)
- if date in html_data:
- print("I already wrote a blog post today!")
- return
- blogpost_file = "databases/blog_topics.txt"
- #blog_subscribers = ["276197608735637505","242018983241318410"]
- if os.path.isfile(blogpost_file):
- with open(blogpost_file, 'r') as f:
- blogpost_topics = f.read()
- f.seek(0)
- topic = f.readline()
- blogpost_topics = blogpost_topics.replace(topic, '')
- with open(blogpost_file, 'w') as f:
- f.write(blogpost_topics)
- if topic != '':
- print("Writing blogpost")
- else:
- print("No topic given for blogpost, generating one.")
- topic = await self.answer_question("Give me one topic for an absurd blogpost.")
-
-
- post_div = '''
- '''
- title_prompt = 'generate an absurd essay title about ' + topic
- title = await self.answer_question(title_prompt, model="gpt-3.5-turbo")
- prompt = 'Write a satirical essay with a serious tone titled: "' + title + '". Do not label parts of the essay.'
- content = await self.answer_question(prompt, model="gpt-4")
- if title in content[:len(title)]:
- content = content.replace(title, '', 1)
- content = f"{content}
"
- content = content.replace('\n\n', "
")
- content = content.replace("
", '')
-
- post_div = post_div.replace("", title)
- post_div = post_div.replace("", date)
- post_div = post_div.replace("", content)
-
- html_data = html_data.replace("", post_div)
- with open(filename, 'w', encoding="utf-8") as f:
- f.write(html_data)
- await self.upload_sftp(filename, (os.getenv('ftp_public_html') + 'ai-blog/'), "index.html")
- run_time = time.time() - start_time
- print("It took " + str(run_time) + " seconds to generate the blog post!")
- output = "Blog Updated! (" + str(run_time) + " seconds) https://ai.phixxy.com/ai-blog"
- #output += '\nNotifying subscribers: '
- #for subscriber in blog_subscribers:
- # output += '<@' + subscriber + '> '
- print(output)
-
@commands.command(
description="Question",
help="Ask a raw chatgpt question. Usage: !question (question)",
@@ -207,131 +137,5 @@ class AsyncOpenAI(commands.Cog):
for chunk in chunks:
await ctx.send(chunk)
- @commands.command(
- description="Website",
- help="Generates a website using gpt 3.5. Usage: !website (topic)",
- brief="Generate a website"
- )
- async def website(self, ctx):
- async def delete_local_pngs(local_folder):
- for filename in os.listdir(local_folder):
- if ".png" in filename:
- os.remove(local_folder + filename)
-
- async def delete_ftp_pngs(server_folder):
- async with asyncssh.connect(os.getenv('ftp_server'), username=os.getenv('ftp_username'), password=os.getenv('ftp_password')) as conn:
- async with conn.start_sftp_client() as sftp:
- for filename in (await sftp.listdir(server_folder)):
- if '.png' in filename:
- try:
- print("Deleting", filename)
- await sftp.remove(server_folder+filename)
- except:
- print("Couldn't delete", filename)
-
- async def extract_image_tags(code):
- count = code.count("
") + index1 + 1
- img_tag = code[index1:index2]
- tags.append(img_tag)
- code = code[index2:]
- return tags
-
- async def extract_image_alt_text(tags):
- alt_texts = []
- for tag in tags:
- index1 = tag.find("alt") + 5
- index2 = tag[index1:].find("\"") + index1
- alt_text = tag[index1:index2]
- alt_texts.append(alt_text)
- return alt_texts
-
- async def generate_images(local_folder, image_list):
- url = os.getenv('stablediffusion_url')
- if url == "disabled":
- return
- file_list = []
- for image in image_list:
- filename = image.replace(" ", "").lower() + ".png"
- payload = {"prompt": image, "steps": 25}
- http_session = aiohttp.ClientSession()
- response = await 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 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"))
- image.save(local_folder + filename, pnginfo=pnginfo)
- file_list.append(filename)
- await http_session.close()
- return file_list
-
- async def add_image_filenames(code, file_list):
- for filename in file_list:
- code = code.replace("src=\"\"", "src=\""+ filename + "\"", 1)
- return code
-
- async def upload_html_and_imgs(local_folder, server_folder):
-
- for filename in os.listdir(local_folder):
- if ".png" in filename:
- await self.upload_sftp(local_folder + filename, (os.getenv('ftp_public_html') + 'ai-webpage/'), filename)
- #explicitly upload html files last!
- for filename in os.listdir(local_folder):
- if ".html" in filename:
- await self.upload_sftp(local_folder + filename, (os.getenv('ftp_public_html') + 'ai-webpage/'), filename)
-
-
- server_folder = os.getenv('ftp_public_html') + 'ai-webpage/'
- local_folder = "tmp/webpage/"
- working_file = local_folder + "index.html"
- if not os.path.exists(local_folder):
- os.mkdir(local_folder)
-
- try:
- await ctx.send("Please wait, this will take a long time! You will be able to view the website here: https://ai.phixxy.com/ai-webpage/")
- with open(working_file, "w") as f:
- f.write("Generating WebsiteThis webpage is currently being generated. The page will refresh once it is complete. Please be patient.
")
- await self.upload_sftp(working_file, server_folder, "index.html")
- topic = ctx.message.content.split(" ", maxsplit=1)[1]
- prompt = "Generate a webpage using html and inline css. The webpage topic should be " + topic + ". Feel free to add image tags with alt text. Leave the image source blank. The images will be added later."
- code = await self.answer_question(prompt)
-
-
- await delete_local_pngs(local_folder)
- await delete_ftp_pngs(server_folder)
-
- tags = await extract_image_tags(code)
- alt_texts = await extract_image_alt_text(tags)
- file_list = await generate_images(local_folder, alt_texts)
- code = await add_image_filenames(code, file_list)
-
- with open(working_file, 'w') as f:
- f.write(code)
- f.close()
-
- await upload_html_and_imgs(local_folder, server_folder)
-
- await ctx.send("Finished https://ai.phixxy.com/ai-webpage/")
- except Exception as error:
- await self.handle_error(error)
- await ctx.send("Failed, Try again.")
-
- @tasks.loop(seconds=1)
- async def ai_task_loop(self):
- current_time = time.localtime()
- if current_time.tm_hour == 17 and current_time.tm_min == 0 and current_time.tm_sec == 0:
- try:
- await self.generate_blog()
- except Exception as error:
- await self.handle_error(error)
-
async def setup(bot):
- bot.add_cog(AsyncOpenAI)
- bot.ai_task_loop.start() #I don't know if this will work or not
\ No newline at end of file
+ bot.add_cog(AsyncOpenAI)
\ No newline at end of file