-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlashcard.py
263 lines (212 loc) · 11.2 KB
/
Flashcard.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import openai
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import PyPDF2
import threading
import os # For environment variables
class FlashcardApp:
def __init__(self, root):
self.root = root
self.root.title("Flashcard App")
self.root.geometry("600x600")
self.flashcards = []
self.current_card = 0
self.night_mode = False
self.cancelled = False # To track if the operation is cancelled
# Setup the UI
self.setup_ui()
def setup_ui(self):
"""Setup the user interface."""
# Main frame for content
self.main_frame = tk.Frame(self.root, padx=20, pady=20, bg="white")
self.main_frame.pack(fill=tk.BOTH, expand=True)
# Top frame for toggle button
top_frame = tk.Frame(self.main_frame, padx=20, pady=10, bg="white")
top_frame.pack(fill=tk.X, side=tk.TOP)
# Night mode toggle button (Top right)
self.toggle_button = tk.Button(top_frame, text="Toggle Night Mode", command=self.toggle_night_mode, font=("Arial", 12), bg="#6c757d", fg="white")
self.toggle_button.pack(side=tk.RIGHT)
# Upload button
self.upload_button = tk.Button(self.main_frame, text="Upload PDF for Flashcards", command=self.upload_pdf, font=("Arial", 12), bg="#007BFF", fg="white")
self.upload_button.pack(pady=10)
# Summarize button
self.summarize_button = tk.Button(self.main_frame, text="Summarize Document", command=self.summarize_document, font=("Arial", 12), bg="#28A745", fg="white")
self.summarize_button.pack(pady=10)
# Label for analysis feedback
self.analysis_label = tk.Label(self.main_frame, text="", font=("Arial", 12), bg="white")
self.analysis_label.pack(pady=10)
# Label for uploaded document name
self.uploaded_doc_label = tk.Label(self.main_frame, text="", font=("Arial", 12), bg="white")
self.uploaded_doc_label.pack(pady=10)
# Text widget for displaying document summary (non-editable)
self.summary_text = tk.Text(self.main_frame, wrap=tk.WORD, height=10, width=50, font=("Arial", 12), padx=10, pady=10, bg="light grey")
self.summary_text.pack(pady=10)
self.summary_text.config(state=tk.DISABLED)
# Flashcard count input
self.num_flashcards_label = tk.Label(self.main_frame, text="Number of flashcards to generate:", font=("Arial", 12), bg="white")
self.num_flashcards_label.pack(pady=5)
self.num_flashcards_entry = tk.Entry(self.main_frame, width=10, font=("Arial", 12))
self.num_flashcards_entry.insert(0, "10") # Default value
self.num_flashcards_entry.pack(pady=5)
# Progress bar
self.progress_bar = ttk.Progressbar(self.main_frame, length=300, mode='determinate')
self.progress_bar.pack(pady=20)
# Loading label
self.loading_label = tk.Label(self.main_frame, text="", font=("Arial", 12), bg="white")
self.loading_label.pack(pady=10)
# Cancel button
self.cancel_button = tk.Button(self.main_frame, text="Cancel", command=self.cancel_operation, font=("Arial", 12), bg="#DC3545", fg="white")
self.cancel_button.pack(pady=10)
def toggle_night_mode(self):
"""Toggle night mode on or off."""
self.night_mode = not self.night_mode
bg_color = "black" if self.night_mode else "white"
fg_color = "white" if self.night_mode else "black"
self.root.config(bg=bg_color)
self.main_frame.config(bg=bg_color)
self.analysis_label.config(bg=bg_color, fg=fg_color)
self.uploaded_doc_label.config(bg=bg_color, fg=fg_color)
self.loading_label.config(bg=bg_color, fg=fg_color)
for widget in self.main_frame.winfo_children():
if isinstance(widget, tk.Button) or isinstance(widget, tk.Entry) or isinstance(widget, tk.Label):
widget.config(bg=bg_color, fg=fg_color)
self.summary_text.config(bg="light grey" if self.night_mode else "white", fg=fg_color)
def upload_pdf(self):
"""Upload and analyze a PDF."""
pdf_file_path = filedialog.askopenfilename(title="Select a PDF", filetypes=[("PDF files", "*.pdf")])
if pdf_file_path:
self.cancelled = False # Reset the cancelled flag
self.analysis_label.config(text="Analyzing document...")
self.loading_label.config(text="")
self.uploaded_doc_label.config(text=f"Uploaded Document: {pdf_file_path.split('/')[-1]}")
self.root.update() # Update the GUI
threading.Thread(target=self.extract_flashcards, args=(pdf_file_path,), daemon=True).start()
def extract_flashcards(self, filepath):
"""Extract text from PDF and create flashcards."""
self.progress_bar['value'] = 0
self.flashcards = []
total_pages = 0
try:
num_flashcards = int(self.num_flashcards_entry.get())
if num_flashcards <= 0:
raise ValueError
except ValueError:
messagebox.showerror("Invalid Input", "Please enter a valid positive number for flashcards.")
return
with open(filepath, 'rb') as file:
reader = PyPDF2.PdfReader(file)
total_pages = len(reader.pages)
for i, page in enumerate(reader.pages):
if self.cancelled:
self.analysis_label.config(text="Operation cancelled.")
self.loading_label.config(text="")
return
text = page.extract_text()
if text:
lines = text.splitlines()
for line in lines:
if len(self.flashcards) >= num_flashcards:
break
if line.strip():
question, answer = self.create_flashcard_pair(line.strip())
self.flashcards.append((question, answer))
self.progress_bar['value'] = (i + 1) / total_pages * 100
self.root.update_idletasks()
if self.flashcards:
self.analysis_label.config(text="Flashcards generated!")
self.show_flashcard_screen() # Open flashcard window
else:
self.analysis_label.config(text="No text found. Please check the document format.")
self.progress_bar['value'] = 0
def create_flashcard_pair(self, line):
"""Create a question and answer from text."""
question = f"What is: '{line}'?"
answer = f"This is the answer: {line}" # Placeholder answer
return question, answer
def cancel_operation(self):
"""Cancel the current operation."""
self.cancelled = True
self.loading_label.config(text="Operation cancelled.")
def summarize_document(self):
"""Summarize the document using OpenAI's API."""
if not self.flashcards:
messagebox.showwarning("No Document", "Please upload a document first.")
return
self.loading_label.config(text="Summarizing document...")
self.root.update()
# Replace with your OpenAI API key from environment variables
openai.api_key = os.getenv("OPENAI_API_KEY")
# Prepare text for summarization
text_to_summarize = " ".join([f"{q} {a}" for q, a in self.flashcards])
try:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"Summarize the following content: {text_to_summarize}",
max_tokens=150,
temperature=0.7,
)
summary_text = response.choices[0].text.strip()
self.summary_text.config(state=tk.NORMAL)
self.summary_text.delete(1.0, tk.END)
self.summary_text.insert(tk.END, summary_text)
self.summary_text.config(state=tk.DISABLED)
self.loading_label.config(text="Summary done!")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {e}")
self.loading_label.config(text="Failed to summarize.")
def show_flashcard_screen(self):
"""Create and display the flashcard window."""
self.flashcard_window = tk.Toplevel(self.root)
self.flashcard_window.title("Flashcards")
self.flashcard_window.geometry("400x400")
self.current_card = 0 # Reset current card index
# Create UI for flashcards
self.question_label = tk.Label(self.flashcard_window, text="", font=("Helvetica", 16), wraplength=300)
self.question_label.pack(pady=10)
self.answer_label = tk.Label(self.flashcard_window, text="", font=("Helvetica", 14), wraplength=300)
self.answer_label.pack(pady=10)
# Show answer button
self.show_answer_button = tk.Button(self.flashcard_window, text="Show Question", command=self.show_answer, font=("Arial", 12), bg="#FFC107", fg="black")
self.show_answer_button.pack(pady=10)
# Navigation buttons
button_frame = tk.Frame(self.flashcard_window)
button_frame.pack(pady=10)
self.prev_button = tk.Button(button_frame, text="<< Previous", command=self.previous_flashcard, font=("Arial", 12), bg="#007BFF", fg="white")
self.prev_button.pack(side=tk.LEFT, padx=5)
self.next_button = tk.Button(button_frame, text="Next >>", command=self.next_flashcard, font=("Arial", 12), bg="#007BFF", fg="white")
self.next_button.pack(side=tk.LEFT, padx=5)
# Display first card
self.display_flashcard()
def display_flashcard(self):
"""Display the current flashcard."""
if self.flashcards:
question, answer = self.flashcards[self.current_card]
self.question_label.config(text=f"Flashcard {self.current_card + 1}/{len(self.flashcards)}: {answer}") # Start with Answer
self.answer_label.config(text="")
else:
self.question_label.config(text="No flashcards available.")
self.answer_label.config(text="")
def show_answer(self):
"""Toggle between showing the question and answer."""
question, answer = self.flashcards[self.current_card]
if self.answer_label.cget("text") == "":
self.answer_label.config(text=question)
self.show_answer_button.config(text="Show Answer")
else:
self.answer_label.config(text="")
self.show_answer_button.config(text="Show Question")
def next_flashcard(self):
"""Navigate to the next flashcard."""
if self.flashcards:
self.current_card = (self.current_card + 1) % len(self.flashcards)
self.display_flashcard()
def previous_flashcard(self):
"""Navigate to the previous flashcard."""
if self.flashcards:
self.current_card = (self.current_card - 1) % len(self.flashcards)
self.display_flashcard()
# Main application
if __name__ == "__main__":
root = tk.Tk()
app = FlashcardApp(root)
root.mainloop()