From ff06c093e37aa2efb3eff0b7427e8dfb6eb25396 Mon Sep 17 00:00:00 2001 From: Benjamin Dweck Date: Fri, 8 Sep 2023 12:19:33 +0300 Subject: [PATCH] Can convert from text to csv list of questions --- requirements.txt | 1 + text2csvdeck.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 text2csvdeck.py diff --git a/requirements.txt b/requirements.txt index 9aca352..f8f71dd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ genanki==0.8.0 Pillow +openai \ No newline at end of file diff --git a/text2csvdeck.py b/text2csvdeck.py new file mode 100644 index 0000000..9919c07 --- /dev/null +++ b/text2csvdeck.py @@ -0,0 +1,59 @@ +import openai +import sys + +# Set up the OpenAI API key +API_KEY = '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' +openai.api_key = API_KEY + +# Given prompt template +PROMPT_TEMPLATE = """ +Please come up with a set of 10 index cards for memorization, including front and back. +The index cards should completely capture the main points and themes of the text. +In addition, they should contain any numbers or data that humans might find difficult to remember. +The goal of the index card set is that one who memorizes it can provide a summary of the text to someone else, +conveying the main points and themes. + +You will provide the questions and answers to me in CSV format, as follows: +``` +Front,Back +What is the capital of New York?,Albany +Where in the world is Carmen San Diego?,Nobody knows +``` + +The question/answer pairs shall not be numbered or contain any signs of being ordered. + +{content} +""" + +def create_csv_deck(text_file_path): + # Read the text content + with open(text_file_path, 'r') as file: + text_content = file.read() + + # Prepare the prompt + prompt = PROMPT_TEMPLATE.format(content=text_content) + + # Get completion from the OpenAI ChatGPT API + response = openai.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=[ + {"role": "user", "content": prompt} + ], + temperature=0, + ) + + # Extract CSV content from response and save to a new file + csv_content = response.choices[0]['message']['content'] + output_filename = text_file_path.replace(".txt", "_deck.csv") + with open(output_filename, 'w') as csv_file: + csv_file.write(csv_content) + + print(f"Saved generated deck to {output_filename}") + + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: python text2csvdeck.py ") + sys.exit(1) + + create_csv_deck(sys.argv[1])