Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,13 @@ import time
|
|
| 4 |
from PyPDF2 import PdfReader
|
| 5 |
|
| 6 |
# ============================
|
| 7 |
-
#
|
| 8 |
# ============================
|
| 9 |
quiz_data = []
|
| 10 |
current_question = 0
|
| 11 |
score = 0
|
| 12 |
start_time = None
|
| 13 |
-
|
| 14 |
-
time_per_question = 30 # seconds for each question
|
| 15 |
|
| 16 |
# ============================
|
| 17 |
# READ PDF
|
|
@@ -27,29 +26,22 @@ def extract_text_from_pdf(pdf_file):
|
|
| 27 |
# ============================
|
| 28 |
# GENERATE QUIZ
|
| 29 |
# ============================
|
| 30 |
-
def generate_quiz(pdf_file,
|
| 31 |
-
global quiz_data, current_question, score, start_time,
|
| 32 |
|
| 33 |
-
|
| 34 |
|
| 35 |
if pdf_file is None:
|
| 36 |
-
return "Please upload a PDF first.",
|
| 37 |
|
| 38 |
text = extract_text_from_pdf(pdf_file)
|
| 39 |
sentences = [s.strip() for s in text.split('.') if len(s.split()) > 6]
|
| 40 |
|
| 41 |
-
if len(sentences) <
|
| 42 |
-
return "Not enough content in PDF.",
|
| 43 |
|
| 44 |
random.shuffle(sentences)
|
| 45 |
|
| 46 |
-
if difficulty == "Easy":
|
| 47 |
-
num_questions = 5
|
| 48 |
-
elif difficulty == "Medium":
|
| 49 |
-
num_questions = 8
|
| 50 |
-
else:
|
| 51 |
-
num_questions = 12
|
| 52 |
-
|
| 53 |
quiz_data = []
|
| 54 |
for i in range(num_questions):
|
| 55 |
correct = sentences[i]
|
|
@@ -66,7 +58,6 @@ def generate_quiz(pdf_file, difficulty, per_question_time):
|
|
| 66 |
current_question = 0
|
| 67 |
score = 0
|
| 68 |
start_time = time.time()
|
| 69 |
-
question_start_time = time.time()
|
| 70 |
|
| 71 |
return show_question()
|
| 72 |
|
|
@@ -74,24 +65,19 @@ def generate_quiz(pdf_file, difficulty, per_question_time):
|
|
| 74 |
# SHOW QUESTION
|
| 75 |
# ============================
|
| 76 |
def show_question():
|
| 77 |
-
global current_question
|
| 78 |
-
|
| 79 |
if current_question >= len(quiz_data):
|
| 80 |
return show_dashboard()
|
| 81 |
|
| 82 |
q = quiz_data[current_question]
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
question_text = f"**Question {current_question+1}:** {q['question']}\n⏰ Time Left: {time_per_question} sec"
|
| 86 |
-
return question_text, gr.update(choices=q["options"], value=None)
|
| 87 |
|
| 88 |
# ============================
|
| 89 |
# SUBMIT ANSWER
|
| 90 |
# ============================
|
| 91 |
def submit_answer(selected):
|
| 92 |
-
global current_question, score
|
| 93 |
|
| 94 |
-
# if no question, show dashboard
|
| 95 |
if current_question >= len(quiz_data):
|
| 96 |
return show_dashboard()
|
| 97 |
|
|
@@ -101,25 +87,8 @@ def submit_answer(selected):
|
|
| 101 |
score += 1
|
| 102 |
|
| 103 |
current_question += 1
|
| 104 |
-
|
| 105 |
return show_question()
|
| 106 |
|
| 107 |
-
# ============================
|
| 108 |
-
# CHECK TIMER
|
| 109 |
-
# ============================
|
| 110 |
-
def check_timer():
|
| 111 |
-
global question_start_time, current_question
|
| 112 |
-
if current_question >= len(quiz_data):
|
| 113 |
-
return show_dashboard()
|
| 114 |
-
elapsed = int(time.time() - question_start_time)
|
| 115 |
-
remaining = time_per_question - elapsed
|
| 116 |
-
if remaining <= 0:
|
| 117 |
-
# Time out → move to next question
|
| 118 |
-
return submit_answer(None)
|
| 119 |
-
q = quiz_data[current_question]
|
| 120 |
-
question_text = f"**Question {current_question+1}:** {q['question']}\n⏰ Time Left: {remaining} sec"
|
| 121 |
-
return question_text, gr.update(choices=q["options"], value=None)
|
| 122 |
-
|
| 123 |
# ============================
|
| 124 |
# DASHBOARD
|
| 125 |
# ============================
|
|
@@ -134,19 +103,18 @@ def show_dashboard():
|
|
| 134 |
📊 Percentage: {percentage:.2f}%
|
| 135 |
⏱ Total Time Taken: {total_time} seconds
|
| 136 |
"""
|
| 137 |
-
return result,
|
| 138 |
|
| 139 |
# ============================
|
| 140 |
# UI
|
| 141 |
# ============================
|
| 142 |
-
with gr.Blocks(title="
|
| 143 |
-
|
| 144 |
-
gr.Markdown("# 📘 AI Quiz Generator with Per-Question Timer")
|
| 145 |
|
| 146 |
with gr.Tab("Upload & Setup"):
|
| 147 |
pdf_input = gr.File(label="Upload PDF")
|
| 148 |
-
|
| 149 |
-
per_question_time_input = gr.Number(value=30, label="
|
| 150 |
start_btn = gr.Button("Start Quiz")
|
| 151 |
|
| 152 |
with gr.Tab("Take Quiz"):
|
|
@@ -154,12 +122,12 @@ with gr.Blocks(title="Smart Quiz Generator with Timer") as app:
|
|
| 154 |
options = gr.Radio([], label="Choose Answer")
|
| 155 |
submit_btn = gr.Button("Submit Answer")
|
| 156 |
|
| 157 |
-
start_btn.click(generate_quiz,
|
|
|
|
| 158 |
outputs=[question_box, options])
|
| 159 |
|
| 160 |
-
submit_btn.click(submit_answer,
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
question_box.load(check_timer, [], [question_box, options], every=1)
|
| 164 |
|
| 165 |
app.launch()
|
|
|
|
| 4 |
from PyPDF2 import PdfReader
|
| 5 |
|
| 6 |
# ============================
|
| 7 |
+
# GLOBALS
|
| 8 |
# ============================
|
| 9 |
quiz_data = []
|
| 10 |
current_question = 0
|
| 11 |
score = 0
|
| 12 |
start_time = None
|
| 13 |
+
question_time = 30
|
|
|
|
| 14 |
|
| 15 |
# ============================
|
| 16 |
# READ PDF
|
|
|
|
| 26 |
# ============================
|
| 27 |
# GENERATE QUIZ
|
| 28 |
# ============================
|
| 29 |
+
def generate_quiz(pdf_file, num_questions, per_question_time):
|
| 30 |
+
global quiz_data, current_question, score, start_time, question_time
|
| 31 |
|
| 32 |
+
question_time = per_question_time
|
| 33 |
|
| 34 |
if pdf_file is None:
|
| 35 |
+
return "Please upload a PDF first.", []
|
| 36 |
|
| 37 |
text = extract_text_from_pdf(pdf_file)
|
| 38 |
sentences = [s.strip() for s in text.split('.') if len(s.split()) > 6]
|
| 39 |
|
| 40 |
+
if len(sentences) < num_questions:
|
| 41 |
+
return "Not enough content in PDF.", []
|
| 42 |
|
| 43 |
random.shuffle(sentences)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
quiz_data = []
|
| 46 |
for i in range(num_questions):
|
| 47 |
correct = sentences[i]
|
|
|
|
| 58 |
current_question = 0
|
| 59 |
score = 0
|
| 60 |
start_time = time.time()
|
|
|
|
| 61 |
|
| 62 |
return show_question()
|
| 63 |
|
|
|
|
| 65 |
# SHOW QUESTION
|
| 66 |
# ============================
|
| 67 |
def show_question():
|
| 68 |
+
global current_question
|
|
|
|
| 69 |
if current_question >= len(quiz_data):
|
| 70 |
return show_dashboard()
|
| 71 |
|
| 72 |
q = quiz_data[current_question]
|
| 73 |
+
return f"**Question {current_question+1}:** {q['question']}\n⏰ Time Allowed: {question_time} sec", q["options"]
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# ============================
|
| 76 |
# SUBMIT ANSWER
|
| 77 |
# ============================
|
| 78 |
def submit_answer(selected):
|
| 79 |
+
global current_question, score
|
| 80 |
|
|
|
|
| 81 |
if current_question >= len(quiz_data):
|
| 82 |
return show_dashboard()
|
| 83 |
|
|
|
|
| 87 |
score += 1
|
| 88 |
|
| 89 |
current_question += 1
|
|
|
|
| 90 |
return show_question()
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
# ============================
|
| 93 |
# DASHBOARD
|
| 94 |
# ============================
|
|
|
|
| 103 |
📊 Percentage: {percentage:.2f}%
|
| 104 |
⏱ Total Time Taken: {total_time} seconds
|
| 105 |
"""
|
| 106 |
+
return result, []
|
| 107 |
|
| 108 |
# ============================
|
| 109 |
# UI
|
| 110 |
# ============================
|
| 111 |
+
with gr.Blocks(title="Basic Quiz Generator") as app:
|
| 112 |
+
gr.Markdown("# 📘 Basic Quiz Generator")
|
|
|
|
| 113 |
|
| 114 |
with gr.Tab("Upload & Setup"):
|
| 115 |
pdf_input = gr.File(label="Upload PDF")
|
| 116 |
+
num_questions_input = gr.Number(value=5, label="Number of Questions")
|
| 117 |
+
per_question_time_input = gr.Number(value=30, label="Time per Question (seconds)")
|
| 118 |
start_btn = gr.Button("Start Quiz")
|
| 119 |
|
| 120 |
with gr.Tab("Take Quiz"):
|
|
|
|
| 122 |
options = gr.Radio([], label="Choose Answer")
|
| 123 |
submit_btn = gr.Button("Submit Answer")
|
| 124 |
|
| 125 |
+
start_btn.click(generate_quiz,
|
| 126 |
+
inputs=[pdf_input, num_questions_input, per_question_time_input],
|
| 127 |
outputs=[question_box, options])
|
| 128 |
|
| 129 |
+
submit_btn.click(submit_answer,
|
| 130 |
+
inputs=options,
|
| 131 |
+
outputs=[question_box, options])
|
|
|
|
| 132 |
|
| 133 |
app.launch()
|