forked from Mru-preeti/Edu_Extract-pdfbased-text-extracter-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
593 lines (456 loc) · 18.8 KB
/
app.py
File metadata and controls
593 lines (456 loc) · 18.8 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
from flask import Flask, render_template, request, session, flash, redirect, send_file, url_for
import sqlite3
import os
import fitz # PyMuPDF for extracting text from PDFs
import re
import random
import nltk
import io
import json
from nltk.tokenize import word_tokenize
from nltk import pos_tag
from nltk.corpus import wordnet, stopwords
from flask import session
import smtplib
from flask_mail import Mail, Message
from fpdf import FPDF
from flask import send_file
from io import BytesIO
from fpdf import FPDF
from transformers import pipeline
from sentence_transformers import SentenceTransformer, util
import matplotlib.pyplot as plt
import base64
conn = sqlite3.connect('Login_data.db', check_same_thread=False)
def is_valid_sentence(line):
words = line.strip().split()
if len(words) < 5:
return False
if line.strip().isupper():
return False
return True
nltk.download('averaged_perceptron_tagger')
nltk.download('punkt')
nltk.download('wordnet')
nltk.download('stopwords')
app = Flask(__name__)
app.secret_key = os.urandom(24)
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'srushtiprajakt.t@gmail.com'
app.config['MAIL_PASSWORD'] = 'mqlzvpwdlqdqsbim'
mail = Mail(app)
UPLOAD_FOLDER = "uploads"
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
@app.route('/')
def login():
return render_template('login.html')
@app.route('/login_validation', methods=['POST'])
def login_validation():
username = request.form.get('username')
password = request.form.get('password')
conn = sqlite3.connect('Login_data.db')
cursor = conn.cursor()
try:
# Fetch the user with both username and password
user = cursor.execute(
"SELECT username, password FROM USERS WHERE username=? AND password=?", (username, password)).fetchone()
finally:
conn.close()
if user is None:
# No user found with the given username or incorrect password
return render_template("login.html", error="Incorrect username or password.")
stored_username, stored_password = user
# Successful login — store username in session and redirect
session['username'] = stored_username
return render_template('home.html', username=stored_username)
@app.route('/signup')
def signup():
return render_template('signup.html')
@app.route('/add_user', methods=['POST'])
def add_user():
username = request.form.get('username')
email_id = request.form.get('email_id')
password = request.form.get('password')
if not username or not email_id or not password:
# Handle missing fields gracefully
return "Please fill out all fields", 400
conn = sqlite3.connect('Login_data.db')
cursor = conn.cursor()
try:
# Check if username already exists
cursor.execute("SELECT * FROM USERS WHERE username=?", (username,))
existing_user = cursor.fetchone()
if existing_user:
# Already exists, redirect back to signup
return redirect('/signup')
# Insert the new user
cursor.execute("INSERT INTO USERS (username, email_id, password) VALUES (?, ?, ?)",
(username, email_id, password))
conn.commit()
return redirect('/')
except sqlite3.Error as e:
print("Database error:", e)
return "Internal server error", 500
finally:
conn.close()
@app.route('/prompt1')
def prompt1():
return render_template('prompt1.html')
@app.route('/upload_pdf', methods=['POST'])
def upload_pdf():
if "pdf" not in request.files:
return "No file found"
pdf = request.files["pdf"]
quiz_type = request.form.get('quiz_type')
if pdf.filename == "":
return "No file selected"
file_path = os.path.join(app.config["UPLOAD_FOLDER"], pdf.filename)
pdf.save(file_path)
extracted_text = extract_text_from_pdf(file_path)
questions, correct_answers = generate_questions(extracted_text, quiz_type)
session['questions'] = questions
session['correct_answers'] = correct_answers
return render_template('quiz.html', questions=questions)
@app.route('/home')
def home():
return render_template('home.html')
otp_store = {}
def extract_text_from_pdf(pdf_path):
text = ""
with fitz.open(pdf_path) as pdf_document:
for page in pdf_document:
text += page.get_text()
return text
def generate_questions(text, quiz_type):
questions = []
correct_answers = []
sentences = re.split(r'(?<=[.?!])\s+', text.strip())
for sentence in sentences:
words = word_tokenize(sentence)
tagged_words = pos_tag(words)
if not is_valid_sentence(sentence):
continue
# Skip sentences with too many words
if len(sentence.split()) > 20:
continue # ⛔ Skip long sentences
# Keep only meaningful words (nouns, verbs, adjectives)
important_words = [
word for word, tag in tagged_words if tag.startswith(('NN', 'VB', 'JJ'))]
# Remove stopwords
filtered_words = [word for word in important_words if word.lower(
) not in stopwords.words('english')]
if len(filtered_words) > 0:
if quiz_type == "fill_in_the_blank":
random_word = random.choice(filtered_words)
fill_in_blank = sentence.replace(random_word, "_____")
questions.append(f"Fill in the blank: {fill_in_blank}")
correct_answers.append(random_word)
elif quiz_type == "mcq":
correct_answer = random.choice(filtered_words)
wrong_options = generate_distractors(correct_answer)
if len(wrong_options) < 3:
continue # Skip this question if we don't have enough distractors
options = wrong_options + [correct_answer]
random.shuffle(options)
mcq = (
f"MCQ: {sentence.replace(correct_answer, '_____')}\n"
f"Options:\n"
f"A) {options[0]}\n"
f"B) {options[1]}\n"
f"C) {options[2]}\n"
f"D) {options[3]}"
)
questions.append(mcq)
# ✅ Ensure it's clean
correct_answers.append(correct_answer.strip())
elif quiz_type == "subjective":
# Use Hugging Face to generate a question
input_text = "generate question: " + sentence
output = qg_pipeline(
input_text, max_length=64, clean_up_tokenization_spaces=True)
question_text = output[0]['generated_text']
questions.append(f"Subjective Question: {question_text}")
# Original sentence is the expected answer
correct_answers.append(sentence)
if len(questions) >= 5:
break
return questions, correct_answers
def generate_distractors(word):
"""Generate wrong answer options using synonyms."""
synonyms = set()
for syn in wordnet.synsets(word):
for lemma in syn.lemmas():
if lemma.name().lower() != word.lower():
synonyms.add(lemma.name().replace("_", " "))
return list(synonyms)[:3] # Return up to 3 wrong options
def word_overlap_similarity(ans1, ans2):
words1 = set(word_tokenize(ans1.lower())) - set(stopwords.words('english'))
words2 = set(word_tokenize(ans2.lower())) - set(stopwords.words('english'))
if not words2:
return 0
overlap = words1.intersection(words2)
return len(overlap) / len(words2)
# Hugging Face pipelines
qg_pipeline = pipeline("text2text-generation", model="valhalla/t5-small-qg-hl")
# For similarity checking
similarity_model = SentenceTransformer('all-MiniLM-L6-v2')
@app.route('/submit_quiz', methods=['POST'])
def submit_quiz():
import json # make sure you have this at the top of your file
questions = session.get('questions', [])
correct_answers = session.get('correct_answers', [])
user_answers = []
detailed_results = []
score = 0
for i in range(len(correct_answers)):
user_answer = request.form.get(f'q{i+1}', '').strip().lower()
correct_answer = correct_answers[i].strip().lower()
user_answers.append(user_answer)
similarity_score = word_overlap_similarity(user_answer, correct_answer)
is_correct = similarity_score >= 0.5 # You can adjust this threshold
# if correct_answer.startswith("subjective:") or "subjective" in request.form.get(f'q{i+1}', '').lower():
# Use similarity check for subjective answers
# emb1 = similarity_model.encode(user_answer, convert_to_tensor=True)
# emb2 = similarity_model.encode(correct_answer, convert_to_tensor=True)
# similarity_score = util.pytorch_cos_sim(emb1, emb2).item()
# is_correct = similarity_score > 0.6 # You can tweak the threshold
# else:
# is_correct = user_answer == correct_answer
# is_correct = user_answer == correct_answer
if is_correct:
score += 1
detailed_results.append({
'qnum': i + 1,
'user_answer': user_answer,
'correct_answer': correct_answer,
'is_correct': is_correct,
'similarity_score': round(similarity_score * 100, 2),
'is_subjective': True # ✅ Add this only for subjective questions
})
# Store results in the database
username = session.get('username') # make sure the user is logged in
quiz_type = 'mcq' # optionally change this dynamically
questions = json.dumps(session.get('questions', []))
useranswers = json.dumps(user_answers)
correctanswers = json.dumps(correct_answers)
score_value = score
conn = sqlite3.connect('Login_data.db')
cursor = conn.cursor()
cursor.execute('''
INSERT INTO results (username, quiz_type, questions, user_answers, Correctanswer, score)
VALUES (?, ?, ?, ?, ?, ?)
''', (username, quiz_type, questions, useranswers, correctanswers, score_value))
conn.commit()
quiz_id = cursor.lastrowid # Get the ID of the newly inserted quiz
session['last_quiz_id'] = quiz_id # ✅ Store it in session
conn.close()
return render_template('result.html', score=score, total=len(correct_answers), results=detailed_results)
# Hugging Face pipelines
qg_pipeline = pipeline("text2text-generation", model="valhalla/t5-small-qg-hl")
# For similarity checking
similarity_model = SentenceTransformer('all-MiniLM-L6-v2')
def remove_unicode(text):
# Remove any character not supported by latin-1
return re.sub(r'[^\x00-\xFF]', '', text)
def generate_explanation(question, correct_answer):
# Dummy explanation
return f"The answer '{correct_answer}' is correct because it fits the context of the question."
@app.route('/download_result/pdf')
def download_result_pdf():
if 'username' not in session or 'last_quiz_id' not in session:
return redirect('/login')
quiz_id = session['last_quiz_id']
username = session['username']
# Fetch from DB
conn = sqlite3.connect('Login_data.db')
cursor = conn.cursor()
results = cursor.execute("""
SELECT quiz_type, questions, user_answers, Correctanswer, score, timestamp
FROM results WHERE username=? and id=?
""", (username, quiz_id)).fetchall()
conn.close()
# Create PDF
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 14)
pdf.cell(0, 10, f'Quiz Results for {username}', ln=True, align='C')
pdf.ln(10)
for idx, (quiz_type, questions, user_answers, correct_answers, score, timestamp) in enumerate(results, start=1):
pdf.set_font('Arial', 'B', 12)
pdf.set_fill_color(230, 230, 230)
pdf.cell(0, 8, f'Result #{idx} - {timestamp}', ln=True, fill=True)
pdf.ln(3)
try:
q_list = json.loads(questions)
a_list = json.loads(user_answers)
c_list = json.loads(correct_answers)
except:
q_list = [questions]
a_list = [user_answers]
c_list = [correct_answers]
pdf.set_font('Arial', '', 11)
for i in range(len(q_list)):
q = remove_unicode(q_list[i])
a = remove_unicode(a_list[i])
c = remove_unicode(c_list[i])
expl = remove_unicode(generate_explanation(q, c))
pdf.multi_cell(0, 8, f"Q{i+1}: {q}")
pdf.multi_cell(0, 8, f"Your Answer: {a}")
pdf.multi_cell(0, 8, f"Correct Answer: {c}")
pdf.set_text_color(100, 100, 100)
pdf.multi_cell(0, 8, f"Explanation: {expl}")
pdf.set_text_color(0, 0, 0)
pdf.ln(3)
pdf.set_font('Arial', 'B', 11)
pdf.cell(0, 10, f"Total Score: {score}/{len(q_list)}", ln=True)
pdf.ln(5)
# Output to BytesIO using dest='S'
pdf_bytes = BytesIO()
pdf_output_str = pdf.output(dest='S').encode(
'latin1') # Important: encode to latin1
pdf_bytes.write(pdf_output_str)
pdf_bytes.seek(0)
return send_file(pdf_bytes, download_name='quiz_results_clean.pdf', as_attachment=True)
def send_otp_email(recipient, otp):
sender_email = 'srushtiprajakt.t@gmail.com' # replace with your email
sender_password = 'mqlzvpwdlqdqsbim' # use app password if needed
subject = 'Your OTP for Password Reset'
body = f'Hello. This mail is from EduExtract. Your OTP is: {otp}'
message = f'Subject: {subject}\n\n{body}'
try:
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, recipient, message)
return True
except Exception as e:
print(f"Failed to send email: {e}")
return False
@app.route('/forgot_password', methods=['GET', 'POST'])
def forgot_password():
if request.method == 'POST':
username_input = request.form['username'].strip()
email_input = request.form['email_id'].strip()
conn = sqlite3.connect('Login_data.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM USERS WHERE username=? AND email_id=?",
(username_input, email_input))
user = cursor.fetchone()
conn.close()
if user:
otp = str(random.randint(100000, 999999))
session['reset_email'] = email_input
session['reset_username'] = username_input
session['otp'] = otp
print("Generated OTP:", otp)
print("Sending OTP to:", email_input)
try:
msg = Message('Password Reset OTP',
sender='srushtiprajakt.t@gmail.com', recipients=[email_input])
msg.body = f'Hello. This mail is from EduExtract. Your OTP for password reset is: {otp}'
mail.send(msg)
flash('OTP has been sent to your email.')
return redirect(url_for('verify_otp'))
except Exception as e:
flash('Failed to send OTP. Try again later.')
import traceback
traceback.print_exc()
else:
flash('Invalid username or email.')
return render_template('forgot_password.html')
@app.route('/verify_otp', methods=['GET', 'POST'])
def verify_otp():
if request.method == 'POST':
email_input = request.form['email_id']
user_otp = request.form['otp']
if (session.get('otp') == user_otp and session.get('reset_email') == email_input):
flash('OTP verified. Please reset your password.')
return render_template('reset_password.html', email=email_input)
else:
flash('Invalid OTP. Try again.')
return render_template('verify_otp.html', email=email_input)
# GET request
return render_template('verify_otp.html', email=session.get('reset_email'))
@app.route('/reset_password', methods=['POST'])
def reset_password():
email = session.get('reset_email') # safer than using hidden form field
new_password = request.form['new_password']
if not is_valid_password(new_password):
flash("Password doesn't meet criteria.")
return render_template('reset_password.html', email_id=email)
if email:
conn = sqlite3.connect('Login_data.db')
cursor = conn.cursor()
cursor.execute(
"UPDATE USERS SET password = ? WHERE email_id = ?", (new_password, email))
conn.commit()
conn.close()
flash('Password reset successfully. You can log in now.')
return redirect(url_for('login'))
else:
flash('Email session expired. Please try again.')
return redirect(url_for('forgot_password'))
def is_valid_password(password):
if len(password) < 8:
return False
if not re.search(r'[A-Z]', password):
return False
if not re.search(r'\d', password):
return False
if not re.search(r'[!@#$%^&*(),.?":{}|<>]', password):
return False
return True
@app.route('/perfomance')
def performance():
username = session.get('username')
if not username:
return redirect('/login')
conn = sqlite3.connect('Login_data.db')
cursor = conn.cursor()
cursor.execute("SELECT score FROM results WHERE username=?", (username,))
scores = cursor.fetchall()
conn.close()
user_results = [row[0] for row in scores]
if not user_results:
user_results = [0]
bar_chart = generate_bar_chart(user_results)
return render_template('perfomance.html', bar_chart=bar_chart)
@app.route('/quiz_history')
def quiz_history():
username = session.get('username')
if not username:
return redirect('/login')
conn = sqlite3.connect('Login_data.db')
cursor = conn.cursor()
cursor.execute("""
SELECT quiz_type, score, timestamp
FROM results
WHERE username=?
ORDER BY timestamp DESC
""", (username,))
history = cursor.fetchall()
conn.close()
return render_template('quiz_history.html', history=history)
def generate_bar_chart(user_results):
quizzes = [f"Quiz {i+1}" for i in range(len(user_results))]
scores = user_results
plt.figure(figsize=(12, 6)) # Increase figure size for clarity
bars = plt.bar(quizzes, scores, color='skyblue')
plt.title("Quiz Performance Over Time", fontsize=16)
plt.xlabel("Quiz", fontsize=12)
plt.ylabel("Score", fontsize=12)
plt.xticks(rotation=45, ha='right') # Rotate x-labels for visibility
plt.yticks(fontsize=10)
plt.tight_layout(pad=2)
img = io.BytesIO()
plt.savefig(img, format='png')
img.seek(0)
graph_url = base64.b64encode(img.getvalue()).decode()
plt.close()
return f"data:image/png;base64,{graph_url}"
if __name__ == '__main__':
app.run(debug=True)