-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonApplication1.py
More file actions
167 lines (145 loc) · 7.26 KB
/
PythonApplication1.py
File metadata and controls
167 lines (145 loc) · 7.26 KB
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
import os
import qai_hub as hub
import numpy as np
import torch
import torchvision
import sys
import pymupdf as pdf # PyMuPDF
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QFileDialog, QTextEdit
from transformers import AutoTokenizer, AutoModelForCausalLM
token = "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"
model_name = "meta-llama/Llama-3.2-3B"
tokenizer = AutoTokenizer.from_pretrained(model_name, token=token, trust_remote_code = True)
model = AutoModelForCausalLM.from_pretrained(model_name, token=token, trust_remote_code = True)
class StudyToolApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("AI-Powered ADHD Study Tool")
self.setGeometry(100, 100, 600, 400) # Set window size
# Create buttons for features
self.upload_button = QPushButton('Upload Textbook')
self.flashcards_button = QPushButton('Generate AI Flashcards')
self.flashcards_button.setEnabled(False)
self.summarize_button = QPushButton('Summarize Content')
self.summarize_button.setEnabled(False) # Disable button for now
self.quiz_button = QPushButton('Make a Quick Quiz')
self.quiz_button.setEnabled(False)
# Create a text area to display extracted text
self.text_display = QTextEdit(self)
self.text_display.setReadOnly(True) # Make text area read-only
# Connect buttons to functions
self.upload_button.clicked.connect(self.upload_textbook)
self.flashcards_button.clicked.connect(self.run_flashcards)
self.summarize_button.clicked.connect(self.summarize_text)
self.quiz_button.clicked.connect(self.make_quiz)
# Layout to arrange widgets
layout = QVBoxLayout()
layout.addWidget(self.upload_button)
layout.addWidget(self.flashcards_button)
layout.addWidget(self.summarize_button)
layout.addWidget(self.quiz_button)
layout.addWidget(self.text_display) # Add text area to layout
# Set layout and display window
self.setLayout(layout)
# Function to extract text from PDF using PyMuPDF
def extract_text_from_pdf(self, pdf_file):
doc = pdf.open(pdf_file) # Open the PDF file
text = ""
for page_num in range(doc.page_count): # Loop through each page
page = doc.load_page(page_num) # Get the page
text += page.get_text() # Extract text from the page
return text
# Function to handle textbook upload
def upload_textbook(self):
global file
file, _ = QFileDialog.getOpenFileName(self, "Open Textbook", "", "PDF Files (*.pdf);;Text Files (*.txt)")
if file:
print(f"Textbook uploaded: {file}")
text = self.extract_text_from_pdf(file)
self.text_display.setText(text) # Display the extracted text
self.quiz_button.setEnabled(True)
self.summarize_button.setEnabled(True)
self.flashcards_button.setEnabled(True)
# Placeholder functions for AI features (to be implemented later)
def generate_flashcards(self, max_new_tokens = 400):
self.text = self.extract_text_from_pdf(file)
self.count = int(input("Please enter the amount of flashcards you would like."))
prompt = f"Please create a python diction of {self.count} important terms in {self.text} as the keys and their definitions as the values \n\n Dictionary:"
inputs = tokenizer(prompt, return_tensors='pt')
outputs = model.generate(**inputs, max_new_tokens = max_new_tokens, do_sample = True,
temperature = 0.7) #Generate dictionary of words + definition
flashcards = tokenizer.decode(outputs[0], skip_special_tokens=True)
return flashcards
def run_flashcards(self):
app = QApplication(sys.argv)
window = FlashcardGame()
window.show()
sys.exit(app.exec())
def summarize_text(self, pdf_file, max_new_tokens = 0):
self.text = self.extract_text_from_pdf(file)
# Construct a prompt that instructs the model to summarize the text.
prompt = f"Please summarize the following text:\n\n{self.text}\n\nSummary:"
# Tokenize the prompt
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=max_new_tokens,
do_sample=True, temperature=0.7) # Generate the summary
summary = tokenizer.decode(outputs[0], skip_special_tokens=True) # decodes + returns text
return summary
def make_quiz(self):
print("Make a Quick Quiz")
# Add quiz model code here
class FlashcardGame(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("AI-generated Flashcards Matching Game")
# AI-generated flashcards
self.flashcards = self.StudyToolApp.generate_flashcards()
self.init_ui()
def init_ui(self):
# Main vertical layout
main_layout = QVBoxLayout()
# Horizontal layout for the two list widgets
lists_layout = QHBoxLayout()
# Create list widgets for terms and definitions
self.terms_list = QListWidget()
self.definitions_list = QListWidget()
# Create Flashcards:
# Populate the lists
self.terms = list(self.flashcards.keys())
self.definitions = list(self.flashcards.values())
random.shuffle(self.definitions) # Shuffle definitions for a challenge
for term in self.terms:
self.terms_list.addItem(term)
for definition in self.definitions:
self.definitions_list.addItem(definition)
# Add the lists to the horizontal layout
lists_layout.addWidget(self.terms_list)
lists_layout.addWidget(self.definitions_list)
# Create a button to check the selected match
self.check_button = QPushButton("Check Match")
self.check_button.clicked.connect(self.check_match)
# Assemble the main layout
main_layout.addLayout(lists_layout)
main_layout.addWidget(self.check_button)
self.setLayout(main_layout)
def check_match(self):
term_item = self.terms_list.currentItem()
definition_item = self.definitions_list.currentItem()
if term_item is None or definition_item is None:
QMessageBox.information(self, "Selection Error", "Please select both a term and a definition.")
return
term = term_item.text()
definition = definition_item.text()
# Check if the selected definition matches the term
if self.flashcards[term] == definition:
QMessageBox.information(self, "Result", f"Correct! '{term}' matches the definition.")
# Optionally remove the matched items
self.terms_list.takeItem(self.terms_list.currentRow())
self.definitions_list.takeItem(self.definitions_list.currentRow())
else:
QMessageBox.warning(self, "Result", "Incorrect match. Please try again.")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = StudyToolApp()
window.show()
sys.exit(app.exec())