Can convert from text to csv list of questions

master
B.J. Dweck 2023-09-08 12:19:33 +03:00
parent 51401ba964
commit ff06c093e3
2 changed files with 60 additions and 0 deletions

View File

@ -1,2 +1,3 @@
genanki==0.8.0
Pillow
openai

59
text2csvdeck.py Normal file
View File

@ -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 <text_file_path>")
sys.exit(1)
create_csv_deck(sys.argv[1])