-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_1.0.0.py
More file actions
299 lines (247 loc) · 12.4 KB
/
reader_1.0.0.py
File metadata and controls
299 lines (247 loc) · 12.4 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import os
import sys
import time
import random
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QTextEdit, QLineEdit, QFileDialog, QMessageBox, QInputDialog
from PyQt6.QtCore import Qt
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_community.vectorstores import FAISS
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
class ChatInterface(QWidget):
def __init__(self):
super().__init__()
self.llm = None # Language model
self.db = None # Vector database
self.all_chunks = [] # All text chunks from PDFs
self.pdf_count = 0 # Number of PDFs processed
self.current_exam_question = 0 # Current question in exam simulation
self.exam_questions = [] # List of exam questions
self.wrong_answers = [] # List of wrong answers in exam simulation
self.api_key = self.get_api_key() # Get OpenAI API key from user
self.initUI() # Initialize the user interface
def get_api_key(self):
# Prompt user for OpenAI API key
api_key, ok = QInputDialog.getText(self, 'OpenAI API Key', 'Enter your OpenAI API key:')
if ok and api_key:
os.environ['OPENAI_API_KEY'] = api_key
return api_key
else:
QMessageBox.critical(self, 'Error', 'API key is required to use this application.')
sys.exit()
def initUI(self):
# Set up the main layout
layout = QVBoxLayout()
# Add button to select PDF directory
self.select_dir_button = QPushButton('Select Directory')
self.select_dir_button.clicked.connect(self.select_directory)
layout.addWidget(self.select_dir_button)
# Add text area for chat display
self.chat_area = QTextEdit()
self.chat_area.setReadOnly(True)
layout.addWidget(self.chat_area)
# Add input field and button for questions
input_layout = QHBoxLayout()
self.question_input = QLineEdit()
self.question_input.setPlaceholderText("Enter your question here")
self.question_input.returnPressed.connect(self.ask_question)
input_layout.addWidget(self.question_input)
self.ask_button = QPushButton('Ask')
self.ask_button.clicked.connect(self.ask_question)
input_layout.addWidget(self.ask_button)
layout.addLayout(input_layout)
# Set the main layout and window properties
self.setLayout(layout)
self.setWindowTitle('PDF Chat')
self.setGeometry(300, 300, 600, 400)
def select_directory(self):
# Open file dialog to select directory with PDFs
directory = QFileDialog.getExistingDirectory(self, "Select Directory")
if directory:
self.process_directory(directory)
def process_directory(self, directory):
# Process all PDFs in the selected directory
self.chat_area.append(f"Processing directory: {directory}")
QApplication.processEvents()
pdf_files = self.find_pdf_files(directory)
if not pdf_files:
self.chat_area.append("No PDF files found in the selected directory.")
return
self.pdf_count = len(pdf_files)
self.chat_area.append(f"Found {self.pdf_count} PDF files.")
QApplication.processEvents()
# Load and process each PDF
documents = []
for pdf_file in pdf_files:
self.chat_area.append(f"Processing file: {pdf_file}")
QApplication.processEvents()
loader = PyPDFLoader(pdf_file)
documents.extend(loader.load())
# Split documents into chunks
self.chat_area.append("Splitting text into chunks...")
QApplication.processEvents()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
self.all_chunks = text_splitter.split_documents(documents)
# Create embeddings and search index
self.chat_area.append("Creating embeddings...")
QApplication.processEvents()
embeddings = OpenAIEmbeddings()
self.chat_area.append("Creating search index...")
QApplication.processEvents()
self.db = FAISS.from_documents(self.all_chunks, embeddings)
# Initialize language model
self.chat_area.append("Initializing language model...")
QApplication.processEvents()
self.llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.2)
self.chat_area.append(f"System ready for questions! {self.pdf_count} PDF documents loaded.")
self.select_dir_button.setEnabled(False)
self.question_input.setEnabled(True)
self.ask_button.setEnabled(True)
def find_pdf_files(self, directory):
# Find all PDF files in the given directory and its subdirectories
pdf_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.pdf'):
pdf_files.append(os.path.join(root, file))
return pdf_files
def ask_question(self):
# Handle user questions or start exam simulation
if not self.llm or not self.db:
self.chat_area.append("Please select a directory with PDF files first.")
return
query = self.question_input.text()
if query:
self.chat_area.append(f"\nQuestion: {query}")
QApplication.processEvents()
try:
start_time = time.time()
if "simulazione di esame" in query.lower() or "exam simulation" in query.lower():
self.chat_area.append("Starting exam simulation...")
self.generate_exam_simulation()
else:
self.answer_question(query)
end_time = time.time()
self.chat_area.append(f"Processing time: {end_time - start_time:.2f} seconds")
except Exception as e:
self.chat_area.append(f"An error occurred: {str(e)}")
self.question_input.clear()
def generate_exam_simulation(self):
# Generate exam simulation questions
num_questions = 30
chunks_per_question = 2
selected_chunks = random.sample(self.all_chunks, min(num_questions * chunks_per_question, len(self.all_chunks)))
prompt_template = """Crea una domanda d'esame a scelta multipla basata sulle seguenti informazioni.
La domanda deve avere 4 possibili risposte, di cui solo una è corretta.
Informazioni:
{context}
Formato della risposta:
[Domanda]
a) [Opzione A]
b) [Opzione B]
c) [Opzione C]
d) [Opzione D]
Risposta corretta: [a/b/c/d]
Assicurati che la "Risposta corretta:" sia una singola lettera minuscola (a, b, c, o d) senza spazi aggiuntivi.
Crea una domanda d'esame in italiano:"""
PROMPT = PromptTemplate(template=prompt_template, input_variables=["context"])
llm_chain = LLMChain(llm=self.llm, prompt=PROMPT)
self.exam_questions = []
for i in range(0, len(selected_chunks), chunks_per_question):
chunk = " ".join([c.page_content for c in selected_chunks[i:i+chunks_per_question]])
question = llm_chain.run(context=chunk)
self.exam_questions.append(question)
self.chat_area.append(f"Question {len(self.exam_questions)} generated")
QApplication.processEvents()
self.current_exam_question = 0
self.wrong_answers = []
self.show_next_exam_question()
def show_next_exam_question(self):
# Display the next exam question or end the simulation
if self.current_exam_question < len(self.exam_questions):
question = self.exam_questions[self.current_exam_question]
self.chat_area.append(f"\nQuestion {self.current_exam_question + 1} of {len(self.exam_questions)}:")
for line in question.split('\n'):
if not line.startswith('Risposta corretta:'):
self.chat_area.append(line)
self.chat_area.append("\nEnter your answer (a, b, c, d), 'next' for the next question, or 'exit' to end the simulation:")
self.question_input.setPlaceholderText("Enter your answer here")
self.ask_button.clicked.disconnect()
self.ask_button.clicked.connect(self.check_exam_answer)
self.question_input.returnPressed.disconnect()
self.question_input.returnPressed.connect(self.check_exam_answer)
else:
self.reset_exam()
def check_exam_answer(self):
# Check the user's answer to the current exam question
answer = self.question_input.text().lower().strip()
if answer == 'exit':
self.chat_area.append("Ending exam simulation early.")
self.reset_exam()
elif answer == 'next':
self.current_exam_question += 1
self.show_next_exam_question()
elif answer in ['a', 'b', 'c', 'd']:
current_question = self.exam_questions[self.current_exam_question]
correct_answer = ''
for line in current_question.split('\n'):
if line.startswith('Risposta corretta:'):
correct_answer = line.split(':')[1].strip().lower()
break
# Debug logging
self.chat_area.append(f"Debug - Your answer: {answer}")
self.chat_area.append(f"Debug - Correct answer: {correct_answer}")
if answer == correct_answer:
self.chat_area.append("Correct!")
else:
self.chat_area.append(f"Wrong. The correct answer was: {correct_answer}")
self.wrong_answers.append((self.current_exam_question, current_question, answer, correct_answer))
self.current_exam_question += 1
self.show_next_exam_question()
else:
self.chat_area.append("Please enter 'a', 'b', 'c', 'd', 'next', or 'exit'.")
self.question_input.clear()
def reset_exam(self):
# End the exam simulation and display results
self.chat_area.append("\nExam simulation completed!")
if self.wrong_answers:
self.chat_area.append("\nWrong answers:")
for i, question, user_answer, correct_answer in self.wrong_answers:
self.chat_area.append(f"\nQuestion {i+1}:")
self.chat_area.append(question)
self.chat_area.append(f"Your answer: {user_answer}")
self.chat_area.append(f"Correct answer: {correct_answer}")
self.chat_area.append(f"\nYou answered {self.current_exam_question} out of {len(self.exam_questions)} questions.")
self.chat_area.append(f"Correct answers: {self.current_exam_question - len(self.wrong_answers)}")
self.chat_area.append(f"Wrong answers: {len(self.wrong_answers)}")
# Reset exam-related variables
self.current_exam_question = 0
self.exam_questions = []
self.wrong_answers = []
self.question_input.setPlaceholderText("Enter your question here")
self.ask_button.clicked.disconnect()
self.ask_button.clicked.connect(self.ask_question)
self.question_input.returnPressed.disconnect()
self.question_input.returnPressed.connect(self.ask_question)
def answer_question(self, query):
# Answer a general question based on the PDF content
docs = self.db.similarity_search(query, k=4)
context = " ".join([doc.page_content for doc in docs])
prompt_template = """Use the following information to answer the user's question.
If you don't find sufficient information to answer, say that you don't have enough information.
Information:
{context}
Question: {question}
Answer:"""
PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
llm_chain = LLMChain(llm=self.llm, prompt=PROMPT)
result = llm_chain.run(context=context, question=query)
self.chat_area.append(f"Answer: {result}")
self.chat_area.append(f"Sources: {[doc.metadata.get('source', 'Unknown') for doc in docs]}")
if __name__ == '__main__':
app = QApplication(sys.argv)
chat_interface = ChatInterface()
chat_interface.show()
sys.exit(app.exec())