diff --git a/utils/.config.template.toml b/utils/.config.template.toml index b2fa1d4..638a555 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -19,6 +19,9 @@ min_comments = { default = 20, optional = false, nmin = 10, type = "int", explan [ai] ai_similarity_enabled = {optional = true, option = [true, false], default = false, type = "bool", explanation = "Threads read from Reddit are sorted based on their similarity to the keywords given below"} ai_similarity_keywords = {optional = true, type="str", example= 'Elon Musk, Twitter, Stocks', explanation = "Every keyword or even sentence, seperated with comma, is used to sort the reddit threads based on similarity"} +use_openai = { optional = true, type = "bool", default = false, example = false, options = [true, false, ], explanation = "Whether to use OpenAI" } +openai_api_key = {optional = true, type="str", example= 'sk-1234-567', explanation = "If you want to access chatGPT you'll need to enter an API Key"} +openai_model = { optional = true, default = "gpt-3.5-turbo", example = "light", options = ["gpt-4", "gpt-3.5-turbo", ], explanation = "Sets completion model for OpenAI." } [settings] allow_nsfw = { optional = false, type = "bool", default = false, example = false, options = [true, false, ], explanation = "Whether to allow NSFW content, True or False" } diff --git a/utils/chatgpt.py b/utils/chatgpt.py new file mode 100644 index 0000000..9b5caa2 --- /dev/null +++ b/utils/chatgpt.py @@ -0,0 +1,46 @@ +import openai +from utils import settings +from utils.console import print_step + + +def shorten_filename(filename: str): + print_step("Now asking ChatGPT to shorten the filename") + openai.api_key = settings.config["ai"]["openai_api_key"] + # Use OpenAI to generate a shorter name that captures the essence of the original name + prompt = f"Create a new filename for the given file \"{filename}\" that is both descriptive and concise. The new filename should be no more than 95 characters in length and should not include any special characters or file extensions. Instead, focus on capturing the essence of the original name in a creative and attention-grabbing way. Avoid using generic terms and instead opt for a name that is both meaningful and memorable. Be sure to strike a balance between being humorous and staying true to the original name. Let your creativity shine!" + completions = openai.ChatCompletion.create( + model=settings.config["ai"]["openai_model"], + messages=[{"role": "user", "content": prompt}] + ) + filename = completions["choices"][0]["message"]["content"].strip().strip('"') + not_permitted_chars = ["<", ">", ":", "\"", "/", "\\", "|", "?", "*", "."] + for char in not_permitted_chars: + filename = filename.replace(char, "") + return filename + +def get_video_details( + subreddit: str, filename: str, reddit_title: str +): + """Gets a title, description and tags from chatGPT and saves as a txt file + + Args: + @param subreddit: + @param reddit_title: + """ + print_step("Now asking ChatGPT for Video Details") + openai.api_key = settings.config["ai"]["openai_api_key"] + messages = [ {"role": "system", "content": + "You are a intelligent assistant."} ] + messages.append( + {"role": "user", "content": "I would like you to supply a video title, description and tags comma-separated for a video about: " + reddit_title}, + ) + chat = openai.ChatCompletion.create( + model=settings.config["ai"]["openai_model"], messages=messages + ) + reply = chat.choices[0].message.content + print(f"ChatGPT: {reply}") + path = f"results/{subreddit}/{filename}" + path = path[:251] + path = path + ".txt" + with open(path, 'w', encoding="utf-8") as f: + f.write(reply) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 4838574..805f009 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -17,6 +17,8 @@ from utils.cleanup import cleanup from utils.console import print_step, print_substep from utils.thumbnail import create_thumbnail from utils.videos import save_data +from utils.chatgpt import get_video_details +from utils.chatgpt import shorten_filename console = Console() @@ -255,6 +257,8 @@ def make_final_video( title_thumb = reddit_obj["thread_title"] filename = f"{name_normalize(title)[:251]}" + if(settings.config["ai"]["use_openai"]): + filename = shorten_filename(title) subreddit = settings.config["reddit"]["thread"]["subreddit"] if not exists(f"./results/{subreddit}"): @@ -346,7 +350,8 @@ def make_final_video( old_percentage = pbar.n pbar.update(100 - old_percentage) pbar.close() - + if(settings.config["ai"]["use_openai"]): + get_video_details(subreddit, filename, title) save_data(subreddit, filename + ".mp4", title, idx, background_config[2]) print_step("Removing temporary files 🗑") cleanups = cleanup(reddit_id)