-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
142 lines (109 loc) · 3.97 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from dotenv import load_dotenv
import os
from google import genai
from typing import List
import argparse
env = os.getenv("GOOGLE_API_KEY")
MAIN_PROMPT = """
You are a helpful assistant tasked with converting markdown notes into flashcards. Here’s what you need to do:
1. **Read and Understand**:
- Analyze the content of the provided markdown notes.
- Identify the key points, definitions, or concepts that can be transformed into flashcard-style questions and answers.
2. **Generate Flashcards**:
- For each concept, create a question and answer pair.
- Use concise language for both the question and answer to ensure clarity.
3. **Formatting Requirements**:
- Output the flashcards in a tab-delimited format, with the question and answer separated by a single tab (`\t`).
- Each flashcard should be on a new line.
4. **Markdown Content**:
Here are the notes (in markdown format) that you need to process:
"""
def load_api_key(env_path: str = None) -> str:
"""
Function to get env variables and return google api key
:param env_path: Path to the .env file
:return: API key
"""
load_dotenv(env_path)
api_key = os.getenv("GOOGLE_API_KEY")
if not api_key:
raise ValueError("Google API key not found!")
return api_key
def get_markdown_files(folder_path):
"""
Gets all markdown files from folder path
:param folder_path: the path to .md notes or obsidian folder
:return: all markdown files as a str
"""
if not os.path.isdir(folder_path):
raise ValueError(f"The path {folder_path} is not a valid directory.")
markdown_files = [
os.path.join(folder_path, file)
for file in os.listdir(folder_path)
if file.endswith(".md")
]
if not markdown_files:
raise ValueError(f"No markdown files found in the directory {folder_path}.")
return markdown_files
def read_files_to_string(file_paths: List[str]):
"""
Reads markdown files and outputs a concatentated string of them
:param file_paths: list of .md files
:return: string of concatenated md files
"""
all_contents = []
for file_path in file_paths:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
all_contents.append(content)
return "\n".join(all_contents)
def get_response(notes: str, key: str) -> str:
"""
api call to get flashcards
:param notes: parsed md notes as str
:param key: google api key
:return: formatted flashcards
"""
client = genai.Client(api_key=key)
response = client.models.generate_content(
model="gemini-2.0-flash-exp", contents=MAIN_PROMPT + notes
)
return response.text
def main():
# Argument parsing
parser = argparse.ArgumentParser(
description="Convert markdown notes to tab-delimited flashcards using a Google API."
)
parser.add_argument(
"folder", type=str, help="Path to the folder containing markdown files."
)
parser.add_argument(
"--env",
type=str,
default=".env",
help="Path to the .env file containing the GOOGLE_API_KEY (default: .env).",
)
parser.add_argument(
"--output",
type=str,
default="flashcards.txt",
help="Path to save the flashcards output (default: flashcards.txt in the current directory).",
)
args = parser.parse_args()
try:
# Load API key
key = load_api_key(args.env)
# Get markdown files and read contents
files = get_markdown_files(args.folder)
file_contents = read_files_to_string(files)
# Generate flashcards via API
response = get_response(file_contents, key)
# Save output to file
output_path = args.output
with open(output_path, "w", encoding="utf-8") as output_file:
output_file.write(response)
print(f"Flashcards saved to {output_path}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()