Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,14 +11,14 @@ import re
|
|
| 11 |
quiz_history = []
|
| 12 |
|
| 13 |
# ============================
|
| 14 |
-
# Extract sentences
|
| 15 |
# ============================
|
| 16 |
def extract_sentences(text):
|
| 17 |
sentences = re.split(r'[.!?\n]', text)
|
| 18 |
return [s.strip() for s in sentences if len(s.strip()) > 20]
|
| 19 |
|
| 20 |
# ============================
|
| 21 |
-
# Generate quiz
|
| 22 |
# ============================
|
| 23 |
def generate_quiz(text, num_questions):
|
| 24 |
try:
|
|
@@ -130,23 +130,22 @@ def submit_answer(selected, quiz, index, score, end_time):
|
|
| 130 |
return show_question(quiz, index, score, end_time)
|
| 131 |
|
| 132 |
# ============================
|
| 133 |
-
# Prepare quiz (
|
| 134 |
# ============================
|
| 135 |
def prepare_quiz(text, pdf_library, selected_pdf, num_questions, timer_minutes):
|
| 136 |
|
| 137 |
-
# FIX: handle None
|
| 138 |
if text is None:
|
| 139 |
text = ""
|
| 140 |
|
| 141 |
-
# PDF
|
| 142 |
if selected_pdf and pdf_library and selected_pdf in pdf_library:
|
| 143 |
text = pdf_library[selected_pdf]
|
| 144 |
|
| 145 |
-
|
|
|
|
| 146 |
if not text.strip():
|
| 147 |
return "⚠️ Provide text or select a PDF.", None, None, None, None, None, None, ""
|
| 148 |
|
| 149 |
-
# Timer
|
| 150 |
try:
|
| 151 |
timer_minutes = float(timer_minutes)
|
| 152 |
if timer_minutes <= 0:
|
|
@@ -157,14 +156,14 @@ def prepare_quiz(text, pdf_library, selected_pdf, num_questions, timer_minutes):
|
|
| 157 |
quiz, msg = generate_quiz(text, num_questions)
|
| 158 |
|
| 159 |
if not quiz:
|
| 160 |
-
return
|
| 161 |
|
| 162 |
end_time = time.time() + timer_minutes * 60
|
| 163 |
|
| 164 |
return show_question(quiz, 0, 0, end_time)
|
| 165 |
|
| 166 |
# ============================
|
| 167 |
-
# Load PDFs
|
| 168 |
# ============================
|
| 169 |
def load_multiple_pdfs(files):
|
| 170 |
if files is None:
|
|
@@ -176,14 +175,21 @@ def load_multiple_pdfs(files):
|
|
| 176 |
for file in files:
|
| 177 |
try:
|
| 178 |
text = ""
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
name = file.name.split("/")[-1]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
pdf_texts[name] = text.strip()
|
| 188 |
names.append(name)
|
| 189 |
|
|
@@ -276,11 +282,7 @@ with gr.Blocks(title="Smart Quiz Generator") as app:
|
|
| 276 |
download_file = gr.File()
|
| 277 |
|
| 278 |
# EVENTS
|
| 279 |
-
pdf_input.change(
|
| 280 |
-
load_multiple_pdfs,
|
| 281 |
-
inputs=pdf_input,
|
| 282 |
-
outputs=[pdf_library_state, pdf_selector]
|
| 283 |
-
)
|
| 284 |
|
| 285 |
start_btn.click(
|
| 286 |
prepare_quiz,
|
|
|
|
| 11 |
quiz_history = []
|
| 12 |
|
| 13 |
# ============================
|
| 14 |
+
# Extract sentences
|
| 15 |
# ============================
|
| 16 |
def extract_sentences(text):
|
| 17 |
sentences = re.split(r'[.!?\n]', text)
|
| 18 |
return [s.strip() for s in sentences if len(s.strip()) > 20]
|
| 19 |
|
| 20 |
# ============================
|
| 21 |
+
# Generate quiz
|
| 22 |
# ============================
|
| 23 |
def generate_quiz(text, num_questions):
|
| 24 |
try:
|
|
|
|
| 130 |
return show_question(quiz, index, score, end_time)
|
| 131 |
|
| 132 |
# ============================
|
| 133 |
+
# Prepare quiz (TEXT + PDF FIXED)
|
| 134 |
# ============================
|
| 135 |
def prepare_quiz(text, pdf_library, selected_pdf, num_questions, timer_minutes):
|
| 136 |
|
|
|
|
| 137 |
if text is None:
|
| 138 |
text = ""
|
| 139 |
|
| 140 |
+
# Use PDF if selected
|
| 141 |
if selected_pdf and pdf_library and selected_pdf in pdf_library:
|
| 142 |
text = pdf_library[selected_pdf]
|
| 143 |
|
| 144 |
+
print("FINAL TEXT LENGTH:", len(text)) # debug
|
| 145 |
+
|
| 146 |
if not text.strip():
|
| 147 |
return "⚠️ Provide text or select a PDF.", None, None, None, None, None, None, ""
|
| 148 |
|
|
|
|
| 149 |
try:
|
| 150 |
timer_minutes = float(timer_minutes)
|
| 151 |
if timer_minutes <= 0:
|
|
|
|
| 156 |
quiz, msg = generate_quiz(text, num_questions)
|
| 157 |
|
| 158 |
if not quiz:
|
| 159 |
+
return "❌ Could not generate quiz. Try better text or PDF.", None, None, None, None, None, None, ""
|
| 160 |
|
| 161 |
end_time = time.time() + timer_minutes * 60
|
| 162 |
|
| 163 |
return show_question(quiz, 0, 0, end_time)
|
| 164 |
|
| 165 |
# ============================
|
| 166 |
+
# Load PDFs (FIXED)
|
| 167 |
# ============================
|
| 168 |
def load_multiple_pdfs(files):
|
| 169 |
if files is None:
|
|
|
|
| 175 |
for file in files:
|
| 176 |
try:
|
| 177 |
text = ""
|
| 178 |
+
|
| 179 |
+
pdf = PdfReader(file.name)
|
| 180 |
+
|
| 181 |
+
for page in pdf.pages:
|
| 182 |
+
page_text = page.extract_text()
|
| 183 |
+
if page_text:
|
| 184 |
+
text += page_text + " "
|
| 185 |
+
|
| 186 |
+
print(f"{file.name} TEXT LENGTH:", len(text)) # debug
|
| 187 |
|
| 188 |
name = file.name.split("/")[-1]
|
| 189 |
+
|
| 190 |
+
if not text.strip():
|
| 191 |
+
text = "This PDF has no readable text."
|
| 192 |
+
|
| 193 |
pdf_texts[name] = text.strip()
|
| 194 |
names.append(name)
|
| 195 |
|
|
|
|
| 282 |
download_file = gr.File()
|
| 283 |
|
| 284 |
# EVENTS
|
| 285 |
+
pdf_input.change(load_multiple_pdfs, inputs=pdf_input, outputs=[pdf_library_state, pdf_selector])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
start_btn.click(
|
| 288 |
prepare_quiz,
|