initial commit

This commit is contained in:
Paillat
2023-01-12 07:41:01 +01:00
parent 106c09f435
commit 83e41c9546
2 changed files with 111 additions and 0 deletions

109
main.py Normal file
View File

@@ -0,0 +1,109 @@
import openai
import discord
from discord import Intents
from discord.commands import slash_command, option
import re
import os
import asyncio
import logging
logging.basicConfig(level=logging.INFO)
#we open the
intstructions = '''Instruction: Here is a presentation with marp. It's not possible to make slides longer than 200 characters. to separate slides,
"
---
"
then go at the line. To add an image illustration , use ![bg left:50% 100%](a-description-of-the-image.png) at the beginning of the slide, just after "---"-It's not possible to add technical images but only illustrations. The images are generated by an ai, the name of the file should be a detailed description of the image wanted. For example " ![bg left:50% 100%](a-man-wearing-a hat-ryding-a-bicicle.png)" but son't need to show a person necessairly. The presentatio should be for everybody, all technical words and concepts, explained. The presentation is minimum 20 slides long. You can use bulletpoints. Use markdown formatting (titles, etc...)'''
bot = discord.Bot()
styles = ["default", "gaia", "uncover", "default-dark", "gaia-dark", "uncover-dark"]
darkstyles = ["default-dark", "gaia-dark", "uncover-dark"]
async def get_style(ctx: discord.AutocompleteContext):
"""Returns a list of colors that begin with the characters entered so far."""
return [color for color in styles if color.startswith(ctx.value.lower())]
@bot.slash_command(name="present", description="Generate a presentation with marp")
#we create a function that takes the subject of the presentation and the style of the presentation as arguments, and that
@option(name="subject", description="The subject of the presentation", required=True)
@option(name="style", description="The style of the presentation", required=False, autocomplete=get_style)
async def present(ctx: discord.ApplicationContext, subject: str, style: str = "default"):
await ctx.defer()
prompt = intstructions + "The subject of the presentation is: " + subject + "<|endofprompt|>"
#replace the spaces in the szbject with dashes
subject2 = subject
subject = subject.replace(" ", "-")
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
temperature=0.7,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["<|endofprompt|>"]
)
#we save the output in a variable
output = response["choices"][0]["text"]
#if the output dosent start with --- or with \n--- or with \n\n--- we add it at the beginning of the output
#we add the marp header
marp = f'''---
marp: true
theme: {styles}
class:
- lead
'''
if style in darkstyles:
marp = marp + f" - invert\n"
# if not output.startswith("---") and not output.startswith("\n---") and not output.startswith("\n\n---"):
# output = "---\n" + output
present = marp + output
##we save the output in a file called "subject.md"
matches = re.finditer(r'!\[.*?\]\((.*?)\)', present)
image_filenames = []
for match in matches:
image_filenames.append(match.group(1))
print(image_filenames)
#we create a text file with the image names and a md file for the presentation with utf8 encoding
with open(subject + ".md", "w", encoding="utf8") as f:
f.write(present)
with open(subject + "-images.txt", "w") as f:
for image in image_filenames:
f.write(image + "\n")
#we execute the command to convert the markdown file to a pdf and html file and also generate the first slide image
cmd = f"marp --pdf --allow-local-files {subject}.md"
os.system(cmd)
print(cmd)
cmd = f"marp --image png -o {subject}.png --allow-local-files {subject}.md"
os.system(cmd)
print(cmd)
cmd = f"marp --html --allow-local-files {subject}.md"
os.system(cmd)
print(cmd)
#we create an embed with the first slide imageand send it with the pdf file and the markdown file
embed = discord.Embed(title=subject2, description="Thanks for using presentator bot. You can download the presentation in different formats (pdf, markdown, html). The images are generated by an ai. If you want to modify your presentation you can use the markdown file. More information about how to modify the file [HERE](https://marp.app).", color=0x00ff00)
files = [discord.File(f"{subject}.pdf"), discord.File(f"{subject}.md"), discord.File(f"{subject}.html"), discord.File(f"{subject}.png")]
embed.set_image(url=f"attachment://{subject}.png")
#now we send the embed and all the 4 files (pdf, markdown, html, png) at the same time
await ctx.respond(embed=embed, files=files)
#we delete the markdown file and the text file with the image names
#wait for 10 seconds
await asyncio.sleep(20)
os.remove(f"{subject}.md")
os.remove(f"{subject}-images.txt")
os.remove(f"{subject}.png")
os.remove(f"{subject}.pdf")
os.remove(f"{subject}.html")
#when the bot is ready we print a message
@bot.event
async def on_ready():
print("Bot is ready")
#get the openai key drom he key.env file
with open("key.env", "r") as f:
apikey = f.read()
openai.api_key = apikey
with open("token.env", "r") as f:
token = f.read()
bot.run(token)