63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
import openai
|
|
import sys
|
|
import os
|
|
|
|
API_KEY = os.environ.get("OPENAI_API_KEY")
|
|
if not API_KEY:
|
|
raise ValueError("Please set the OPENAI_API_KEY environment variable.")
|
|
|
|
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 = "output_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])
|