feat: restore dynamic NCERT quiz with 505 questions from original repo
Browse files- src/api/v1/quiz.py +45 -0
- src/main.py +2 -0
src/api/v1/quiz.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from fastapi import APIRouter
|
| 3 |
+
from backend.src.math.knowledge_indexer import KnowledgeIndexer
|
| 4 |
+
|
| 5 |
+
router = APIRouter()
|
| 6 |
+
|
| 7 |
+
# Global cache for the quiz structure to avoid parsing markdown on every request
|
| 8 |
+
_QUIZ_STRUCTURE_CACHE = None
|
| 9 |
+
|
| 10 |
+
def build_quiz_structure():
|
| 11 |
+
global _QUIZ_STRUCTURE_CACHE
|
| 12 |
+
if _QUIZ_STRUCTURE_CACHE is not None:
|
| 13 |
+
return _QUIZ_STRUCTURE_CACHE
|
| 14 |
+
|
| 15 |
+
indexer = KnowledgeIndexer()
|
| 16 |
+
docs = indexer.load_markdown_files()
|
| 17 |
+
|
| 18 |
+
quiz_tree = {}
|
| 19 |
+
|
| 20 |
+
for doc in docs:
|
| 21 |
+
m = doc.metadata
|
| 22 |
+
cl = m.get("class_level", "")
|
| 23 |
+
ch = m.get("chapter", "")
|
| 24 |
+
ex = m.get("exercise", "")
|
| 25 |
+
|
| 26 |
+
if not (cl and ch and ex):
|
| 27 |
+
continue
|
| 28 |
+
|
| 29 |
+
# The exact same regex used in the original Streamlit app to extract Q1, Q2, etc.
|
| 30 |
+
qs = re.findall(r'Q\d+[^\n]*(?:\n(?!Q\d+)[^\n]*)*', doc.page_content)
|
| 31 |
+
|
| 32 |
+
if qs:
|
| 33 |
+
if cl not in quiz_tree:
|
| 34 |
+
quiz_tree[cl] = {}
|
| 35 |
+
if ch not in quiz_tree[cl]:
|
| 36 |
+
quiz_tree[cl][ch] = {}
|
| 37 |
+
quiz_tree[cl][ch][ex] = qs
|
| 38 |
+
|
| 39 |
+
_QUIZ_STRUCTURE_CACHE = quiz_tree
|
| 40 |
+
return quiz_tree
|
| 41 |
+
|
| 42 |
+
@router.get("/structure")
|
| 43 |
+
def get_quiz_structure():
|
| 44 |
+
"""Returns the nested tree of Classes -> Chapters -> Exercises -> Questions"""
|
| 45 |
+
return build_quiz_structure()
|
src/main.py
CHANGED
|
@@ -80,6 +80,8 @@ app.add_middleware(
|
|
| 80 |
app.include_router(chat.router)
|
| 81 |
app.include_router(progress.router)
|
| 82 |
|
|
|
|
|
|
|
| 83 |
|
| 84 |
@app.get("/health")
|
| 85 |
async def health_check():
|
|
|
|
| 80 |
app.include_router(chat.router)
|
| 81 |
app.include_router(progress.router)
|
| 82 |
|
| 83 |
+
from backend.src.api.v1 import quiz
|
| 84 |
+
app.include_router(quiz.router, prefix="/api/v1/quiz", tags=["Quiz"])
|
| 85 |
|
| 86 |
@app.get("/health")
|
| 87 |
async def health_check():
|