mirror of
https://github.com/Paillat-dev/presentator.git
synced 2026-01-02 09:16:20 +00:00
Fixed sme things and added dalle option
This commit is contained in:
12
README.md
12
README.md
@@ -12,13 +12,13 @@
|
|||||||
- We send the pdf and html files to the user
|
- We send the pdf and html files to the user
|
||||||
|
|
||||||
# How to install
|
# How to install
|
||||||
**IMPORTANT** Linux installation isn't documented yet, if anyone with a nvidia gpu wants to complete the steps for linux, feel free to pull request.
|
**IMPORTANT** Linux and MacOS installation isn't documented yet, if anyone wanths to complete it, feel free to do a pull request.
|
||||||
## Requirements
|
## Requirements
|
||||||
- Python 3.8
|
- Python 3.8
|
||||||
- Pip
|
- Pip
|
||||||
- A Discord bot token
|
- A Discord bot token
|
||||||
- An openai api key
|
- An openai api key
|
||||||
- (Optional) An Nvidia GPU (for image generation)
|
- (Optional) An Nvidia GPU (for local image generation)
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
- Clone the repository
|
- Clone the repository
|
||||||
@@ -42,7 +42,8 @@ scoop install marp
|
|||||||
use_images = False
|
use_images = False
|
||||||
```
|
```
|
||||||
|
|
||||||
### Image generation (optional)
|
## Image generation (optional)
|
||||||
|
### With Stable Diffusion UI (powerful gpu option)
|
||||||
- Install [Stable Diffusion UI](https://github.com/cmdr2/stable-diffusion-ui) and switch to the `beta` branch.
|
- Install [Stable Diffusion UI](https://github.com/cmdr2/stable-diffusion-ui) and switch to the `beta` branch.
|
||||||
- Copy the `./image_gen_api/main.py` file to the `stable-diffusion-ui` folder
|
- Copy the `./image_gen_api/main.py` file to the `stable-diffusion-ui` folder
|
||||||
- Open the file called `Dev Console.cmd` in the `stable-diffusion-ui` folder and run the following commands:
|
- Open the file called `Dev Console.cmd` in the `stable-diffusion-ui` folder and run the following commands:
|
||||||
@@ -50,6 +51,9 @@ use_images = False
|
|||||||
pip install uvicorn
|
pip install uvicorn
|
||||||
pip install fastapi
|
pip install fastapi
|
||||||
```
|
```
|
||||||
|
- In the file `main.py`, at the first line, enable or disable the `sd` image generation.
|
||||||
|
### With DALL·E 2 (costs dalle credits)
|
||||||
|
- In the file `main.py`, at the first line, enable or disable the dalle image generation.
|
||||||
|
|
||||||
# Running
|
# Running
|
||||||
- Run the `main.py` file with :
|
- Run the `main.py` file with :
|
||||||
@@ -57,7 +61,7 @@ pip install fastapi
|
|||||||
python main.py
|
python main.py
|
||||||
```
|
```
|
||||||
|
|
||||||
### Image generation (optional)
|
### Local image generation (optional, only if you use the local image generation option)
|
||||||
- Open the file called `Dev Console.cmd` in the `stable-diffusion-ui` folder and run the following commands:
|
- Open the file called `Dev Console.cmd` in the `stable-diffusion-ui` folder and run the following commands:
|
||||||
```
|
```
|
||||||
uvicorn main:app --reload
|
uvicorn main:app --reload
|
||||||
|
|||||||
@@ -1,7 +1,16 @@
|
|||||||
import requests
|
import requests
|
||||||
import os
|
import os
|
||||||
async def generate(prompt,path):
|
import openai
|
||||||
r = requests.get(f"http://localhost:8000/generate_image?prompt={prompt}&path={path}")
|
async def generate(prompt, path, mode, openai_key):
|
||||||
return r.json()
|
#r = requests.get(f"http://localhost:8000/generate_image?prompt={prompt}&path={path}")
|
||||||
#print the current working directory
|
if mode == "sd":
|
||||||
print(os.getcwd())
|
r = requests.get(f"https://localhost:8000/generate_image?prompt={prompt}&path={path}")
|
||||||
|
return "image generated"
|
||||||
|
if mode == "dalle":
|
||||||
|
openai.api_key = openai_key
|
||||||
|
img = await openai.Image.acreate(
|
||||||
|
prompt=prompt,
|
||||||
|
n=1,
|
||||||
|
size="1024x1024",
|
||||||
|
)
|
||||||
|
return img
|
||||||
70
code/main.py
70
code/main.py
@@ -1,4 +1,7 @@
|
|||||||
use_images = True
|
#Uncomment you prefered images generation method
|
||||||
|
#use_images = "dalle" # generate images remotely with dalle 2 in your openai account
|
||||||
|
use_images = "sd" # generate images locally with stable diffusion sdkit ui (instructions in the readme)
|
||||||
|
#use_images = "No" # no images
|
||||||
import openai
|
import openai
|
||||||
# from openai import api_key
|
# from openai import api_key
|
||||||
import discord
|
import discord
|
||||||
@@ -10,10 +13,12 @@ import asyncio
|
|||||||
import logging
|
import logging
|
||||||
import datetime
|
import datetime
|
||||||
import base64
|
import base64
|
||||||
if use_images: import imagesGeneration
|
import requests
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
if use_images != "No": import imagesGeneration
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
imageint = ""
|
imageint = ""
|
||||||
if use_images: imageint = "To add an image illustration , use  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 \" \" but don't need to show a person necessairly."
|
if use_images != "No": imageint = "To add an image illustration , use  at the beginning of the slide, just after \"---\". Use only .png. 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 \" \" but don't need to show a person necessairly."
|
||||||
intstructions = f'''Here is a presentation with marp. It's not possible to make slides longer than 200 characters. to separate slides,
|
intstructions = f'''Here is a presentation with marp. It's not possible to make slides longer than 200 characters. to separate slides,
|
||||||
"
|
"
|
||||||
|
|
||||||
@@ -43,24 +48,30 @@ async def present(ctx: discord.ApplicationContext, subject: str, style: str = "d
|
|||||||
await ctx.defer()
|
await ctx.defer()
|
||||||
date = datetime.datetime.now()
|
date = datetime.datetime.now()
|
||||||
date = date.strftime("%Y-%m-%d-%H-%M-%S")
|
date = date.strftime("%Y-%m-%d-%H-%M-%S")
|
||||||
prompt = f"{intstructions} {indications} The subject of the presentation is: {subject} The Language is: {language} <|endofprompt|> \n"
|
marp = f'''---
|
||||||
#replace the spaces in the szbject with dashes
|
marp: true
|
||||||
|
theme: {styles[styles.index(style)]}
|
||||||
|
class:
|
||||||
|
- lead
|
||||||
|
'''
|
||||||
|
if style in darkstyles: marp = marp + f" - invert\n---"
|
||||||
|
else: marp = marp + "\n---"
|
||||||
|
prompt = f"{intstructions} {indications} The subject of the presentation is: {subject} The Language is: {language} <|endofprompt|> \n {marp}"
|
||||||
subject2 = subject
|
subject2 = subject
|
||||||
subject = subject.replace(" ", "-")
|
subject = subject.replace(" ", "-")
|
||||||
#we save teh subject in base64 in a variable
|
#we save teh subject in base64 in a variable
|
||||||
b64 = base64.urlsafe_b64encode(subject.encode("utf-8"))
|
b64 = base64.urlsafe_b64encode(subject.encode("utf-8"))
|
||||||
#if dosen't exist, create a directory called "userid" where the userid is the id of the user who called the command
|
#if dosen't exist, create a directory called "userid" where the userid is the id of the user who called the command
|
||||||
uid = str(ctx.author.id)
|
uid = str(ctx.author.id)
|
||||||
if not os.path.exists("data/"+uid):
|
if not os.path.exists("./data/"+uid):
|
||||||
os.mkdir("data/"+uid)
|
os.mkdir("./data/"+uid)
|
||||||
datenow = datetime.datetime.now()
|
datenow = datetime.datetime.now()
|
||||||
datenow = datenow.strftime("%Y-%m-%d-%H-%M-%S")
|
datenow = datenow.strftime("%Y-%m-%d-%H-%M-%S")
|
||||||
os.mkdir(f"data/{uid}/{b64}{datenow}")
|
os.mkdir(f"./data/{uid}/{b64}{datenow}")
|
||||||
response = await openai.Completion.acreate(
|
response = await openai.Completion.acreate(
|
||||||
engine="text-davinci-003",
|
engine="text-davinci-003",
|
||||||
prompt=prompt,
|
prompt=prompt,
|
||||||
temperature=0.7,
|
temperature=0.6,
|
||||||
max_tokens=1024,
|
max_tokens=1024,
|
||||||
top_p=1,
|
top_p=1,
|
||||||
frequency_penalty=0,
|
frequency_penalty=0,
|
||||||
@@ -69,19 +80,6 @@ async def present(ctx: discord.ApplicationContext, subject: str, style: str = "d
|
|||||||
)
|
)
|
||||||
#we save the output in a variable
|
#we save the output in a variable
|
||||||
output = response["choices"][0]["text"]
|
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[styles.index(style)]}
|
|
||||||
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
|
present = marp + output
|
||||||
##we save the output in a file called "subject.md"
|
##we save the output in a file called "subject.md"
|
||||||
matches = re.finditer(r'!\[.*?\]\((.*?)\)', present)
|
matches = re.finditer(r'!\[.*?\]\((.*?)\)', present)
|
||||||
@@ -89,23 +87,25 @@ class:
|
|||||||
for match in matches:
|
for match in matches:
|
||||||
image_filenames.append(match.group(1))
|
image_filenames.append(match.group(1))
|
||||||
#we create a text file with the image names and a md file for the presentation with utf8 encoding
|
#we create a text file with the image names and a md file for the presentation with utf8 encoding
|
||||||
with open(f"./data/{uid}/{b64}{datenow}/{subject}.md", "w", encoding="utf8") as f:
|
|
||||||
f.write(present)
|
|
||||||
with open(f"./data/{uid}/{b64}{datenow}/{subject}-images.txt", "w", encoding="utf8") as f:
|
with open(f"./data/{uid}/{b64}{datenow}/{subject}-images.txt", "w", encoding="utf8") as f:
|
||||||
for image in image_filenames:
|
for image in image_filenames:
|
||||||
f.write(image + "\n")
|
f.write(image + "\n")
|
||||||
#now we generate the images, if there are any
|
#now we generate the images, if there are any
|
||||||
if len(image_filenames) > 0 and use_images:
|
if len(image_filenames) > 0 and use_images!="no":
|
||||||
#now we first remove the extension from the image filenames by removing the last 4 characters
|
#now we first remove the extension from the image filenames by removing the last 4 characters
|
||||||
image_filenames = [image[:-4] for image in image_filenames]
|
image_filenames = [image[:-4] for image in image_filenames]
|
||||||
print(image_filenames)
|
print(image_filenames)
|
||||||
for images in image_filenames:
|
for images in image_filenames:
|
||||||
#we download the image
|
#we download the image
|
||||||
await imagesGeneration.generate(images, f"{os.getcwd()}\\data\\{uid}\\{b64}{datenow}\\")
|
print ("generating image" + images)
|
||||||
#now we rename the image to remove the _0 from the end of the filename
|
r = await imagesGeneration.generate(images, f"{os.getcwd()}\\data\\{uid}\\{b64}{datenow}\\", use_images, apikey)
|
||||||
os.rename(f"{os.getcwd()}\\data\\{uid}\\{b64}{datenow}\\{images}_0.png", f"{os.getcwd()}\\data\\{uid}\\{b64}{datenow}\\{images}.png")
|
if use_images == "sd": os.rename(f"{os.getcwd()}\\.\\data\\{uid}\\{b64}{datenow}\\{images}_0.png", f"{os.getcwd()}\\data\\{uid}\\{b64}{datenow}\\{images}.png")
|
||||||
#now we whait 10 seconds for discord to resume the websocket connection
|
if use_images == "dalle":
|
||||||
# await asyncio.sleep(10)
|
image_url = r['data'][0]['url']
|
||||||
|
img_data = requests.get(image_url).content
|
||||||
|
with open(f'./data/{uid}/{b64}{datenow}/{images}.png', 'wb') as handler:
|
||||||
|
handler.write(img_data)
|
||||||
|
with open(f"./data/{uid}/{b64}{datenow}/{subject}.md", "w", encoding="utf8") as f: f.write(present)
|
||||||
#we execute the command to convert the markdown file to a pdf and html file and also generate the first slide image
|
#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 ./data/{uid}/{b64}{datenow}/{subject}.md"
|
cmd = f"marp --pdf --allow-local-files ./data/{uid}/{b64}{datenow}/{subject}.md"
|
||||||
os.system(cmd)
|
os.system(cmd)
|
||||||
@@ -162,9 +162,9 @@ async def on_ready():
|
|||||||
if not os.path.exists("data"):
|
if not os.path.exists("data"):
|
||||||
os.mkdir("data")
|
os.mkdir("data")
|
||||||
#get the openai key drom he key.env file
|
#get the openai key drom he key.env file
|
||||||
with open("key.env", "r") as f:
|
load_dotenv()
|
||||||
apikey = f.read()
|
token = os.getenv("TOKEN")
|
||||||
|
apikey = os.getenv("OPENAI")
|
||||||
openai.api_key = apikey
|
openai.api_key = apikey
|
||||||
with open("token.env", "r") as f:
|
print(token)
|
||||||
token = f.read()
|
|
||||||
bot.run(token)
|
bot.run(token)
|
||||||
Reference in New Issue
Block a user