Update app.py
Browse files
app.py
CHANGED
|
@@ -4,17 +4,7 @@ import time
|
|
| 4 |
from PyPDF2 import PdfReader
|
| 5 |
|
| 6 |
# ============================
|
| 7 |
-
#
|
| 8 |
-
# ============================
|
| 9 |
-
try:
|
| 10 |
-
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 11 |
-
from reportlab.lib.styles import getSampleStyleSheet
|
| 12 |
-
REPORTLAB_AVAILABLE = True
|
| 13 |
-
except:
|
| 14 |
-
REPORTLAB_AVAILABLE = False
|
| 15 |
-
|
| 16 |
-
# ============================
|
| 17 |
-
# GLOBAL STORAGE (for uploaded PDF text)
|
| 18 |
# ============================
|
| 19 |
PDF_TEXT = ""
|
| 20 |
|
|
@@ -35,10 +25,10 @@ def load_pdf(file):
|
|
| 35 |
if t:
|
| 36 |
text += t
|
| 37 |
PDF_TEXT = text
|
| 38 |
-
return "✅ PDF Loaded Successfully!
|
| 39 |
|
| 40 |
# ============================
|
| 41 |
-
# CLEAR PDF (
|
| 42 |
# ============================
|
| 43 |
def clear_pdf():
|
| 44 |
global PDF_TEXT
|
|
@@ -105,8 +95,7 @@ def generate_quiz(text, num_questions, difficulty):
|
|
| 105 |
quiz.append({
|
| 106 |
"question": question,
|
| 107 |
"options": options,
|
| 108 |
-
"answer": answer
|
| 109 |
-
"hint": answer[0] + "..."
|
| 110 |
})
|
| 111 |
|
| 112 |
return quiz
|
|
@@ -117,7 +106,6 @@ def generate_quiz(text, num_questions, difficulty):
|
|
| 117 |
def start(text, num_q, minutes, difficulty):
|
| 118 |
global PDF_TEXT
|
| 119 |
|
| 120 |
-
# Use PDF if available, otherwise textbox
|
| 121 |
source_text = PDF_TEXT if PDF_TEXT else text
|
| 122 |
|
| 123 |
if not source_text or len(source_text.strip()) < 50:
|
|
@@ -156,12 +144,6 @@ def submit(ans, quiz, index, score, end_time):
|
|
| 156 |
|
| 157 |
return show_question(quiz, index+1, score, end_time, fb)
|
| 158 |
|
| 159 |
-
# ============================
|
| 160 |
-
# HINT
|
| 161 |
-
# ============================
|
| 162 |
-
def hint(quiz, index):
|
| 163 |
-
return f"💡 Hint: starts with '{quiz[index]['hint']}'"
|
| 164 |
-
|
| 165 |
# ============================
|
| 166 |
# FINISH + REVIEW MODE
|
| 167 |
# ============================
|
|
@@ -174,27 +156,6 @@ def finish_quiz(quiz, score):
|
|
| 174 |
result = f"# 🎯 Final Score: {score}/{len(quiz)}\n\n" + review
|
| 175 |
return result, gr.update(choices=[]), None, None, None
|
| 176 |
|
| 177 |
-
# ============================
|
| 178 |
-
# EXPORT PDF
|
| 179 |
-
# ============================
|
| 180 |
-
def export_pdf(quiz):
|
| 181 |
-
if not REPORTLAB_AVAILABLE:
|
| 182 |
-
return "⚠️ Install reportlab to export PDF."
|
| 183 |
-
|
| 184 |
-
doc = SimpleDocTemplate("quiz_output.pdf")
|
| 185 |
-
styles = getSampleStyleSheet()
|
| 186 |
-
|
| 187 |
-
elements = [Paragraph("<b>Generated Quiz</b>", styles['Title']), Spacer(1, 12)]
|
| 188 |
-
|
| 189 |
-
for i, q in enumerate(quiz):
|
| 190 |
-
elements.append(Paragraph(f"Q{i+1}: {q['question']}", styles['Normal']))
|
| 191 |
-
for opt in q["options"]:
|
| 192 |
-
elements.append(Paragraph(f"- {opt}", styles['Normal']))
|
| 193 |
-
elements.append(Spacer(1, 10))
|
| 194 |
-
|
| 195 |
-
doc.build(elements)
|
| 196 |
-
return "quiz_output.pdf"
|
| 197 |
-
|
| 198 |
# ============================
|
| 199 |
# UI WITH TABS
|
| 200 |
# ============================
|
|
@@ -221,12 +182,7 @@ with gr.Blocks() as app:
|
|
| 221 |
|
| 222 |
question = gr.Markdown()
|
| 223 |
options = gr.Radio(label="Choose Answer")
|
| 224 |
-
|
| 225 |
submit_btn = gr.Button("Submit Answer")
|
| 226 |
-
hint_btn = gr.Button("Need Hint?")
|
| 227 |
-
export_btn = gr.Button("Export Quiz to PDF")
|
| 228 |
-
|
| 229 |
-
hint_box = gr.Markdown()
|
| 230 |
|
| 231 |
quiz_state = gr.State()
|
| 232 |
index_state = gr.State()
|
|
@@ -239,7 +195,4 @@ with gr.Blocks() as app:
|
|
| 239 |
submit_btn.click(submit, [options, quiz_state, index_state, score_state, end_state],
|
| 240 |
[question, options, quiz_state, index_state, score_state, end_state])
|
| 241 |
|
| 242 |
-
hint_btn.click(hint, [quiz_state, index_state], hint_box)
|
| 243 |
-
export_btn.click(export_pdf, quiz_state, gr.File())
|
| 244 |
-
|
| 245 |
app.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 4 |
from PyPDF2 import PdfReader
|
| 5 |
|
| 6 |
# ============================
|
| 7 |
+
# GLOBAL STORAGE (PDF TEXT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# ============================
|
| 9 |
PDF_TEXT = ""
|
| 10 |
|
|
|
|
| 25 |
if t:
|
| 26 |
text += t
|
| 27 |
PDF_TEXT = text
|
| 28 |
+
return "✅ PDF Loaded Successfully!"
|
| 29 |
|
| 30 |
# ============================
|
| 31 |
+
# CLEAR PDF (CHANGE BOOK)
|
| 32 |
# ============================
|
| 33 |
def clear_pdf():
|
| 34 |
global PDF_TEXT
|
|
|
|
| 95 |
quiz.append({
|
| 96 |
"question": question,
|
| 97 |
"options": options,
|
| 98 |
+
"answer": answer
|
|
|
|
| 99 |
})
|
| 100 |
|
| 101 |
return quiz
|
|
|
|
| 106 |
def start(text, num_q, minutes, difficulty):
|
| 107 |
global PDF_TEXT
|
| 108 |
|
|
|
|
| 109 |
source_text = PDF_TEXT if PDF_TEXT else text
|
| 110 |
|
| 111 |
if not source_text or len(source_text.strip()) < 50:
|
|
|
|
| 144 |
|
| 145 |
return show_question(quiz, index+1, score, end_time, fb)
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
# ============================
|
| 148 |
# FINISH + REVIEW MODE
|
| 149 |
# ============================
|
|
|
|
| 156 |
result = f"# 🎯 Final Score: {score}/{len(quiz)}\n\n" + review
|
| 157 |
return result, gr.update(choices=[]), None, None, None
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
# ============================
|
| 160 |
# UI WITH TABS
|
| 161 |
# ============================
|
|
|
|
| 182 |
|
| 183 |
question = gr.Markdown()
|
| 184 |
options = gr.Radio(label="Choose Answer")
|
|
|
|
| 185 |
submit_btn = gr.Button("Submit Answer")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
quiz_state = gr.State()
|
| 188 |
index_state = gr.State()
|
|
|
|
| 195 |
submit_btn.click(submit, [options, quiz_state, index_state, score_state, end_state],
|
| 196 |
[question, options, quiz_state, index_state, score_state, end_state])
|
| 197 |
|
|
|
|
|
|
|
|
|
|
| 198 |
app.launch(server_name="0.0.0.0", server_port=7860)
|