-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew.py
More file actions
193 lines (165 loc) · 8.1 KB
/
new.py
File metadata and controls
193 lines (165 loc) · 8.1 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
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
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)
if tokenizer.pad_token is None:
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
model.resize_token_embeddings(len(tokenizer))
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):
if not file: # Ensure a file was uploaded
self.text_display.setText("No file uploaded.")
return
self.text = self.extract_text_from_pdf(file) # Extract text
if not self.text.strip(): # Handle empty text
self.text_display.setText("No text extracted from the file.")
return
self.text = self.extract_text_from_pdf(file)
max_length = 2048
self.count = int(input("Please enter the amount of flashcards you would like."))
prompt = f"Create a list of {self.count} important terms in {self.text[:500]} and their definitions as a python dictionary. Dictionary:"
inputs = tokenizer(prompt, return_tensors='pt', max_length = max_length, padding = "max_length", truncation = True)
outputs = model.generate(**inputs, max_new_tokens = 200, 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):
self.text_display.setText(self.generate_flashcards())
"""flashcards = self.generate_flashcards()
app = QApplication(sys.argv)
window = FlashcardGame(flashcards)
window.show()
sys.exit(app.exec())"""
def summarize_text(self):
if not file: # Ensure a file was uploaded
print("No file uploaded.")
return
self.text = self.extract_text_from_pdf(file) # Extract text
if not self.text.strip(): # Handle empty text
print("No text extracted from the file.")
return
# Construct a prompt for summarization
prompt = f"Please summarize the following text:\n\n{self.text[:1000]}\n\nSummary:"
# Limiting input to 1000 chars to prevent exceeding model limits
max_length = 1024
inputs = tokenizer(prompt, return_tensors="pt", max_length = max_length, padding = "max_length", truncation = True)
outputs = model.generate(**inputs, max_new_tokens=200, do_sample=True, temperature=0.7)
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
self.text_display.clear() # Clear previous text)
self.text_display.setText(summary) #Display Summary in the UI
def make_quiz(self):
print("Make a Quick Quiz")
# Add quiz model code here
class FlashcardGame(QWidget):
def __init__(self, flashcards):
super().__init__()
self.setWindowTitle("AI-generated Flashcards Matching Game")
# AI-generated flashcards
self.flashcards = 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())