Spaces:
Sleeping
Sleeping
github-actions[bot] commited on
Commit ·
d68e358
1
Parent(s): 8a85a30
deploy: backend from e6c861a
Browse files- rag/curriculum_rag.py +128 -19
- rag/firebase_storage_loader.py +362 -16
- test_retrieval.py +165 -39
rag/curriculum_rag.py
CHANGED
|
@@ -20,6 +20,46 @@ def _normalize_subject(subject: Optional[str]) -> Optional[str]:
|
|
| 20 |
return raw
|
| 21 |
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def _to_where(
|
| 24 |
subject: Optional[str] = None,
|
| 25 |
quarter: Optional[int] = None,
|
|
@@ -50,7 +90,13 @@ def _to_where(
|
|
| 50 |
if competency_code:
|
| 51 |
clauses.append({"competency_code": {"$eq": competency_code}})
|
| 52 |
if storage_path:
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if not clauses:
|
| 55 |
return None
|
| 56 |
if len(clauses) == 1:
|
|
@@ -102,18 +148,50 @@ def retrieve_curriculum_context(
|
|
| 102 |
for idx, content in enumerate(documents):
|
| 103 |
md = metadatas[idx] if idx < len(metadatas) and isinstance(metadatas[idx], dict) else {}
|
| 104 |
distance = float(distances[idx]) if idx < len(distances) else 1.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
rows.append({
|
| 106 |
"content": str(content or ""),
|
| 107 |
"subject": str(md.get("subject") or "unknown"),
|
| 108 |
"quarter": int(md.get("quarter") or 0),
|
| 109 |
"content_domain": str(md.get("content_domain") or "general"),
|
| 110 |
"chunk_type": str(md.get("chunk_type") or "concept"),
|
| 111 |
-
"source_file":
|
| 112 |
-
"storage_path":
|
| 113 |
"module_id": str(md.get("module_id") or ""),
|
| 114 |
"lesson_id": str(md.get("lesson_id") or ""),
|
| 115 |
"competency_code": str(md.get("competency_code") or ""),
|
| 116 |
-
"page":
|
| 117 |
"score": _distance_to_score(distance),
|
| 118 |
})
|
| 119 |
return rows
|
|
@@ -168,21 +246,49 @@ def retrieve_lesson_pdf_context(
|
|
| 168 |
top_k: int = 8,
|
| 169 |
) -> Tuple[list[dict], str]:
|
| 170 |
"""Retrieve chunks by storage_path exact match + semantic ranking; fallback to general query.
|
| 171 |
-
|
| 172 |
-
NOTE: Curriculum PDF chunks are often tagged with quarter=1 even when
|
| 173 |
-
|
|
|
|
| 174 |
"""
|
| 175 |
-
|
| 176 |
if storage_path:
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
# Try 2: General query with exact quarter
|
| 188 |
general_chunks = retrieve_curriculum_context(
|
|
@@ -191,7 +297,7 @@ def retrieve_lesson_pdf_context(
|
|
| 191 |
quarter=quarter,
|
| 192 |
top_k=top_k,
|
| 193 |
)
|
| 194 |
-
|
| 195 |
# Try 3: Fallback to quarter=1 (most curriculum PDFs are tagged Q1)
|
| 196 |
if not general_chunks and quarter != 1:
|
| 197 |
general_chunks = retrieve_curriculum_context(
|
|
@@ -200,7 +306,7 @@ def retrieve_lesson_pdf_context(
|
|
| 200 |
quarter=1,
|
| 201 |
top_k=top_k,
|
| 202 |
)
|
| 203 |
-
|
| 204 |
# Try 4: Final fallback - no quarter filter at all
|
| 205 |
if not general_chunks:
|
| 206 |
general_chunks = retrieve_curriculum_context(
|
|
@@ -221,6 +327,9 @@ def retrieve_lesson_pdf_context(
|
|
| 221 |
deduped.sort(key=lambda x: x.get("score", 0), reverse=True)
|
| 222 |
return deduped[:top_k], "hybrid"
|
| 223 |
|
|
|
|
|
|
|
|
|
|
| 224 |
return general_chunks, "general"
|
| 225 |
|
| 226 |
|
|
|
|
| 20 |
return raw
|
| 21 |
|
| 22 |
|
| 23 |
+
def _normalize_storage_candidates(storage_path: Optional[str]) -> Tuple[List[str], List[str]]:
|
| 24 |
+
"""Return candidate (storage_paths, filenames) for exact-match retrieval."""
|
| 25 |
+
if not storage_path:
|
| 26 |
+
return [], []
|
| 27 |
+
clean = storage_path.replace("\\", "/").strip("/")
|
| 28 |
+
if clean.startswith("gs://"):
|
| 29 |
+
parts_gs = clean.split("/", 2)
|
| 30 |
+
if len(parts_gs) > 2:
|
| 31 |
+
clean = parts_gs[2]
|
| 32 |
+
|
| 33 |
+
name = clean.split("/")[-1]
|
| 34 |
+
stem = name.rsplit(".", 1)[0] if "." in name else name
|
| 35 |
+
|
| 36 |
+
candidate_files = [name]
|
| 37 |
+
if f"{stem}.pdf" not in candidate_files:
|
| 38 |
+
candidate_files.append(f"{stem}.pdf")
|
| 39 |
+
if f"{stem}.md" not in candidate_files:
|
| 40 |
+
candidate_files.append(f"{stem}.md")
|
| 41 |
+
|
| 42 |
+
candidate_paths = [clean]
|
| 43 |
+
if not clean.startswith("curriculum/"):
|
| 44 |
+
candidate_paths.append(f"curriculum/{clean}")
|
| 45 |
+
else:
|
| 46 |
+
candidate_paths.append(clean[len("curriculum/"):])
|
| 47 |
+
|
| 48 |
+
extended_paths = []
|
| 49 |
+
for p in candidate_paths:
|
| 50 |
+
extended_paths.append(p)
|
| 51 |
+
if p.endswith(".pdf"):
|
| 52 |
+
extended_paths.append(p[:-4] + ".md")
|
| 53 |
+
extended_paths.append(p.replace("/PDF/", "/Parsed Markdown/")[:-4] + ".md")
|
| 54 |
+
elif p.endswith(".md"):
|
| 55 |
+
extended_paths.append(p[:-3] + ".pdf")
|
| 56 |
+
extended_paths.append(p.replace("/Parsed Markdown/", "/PDF/")[:-3] + ".pdf")
|
| 57 |
+
|
| 58 |
+
cand_paths_deduped = list(dict.fromkeys(extended_paths))
|
| 59 |
+
cand_files_deduped = list(dict.fromkeys(candidate_files))
|
| 60 |
+
return cand_paths_deduped, cand_files_deduped
|
| 61 |
+
|
| 62 |
+
|
| 63 |
def _to_where(
|
| 64 |
subject: Optional[str] = None,
|
| 65 |
quarter: Optional[int] = None,
|
|
|
|
| 90 |
if competency_code:
|
| 91 |
clauses.append({"competency_code": {"$eq": competency_code}})
|
| 92 |
if storage_path:
|
| 93 |
+
cand_paths, cand_files = _normalize_storage_candidates(storage_path)
|
| 94 |
+
clauses.append({
|
| 95 |
+
"$or": [
|
| 96 |
+
{"storage_path": {"$in": cand_paths}},
|
| 97 |
+
{"source_file": {"$in": cand_files}},
|
| 98 |
+
]
|
| 99 |
+
})
|
| 100 |
if not clauses:
|
| 101 |
return None
|
| 102 |
if len(clauses) == 1:
|
|
|
|
| 148 |
for idx, content in enumerate(documents):
|
| 149 |
md = metadatas[idx] if idx < len(metadatas) and isinstance(metadatas[idx], dict) else {}
|
| 150 |
distance = float(distances[idx]) if idx < len(distances) else 1.0
|
| 151 |
+
|
| 152 |
+
ret_storage = str(md.get("storage_path") or "").strip()
|
| 153 |
+
ret_source_file = str(md.get("source_file") or "").strip()
|
| 154 |
+
ret_source_path = str(md.get("source_path") or "").strip()
|
| 155 |
+
|
| 156 |
+
if not ret_storage:
|
| 157 |
+
if ret_source_path:
|
| 158 |
+
norm_sp = ret_source_path.replace("\\", "/")
|
| 159 |
+
if "curriculum/" in norm_sp:
|
| 160 |
+
ret_storage = "curriculum/" + norm_sp.split("curriculum/", 1)[1]
|
| 161 |
+
if not ret_storage and storage_path:
|
| 162 |
+
norm_sp = storage_path.replace("\\", "/").strip("/")
|
| 163 |
+
ret_storage = norm_sp if norm_sp.startswith("curriculum/") else f"curriculum/{norm_sp}"
|
| 164 |
+
if not ret_storage and ret_source_file:
|
| 165 |
+
ret_storage = f"curriculum/{ret_source_file}"
|
| 166 |
+
|
| 167 |
+
if not ret_source_file:
|
| 168 |
+
if ret_storage:
|
| 169 |
+
ret_source_file = ret_storage.split("/")[-1]
|
| 170 |
+
elif ret_source_path:
|
| 171 |
+
ret_source_file = ret_source_path.replace("\\", "/").split("/")[-1]
|
| 172 |
+
|
| 173 |
+
raw_page = md.get("page")
|
| 174 |
+
if raw_page is not None and str(raw_page).isdigit() and int(raw_page) > 0:
|
| 175 |
+
page = int(raw_page)
|
| 176 |
+
else:
|
| 177 |
+
raw_chunk = md.get("chunk_index")
|
| 178 |
+
if raw_chunk is not None and str(raw_chunk).isdigit() and int(raw_chunk) > 0:
|
| 179 |
+
page = int(raw_chunk)
|
| 180 |
+
else:
|
| 181 |
+
page = 1
|
| 182 |
+
|
| 183 |
rows.append({
|
| 184 |
"content": str(content or ""),
|
| 185 |
"subject": str(md.get("subject") or "unknown"),
|
| 186 |
"quarter": int(md.get("quarter") or 0),
|
| 187 |
"content_domain": str(md.get("content_domain") or "general"),
|
| 188 |
"chunk_type": str(md.get("chunk_type") or "concept"),
|
| 189 |
+
"source_file": ret_source_file,
|
| 190 |
+
"storage_path": ret_storage,
|
| 191 |
"module_id": str(md.get("module_id") or ""),
|
| 192 |
"lesson_id": str(md.get("lesson_id") or ""),
|
| 193 |
"competency_code": str(md.get("competency_code") or ""),
|
| 194 |
+
"page": page,
|
| 195 |
"score": _distance_to_score(distance),
|
| 196 |
})
|
| 197 |
return rows
|
|
|
|
| 246 |
top_k: int = 8,
|
| 247 |
) -> Tuple[list[dict], str]:
|
| 248 |
"""Retrieve chunks by storage_path exact match + semantic ranking; fallback to general query.
|
| 249 |
+
|
| 250 |
+
NOTE: Curriculum PDF chunks are often tagged with quarter=1 or 0 even when covering other topics.
|
| 251 |
+
When storage_path is specified, exact-match retrieval searches by exact path/filename with
|
| 252 |
+
hierarchical fallback (exact quarter -> subject only -> file only) before falling back to general queries.
|
| 253 |
"""
|
| 254 |
+
exact_chunks: list[dict] = []
|
| 255 |
if storage_path:
|
| 256 |
+
# Try 1: Exact match with storage_path + quarter
|
| 257 |
+
if quarter and quarter > 0:
|
| 258 |
+
exact_chunks = retrieve_curriculum_context(
|
| 259 |
+
query=topic,
|
| 260 |
+
subject=subject,
|
| 261 |
+
quarter=quarter,
|
| 262 |
+
storage_path=storage_path,
|
| 263 |
+
top_k=top_k,
|
| 264 |
+
)
|
| 265 |
+
if exact_chunks and any(c.get("score", 0) >= 0.65 for c in exact_chunks):
|
| 266 |
+
return exact_chunks, "exact"
|
| 267 |
+
|
| 268 |
+
# Try 1b: Exact match with storage_path + subject (without quarter filter)
|
| 269 |
+
if not exact_chunks or not any(c.get("score", 0) >= 0.65 for c in exact_chunks):
|
| 270 |
+
fallback_chunks = retrieve_curriculum_context(
|
| 271 |
+
query=topic,
|
| 272 |
+
subject=subject,
|
| 273 |
+
storage_path=storage_path,
|
| 274 |
+
top_k=top_k,
|
| 275 |
+
)
|
| 276 |
+
if fallback_chunks:
|
| 277 |
+
exact_chunks = fallback_chunks
|
| 278 |
+
if any(c.get("score", 0) >= 0.65 for c in exact_chunks):
|
| 279 |
+
return exact_chunks, "exact"
|
| 280 |
+
|
| 281 |
+
# Try 1c: Exact match with storage_path alone (file only)
|
| 282 |
+
if not exact_chunks or not any(c.get("score", 0) >= 0.65 for c in exact_chunks):
|
| 283 |
+
fallback_chunks = retrieve_curriculum_context(
|
| 284 |
+
query=topic,
|
| 285 |
+
storage_path=storage_path,
|
| 286 |
+
top_k=top_k,
|
| 287 |
+
)
|
| 288 |
+
if fallback_chunks:
|
| 289 |
+
exact_chunks = fallback_chunks
|
| 290 |
+
if any(c.get("score", 0) >= 0.65 for c in exact_chunks):
|
| 291 |
+
return exact_chunks, "exact"
|
| 292 |
|
| 293 |
# Try 2: General query with exact quarter
|
| 294 |
general_chunks = retrieve_curriculum_context(
|
|
|
|
| 297 |
quarter=quarter,
|
| 298 |
top_k=top_k,
|
| 299 |
)
|
| 300 |
+
|
| 301 |
# Try 3: Fallback to quarter=1 (most curriculum PDFs are tagged Q1)
|
| 302 |
if not general_chunks and quarter != 1:
|
| 303 |
general_chunks = retrieve_curriculum_context(
|
|
|
|
| 306 |
quarter=1,
|
| 307 |
top_k=top_k,
|
| 308 |
)
|
| 309 |
+
|
| 310 |
# Try 4: Final fallback - no quarter filter at all
|
| 311 |
if not general_chunks:
|
| 312 |
general_chunks = retrieve_curriculum_context(
|
|
|
|
| 327 |
deduped.sort(key=lambda x: x.get("score", 0), reverse=True)
|
| 328 |
return deduped[:top_k], "hybrid"
|
| 329 |
|
| 330 |
+
if exact_chunks and not general_chunks:
|
| 331 |
+
return exact_chunks, "exact"
|
| 332 |
+
|
| 333 |
return general_chunks, "general"
|
| 334 |
|
| 335 |
|
rag/firebase_storage_loader.py
CHANGED
|
@@ -8,7 +8,8 @@ from __future__ import annotations
|
|
| 8 |
import logging
|
| 9 |
import os
|
| 10 |
from pathlib import Path
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
logger = logging.getLogger("mathpulse.fb_storage_loader")
|
| 14 |
|
|
@@ -159,41 +160,115 @@ def list_curriculum_blobs(prefix: str = "curriculum/") -> List[Dict[str, Any]]:
|
|
| 159 |
|
| 160 |
|
| 161 |
def infer_storage_metadata(storage_path: str) -> Dict[str, Any]:
|
| 162 |
-
"""Infer metadata for a curriculum resource stored in Firebase Storage."""
|
| 163 |
-
clean_path = storage_path.replace("\\", "/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
parts = [p.lower() for p in clean_path.split("/")]
|
| 165 |
joined = " ".join(parts)
|
|
|
|
|
|
|
| 166 |
filename = parts[-1] if parts else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
|
| 169 |
-
subject_id = "finite-math" if "finite math" in joined else "gen-math"
|
| 170 |
quarter = 0
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
-
|
| 177 |
-
if "las" in filename:
|
| 178 |
resource_type = "learning_activity_sheet"
|
| 179 |
-
elif
|
| 180 |
resource_type = "lesson_exemplar"
|
| 181 |
-
elif
|
| 182 |
resource_type = "curriculum_guide"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
return {
|
| 185 |
"subject": subject,
|
| 186 |
"subjectId": subject_id,
|
| 187 |
"type": resource_type,
|
| 188 |
-
"
|
|
|
|
| 189 |
"quarter": quarter,
|
| 190 |
-
"storage_path":
|
| 191 |
"filename": filename,
|
| 192 |
}
|
| 193 |
|
| 194 |
|
| 195 |
PDF_METADATA: Dict[str, dict] = {
|
| 196 |
-
# General Mathematics Q1 — SDO Navotas teaching
|
| 197 |
"curriculum/gen_math_sdo/SDO_Navotas_Gen.Math_SHS_1stSem.FV.pdf": {
|
| 198 |
"subject": "General Mathematics",
|
| 199 |
"subjectId": "gen-math",
|
|
@@ -202,6 +277,30 @@ PDF_METADATA: Dict[str, dict] = {
|
|
| 202 |
"quarter": 1,
|
| 203 |
"storage_path": "curriculum/gen_math_sdo/SDO_Navotas_Gen.Math_SHS_1stSem.FV.pdf",
|
| 204 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
# General Mathematics Q2 — Interest & Annuities modules (~27-35 pages each)
|
| 206 |
"curriculum/general_math/genmath_q2_mod1_simpleandcompoundinterests_v2.pdf": {
|
| 207 |
"subject": "General Mathematics",
|
|
@@ -244,4 +343,251 @@ PDF_METADATA: Dict[str, dict] = {
|
|
| 244 |
"quarter": 1,
|
| 245 |
"storage_path": "curriculum/stat_prob/Full.pdf",
|
| 246 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
}
|
|
|
|
| 8 |
import logging
|
| 9 |
import os
|
| 10 |
from pathlib import Path
|
| 11 |
+
import re
|
| 12 |
+
from typing import Any, Dict, List, Optional, Tuple
|
| 13 |
|
| 14 |
logger = logging.getLogger("mathpulse.fb_storage_loader")
|
| 15 |
|
|
|
|
| 160 |
|
| 161 |
|
| 162 |
def infer_storage_metadata(storage_path: str) -> Dict[str, Any]:
|
| 163 |
+
"""Infer metadata for a curriculum resource stored in Firebase Storage or local filesystem."""
|
| 164 |
+
clean_path = storage_path.replace("\\", "/").strip("/")
|
| 165 |
+
if clean_path.startswith("gs://"):
|
| 166 |
+
parts_gs = clean_path.split("/", 2)
|
| 167 |
+
if len(parts_gs) > 2:
|
| 168 |
+
clean_path = parts_gs[2]
|
| 169 |
+
|
| 170 |
parts = [p.lower() for p in clean_path.split("/")]
|
| 171 |
joined = " ".join(parts)
|
| 172 |
+
subparts = parts[1:] if len(parts) > 1 and parts[0] == "curriculum" else parts
|
| 173 |
+
sub_joined = " ".join(subparts)
|
| 174 |
filename = parts[-1] if parts else ""
|
| 175 |
+
filename_lower = filename.lower()
|
| 176 |
+
|
| 177 |
+
# Subject and SubjectId detection
|
| 178 |
+
if "stat_prob" in joined or "statistics" in joined or "prob" in joined:
|
| 179 |
+
subject = "Statistics and Probability"
|
| 180 |
+
subject_id = "stats-prob"
|
| 181 |
+
content_domain = "statistics"
|
| 182 |
+
elif "finite math 1" in joined or "finite-math-1" in joined or "finite-mathematics-1" in joined:
|
| 183 |
+
subject = "Finite Mathematics 1"
|
| 184 |
+
subject_id = "finite-math-1"
|
| 185 |
+
content_domain = "general"
|
| 186 |
+
elif "finite math 2" in joined or "finite-math-2" in joined or "finite-mathematics-2" in joined:
|
| 187 |
+
subject = "Finite Mathematics 2"
|
| 188 |
+
subject_id = "finite-math-2"
|
| 189 |
+
content_domain = "general"
|
| 190 |
+
elif "finite math" in joined or "finite" in joined:
|
| 191 |
+
subject = "Finite Mathematics"
|
| 192 |
+
subject_id = "finite-math"
|
| 193 |
+
content_domain = "general"
|
| 194 |
+
elif "bus.math" in joined or "business math" in joined or "bus_math" in joined:
|
| 195 |
+
subject = "Business Mathematics"
|
| 196 |
+
subject_id = "business-math"
|
| 197 |
+
content_domain = "general"
|
| 198 |
+
elif "org" in joined and ("mngt" in joined or "mgmt" in joined or "management" in joined):
|
| 199 |
+
subject = "Organization and Management"
|
| 200 |
+
subject_id = "org-mgmt"
|
| 201 |
+
content_domain = "general"
|
| 202 |
+
else:
|
| 203 |
+
subject = "General Mathematics"
|
| 204 |
+
subject_id = "gen-math"
|
| 205 |
+
content_domain = "general"
|
| 206 |
|
| 207 |
+
# Quarter detection (1, 2, 3, 4)
|
|
|
|
| 208 |
quarter = 0
|
| 209 |
+
q_match = re.search(r"(?:quarter[\s_/-]*|[\b_/-]q)([1-4])(?!\d)", clean_path, re.IGNORECASE)
|
| 210 |
+
if q_match:
|
| 211 |
+
quarter = int(q_match.group(1))
|
| 212 |
+
elif "1stsem" in joined or "1st sem" in joined or "firstsem" in joined or "first sem" in joined or "term 1" in joined:
|
| 213 |
+
quarter = 1
|
| 214 |
+
elif "2ndsem" in joined or "2nd sem" in joined or "secondsem" in joined or "second sem" in joined or "term 2" in joined:
|
| 215 |
+
quarter = 2
|
| 216 |
+
elif "finite math 1" in joined:
|
| 217 |
+
quarter = 1
|
| 218 |
+
elif "finite math 2" in joined:
|
| 219 |
+
quarter = 2
|
| 220 |
+
elif "stat_prob" in joined:
|
| 221 |
+
quarter = 1
|
| 222 |
+
|
| 223 |
+
# Resource type detection: learning_activity_sheet, lesson_exemplar, curriculum_guide, sdo_module
|
| 224 |
+
is_las = bool(re.search(r"\b(las|learning[\s_-]*activity)\b|[-_]las\d*[-_.]", sub_joined, re.IGNORECASE))
|
| 225 |
+
is_le = bool(re.search(r"\b(lesson[\s_-]*exemplar)\b|[-_]le\d*[-_.]|\ble\d+\b", sub_joined, re.IGNORECASE))
|
| 226 |
+
is_guide = bool(
|
| 227 |
+
re.search(r"\b(curriculum[\s_-]*guide|budget[\s_-]*of[\s_-]*work|budget)\b|curriculum & budget", sub_joined, re.IGNORECASE)
|
| 228 |
+
or filename_lower.startswith("general-mathematics-")
|
| 229 |
+
or filename_lower.startswith("finite-mathematics-")
|
| 230 |
+
)
|
| 231 |
|
| 232 |
+
if is_las:
|
|
|
|
| 233 |
resource_type = "learning_activity_sheet"
|
| 234 |
+
elif is_le:
|
| 235 |
resource_type = "lesson_exemplar"
|
| 236 |
+
elif is_guide:
|
| 237 |
resource_type = "curriculum_guide"
|
| 238 |
+
else:
|
| 239 |
+
resource_type = "sdo_module"
|
| 240 |
+
|
| 241 |
+
curated = PDF_METADATA.get(storage_path) or PDF_METADATA.get(clean_path)
|
| 242 |
+
if not curated and clean_path.startswith("curriculum/"):
|
| 243 |
+
curated = PDF_METADATA.get(clean_path[len("curriculum/"):])
|
| 244 |
+
if not curated and not clean_path.startswith("curriculum/"):
|
| 245 |
+
curated = PDF_METADATA.get(f"curriculum/{clean_path}")
|
| 246 |
+
if not curated and filename:
|
| 247 |
+
curated = PDF_METADATA.get(filename)
|
| 248 |
+
|
| 249 |
+
if curated:
|
| 250 |
+
subject = curated.get("subject", subject)
|
| 251 |
+
subject_id = curated.get("subjectId", subject_id)
|
| 252 |
+
resource_type = curated.get("type") or curated.get("resource_type", resource_type)
|
| 253 |
+
content_domain = curated.get("content_domain", content_domain)
|
| 254 |
+
quarter = curated.get("quarter", quarter)
|
| 255 |
+
|
| 256 |
+
full_storage_path = clean_path if clean_path.startswith("curriculum/") else f"curriculum/{clean_path}"
|
| 257 |
|
| 258 |
return {
|
| 259 |
"subject": subject,
|
| 260 |
"subjectId": subject_id,
|
| 261 |
"type": resource_type,
|
| 262 |
+
"resource_type": resource_type,
|
| 263 |
+
"content_domain": content_domain,
|
| 264 |
"quarter": quarter,
|
| 265 |
+
"storage_path": full_storage_path,
|
| 266 |
"filename": filename,
|
| 267 |
}
|
| 268 |
|
| 269 |
|
| 270 |
PDF_METADATA: Dict[str, dict] = {
|
| 271 |
+
# General Mathematics Q1 — SDO Navotas teaching modules
|
| 272 |
"curriculum/gen_math_sdo/SDO_Navotas_Gen.Math_SHS_1stSem.FV.pdf": {
|
| 273 |
"subject": "General Mathematics",
|
| 274 |
"subjectId": "gen-math",
|
|
|
|
| 277 |
"quarter": 1,
|
| 278 |
"storage_path": "curriculum/gen_math_sdo/SDO_Navotas_Gen.Math_SHS_1stSem.FV.pdf",
|
| 279 |
},
|
| 280 |
+
"curriculum/gen_math_sdo/SHS_GM_Q1_LAS1_LE1.pdf": {
|
| 281 |
+
"subject": "General Mathematics",
|
| 282 |
+
"subjectId": "gen-math",
|
| 283 |
+
"type": "learning_activity_sheet",
|
| 284 |
+
"content_domain": "general",
|
| 285 |
+
"quarter": 1,
|
| 286 |
+
"storage_path": "curriculum/gen_math_sdo/SHS_GM_Q1_LAS1_LE1.pdf",
|
| 287 |
+
},
|
| 288 |
+
"curriculum/gen_math_sdo/SHS_GM_Q1_LAS2_LE1.pdf": {
|
| 289 |
+
"subject": "General Mathematics",
|
| 290 |
+
"subjectId": "gen-math",
|
| 291 |
+
"type": "learning_activity_sheet",
|
| 292 |
+
"content_domain": "general",
|
| 293 |
+
"quarter": 1,
|
| 294 |
+
"storage_path": "curriculum/gen_math_sdo/SHS_GM_Q1_LAS2_LE1.pdf",
|
| 295 |
+
},
|
| 296 |
+
"curriculum/gen_math_sdo/SHS_GM_Q1_LAS3_LE1.pdf": {
|
| 297 |
+
"subject": "General Mathematics",
|
| 298 |
+
"subjectId": "gen-math",
|
| 299 |
+
"type": "learning_activity_sheet",
|
| 300 |
+
"content_domain": "general",
|
| 301 |
+
"quarter": 1,
|
| 302 |
+
"storage_path": "curriculum/gen_math_sdo/SHS_GM_Q1_LAS3_LE1.pdf",
|
| 303 |
+
},
|
| 304 |
# General Mathematics Q2 — Interest & Annuities modules (~27-35 pages each)
|
| 305 |
"curriculum/general_math/genmath_q2_mod1_simpleandcompoundinterests_v2.pdf": {
|
| 306 |
"subject": "General Mathematics",
|
|
|
|
| 343 |
"quarter": 1,
|
| 344 |
"storage_path": "curriculum/stat_prob/Full.pdf",
|
| 345 |
},
|
| 346 |
+
# SSHS Learning Resources: Finite Mathematics 1 & 2
|
| 347 |
+
"curriculum/sshs_learning_resources/Finite Mathematics/Finite Math 1/PDF/Finite Math 1_LAS.pdf": {
|
| 348 |
+
"subject": "Finite Mathematics 1",
|
| 349 |
+
"subjectId": "finite-math-1",
|
| 350 |
+
"type": "learning_activity_sheet",
|
| 351 |
+
"content_domain": "general",
|
| 352 |
+
"quarter": 1,
|
| 353 |
+
"storage_path": "curriculum/sshs_learning_resources/Finite Mathematics/Finite Math 1/PDF/Finite Math 1_LAS.pdf",
|
| 354 |
+
},
|
| 355 |
+
"curriculum/sshs_learning_resources/Finite Mathematics/Finite Math 1/PDF/Finite Math 1_LE.pdf": {
|
| 356 |
+
"subject": "Finite Mathematics 1",
|
| 357 |
+
"subjectId": "finite-math-1",
|
| 358 |
+
"type": "lesson_exemplar",
|
| 359 |
+
"content_domain": "general",
|
| 360 |
+
"quarter": 1,
|
| 361 |
+
"storage_path": "curriculum/sshs_learning_resources/Finite Mathematics/Finite Math 1/PDF/Finite Math 1_LE.pdf",
|
| 362 |
+
},
|
| 363 |
+
"curriculum/sshs_learning_resources/Finite Mathematics/Finite Math 2/PDF/Finite Math 2_LAS.pdf": {
|
| 364 |
+
"subject": "Finite Mathematics 2",
|
| 365 |
+
"subjectId": "finite-math-2",
|
| 366 |
+
"type": "learning_activity_sheet",
|
| 367 |
+
"content_domain": "general",
|
| 368 |
+
"quarter": 2,
|
| 369 |
+
"storage_path": "curriculum/sshs_learning_resources/Finite Mathematics/Finite Math 2/PDF/Finite Math 2_LAS.pdf",
|
| 370 |
+
},
|
| 371 |
+
"curriculum/sshs_learning_resources/Finite Mathematics/Finite Math 2/PDF/Finite Math 2_LE.pdf": {
|
| 372 |
+
"subject": "Finite Mathematics 2",
|
| 373 |
+
"subjectId": "finite-math-2",
|
| 374 |
+
"type": "lesson_exemplar",
|
| 375 |
+
"content_domain": "general",
|
| 376 |
+
"quarter": 2,
|
| 377 |
+
"storage_path": "curriculum/sshs_learning_resources/Finite Mathematics/Finite Math 2/PDF/Finite Math 2_LE.pdf",
|
| 378 |
+
},
|
| 379 |
+
# SSHS Learning Resources: General Mathematics Complete Course (Term 1)
|
| 380 |
+
"curriculum/sshs_learning_resources/General Mathematics/Complete Course (Term 1)/PDF/General Mathematics_LAS.pdf": {
|
| 381 |
+
"subject": "General Mathematics",
|
| 382 |
+
"subjectId": "gen-math",
|
| 383 |
+
"type": "learning_activity_sheet",
|
| 384 |
+
"content_domain": "general",
|
| 385 |
+
"quarter": 1,
|
| 386 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Complete Course (Term 1)/PDF/General Mathematics_LAS.pdf",
|
| 387 |
+
},
|
| 388 |
+
"curriculum/sshs_learning_resources/General Mathematics/Complete Course (Term 1)/PDF/General Mathematics_LE.pdf": {
|
| 389 |
+
"subject": "General Mathematics",
|
| 390 |
+
"subjectId": "gen-math",
|
| 391 |
+
"type": "lesson_exemplar",
|
| 392 |
+
"content_domain": "general",
|
| 393 |
+
"quarter": 1,
|
| 394 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Complete Course (Term 1)/PDF/General Mathematics_LE.pdf",
|
| 395 |
+
},
|
| 396 |
+
# SSHS Learning Resources: Curriculum Guide & Budget of Work
|
| 397 |
+
"curriculum/sshs_learning_resources/General Mathematics/Curriculum & Budget of Work/PDF/GENERAL-MATHEMATICS-1.pdf": {
|
| 398 |
+
"subject": "General Mathematics",
|
| 399 |
+
"subjectId": "gen-math",
|
| 400 |
+
"type": "curriculum_guide",
|
| 401 |
+
"content_domain": "general",
|
| 402 |
+
"quarter": 1,
|
| 403 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Curriculum & Budget of Work/PDF/GENERAL-MATHEMATICS-1.pdf",
|
| 404 |
+
},
|
| 405 |
+
"curriculum/sshs_learning_resources/General Mathematics/Curriculum & Budget of Work/PDF/General-Mathematics-2.pdf": {
|
| 406 |
+
"subject": "General Mathematics",
|
| 407 |
+
"subjectId": "gen-math",
|
| 408 |
+
"type": "curriculum_guide",
|
| 409 |
+
"content_domain": "general",
|
| 410 |
+
"quarter": 2,
|
| 411 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Curriculum & Budget of Work/PDF/General-Mathematics-2.pdf",
|
| 412 |
+
},
|
| 413 |
+
# SSHS Learning Resources: General Mathematics Quarter 1
|
| 414 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Learning Activity Sheets/PDF/SHS_GM_Q1_LAS1.pdf": {
|
| 415 |
+
"subject": "General Mathematics",
|
| 416 |
+
"subjectId": "gen-math",
|
| 417 |
+
"type": "learning_activity_sheet",
|
| 418 |
+
"content_domain": "general",
|
| 419 |
+
"quarter": 1,
|
| 420 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Learning Activity Sheets/PDF/SHS_GM_Q1_LAS1.pdf",
|
| 421 |
+
},
|
| 422 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Learning Activity Sheets/PDF/SHS_GM_Q1_LAS2.pdf": {
|
| 423 |
+
"subject": "General Mathematics",
|
| 424 |
+
"subjectId": "gen-math",
|
| 425 |
+
"type": "learning_activity_sheet",
|
| 426 |
+
"content_domain": "general",
|
| 427 |
+
"quarter": 1,
|
| 428 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Learning Activity Sheets/PDF/SHS_GM_Q1_LAS2.pdf",
|
| 429 |
+
},
|
| 430 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Learning Activity Sheets/PDF/SHS_GM_Q1_LAS4.pdf": {
|
| 431 |
+
"subject": "General Mathematics",
|
| 432 |
+
"subjectId": "gen-math",
|
| 433 |
+
"type": "learning_activity_sheet",
|
| 434 |
+
"content_domain": "general",
|
| 435 |
+
"quarter": 1,
|
| 436 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Learning Activity Sheets/PDF/SHS_GM_Q1_LAS4.pdf",
|
| 437 |
+
},
|
| 438 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Lesson Exemplars/PDF/SHS_GM_Q1_LE1.pdf": {
|
| 439 |
+
"subject": "General Mathematics",
|
| 440 |
+
"subjectId": "gen-math",
|
| 441 |
+
"type": "lesson_exemplar",
|
| 442 |
+
"content_domain": "general",
|
| 443 |
+
"quarter": 1,
|
| 444 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Lesson Exemplars/PDF/SHS_GM_Q1_LE1.pdf",
|
| 445 |
+
},
|
| 446 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Lesson Exemplars/PDF/SHS_GM_Q1_LE2.pdf": {
|
| 447 |
+
"subject": "General Mathematics",
|
| 448 |
+
"subjectId": "gen-math",
|
| 449 |
+
"type": "lesson_exemplar",
|
| 450 |
+
"content_domain": "general",
|
| 451 |
+
"quarter": 1,
|
| 452 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Lesson Exemplars/PDF/SHS_GM_Q1_LE2.pdf",
|
| 453 |
+
},
|
| 454 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Lesson Exemplars/PDF/SHS_GM_Q1_LE3.pdf": {
|
| 455 |
+
"subject": "General Mathematics",
|
| 456 |
+
"subjectId": "gen-math",
|
| 457 |
+
"type": "lesson_exemplar",
|
| 458 |
+
"content_domain": "general",
|
| 459 |
+
"quarter": 1,
|
| 460 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Lesson Exemplars/PDF/SHS_GM_Q1_LE3.pdf",
|
| 461 |
+
},
|
| 462 |
+
# SSHS Learning Resources: General Mathematics Quarter 2
|
| 463 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 2/Learning Activity Sheets/PDF/SHS_GM_Q2_LAS2.pdf": {
|
| 464 |
+
"subject": "General Mathematics",
|
| 465 |
+
"subjectId": "gen-math",
|
| 466 |
+
"type": "learning_activity_sheet",
|
| 467 |
+
"content_domain": "general",
|
| 468 |
+
"quarter": 2,
|
| 469 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 2/Learning Activity Sheets/PDF/SHS_GM_Q2_LAS2.pdf",
|
| 470 |
+
},
|
| 471 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 2/Lesson Exemplars/PDF/SHS_GM_Q2_LE4.pdf": {
|
| 472 |
+
"subject": "General Mathematics",
|
| 473 |
+
"subjectId": "gen-math",
|
| 474 |
+
"type": "lesson_exemplar",
|
| 475 |
+
"content_domain": "general",
|
| 476 |
+
"quarter": 2,
|
| 477 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 2/Lesson Exemplars/PDF/SHS_GM_Q2_LE4.pdf",
|
| 478 |
+
},
|
| 479 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 2/Lesson Exemplars/PDF/SHS_GM_Q2_LE5.pdf": {
|
| 480 |
+
"subject": "General Mathematics",
|
| 481 |
+
"subjectId": "gen-math",
|
| 482 |
+
"type": "lesson_exemplar",
|
| 483 |
+
"content_domain": "general",
|
| 484 |
+
"quarter": 2,
|
| 485 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 2/Lesson Exemplars/PDF/SHS_GM_Q2_LE5.pdf",
|
| 486 |
+
},
|
| 487 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 2/Lesson Exemplars/PDF/SHS_GM_Q2_LE6.pdf": {
|
| 488 |
+
"subject": "General Mathematics",
|
| 489 |
+
"subjectId": "gen-math",
|
| 490 |
+
"type": "lesson_exemplar",
|
| 491 |
+
"content_domain": "general",
|
| 492 |
+
"quarter": 2,
|
| 493 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 2/Lesson Exemplars/PDF/SHS_GM_Q2_LE6.pdf",
|
| 494 |
+
},
|
| 495 |
+
# SSHS Learning Resources: General Mathematics Quarter 3
|
| 496 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 3/Learning Activity Sheets/PDF/SHS_GM_Q3_LAS_LE7.pdf": {
|
| 497 |
+
"subject": "General Mathematics",
|
| 498 |
+
"subjectId": "gen-math",
|
| 499 |
+
"type": "learning_activity_sheet",
|
| 500 |
+
"content_domain": "general",
|
| 501 |
+
"quarter": 3,
|
| 502 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 3/Learning Activity Sheets/PDF/SHS_GM_Q3_LAS_LE7.pdf",
|
| 503 |
+
},
|
| 504 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 3/Learning Activity Sheets/PDF/SHS_GM_Q3_LAS_LE8.pdf": {
|
| 505 |
+
"subject": "General Mathematics",
|
| 506 |
+
"subjectId": "gen-math",
|
| 507 |
+
"type": "learning_activity_sheet",
|
| 508 |
+
"content_domain": "general",
|
| 509 |
+
"quarter": 3,
|
| 510 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 3/Learning Activity Sheets/PDF/SHS_GM_Q3_LAS_LE8.pdf",
|
| 511 |
+
},
|
| 512 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 3/Learning Activity Sheets/PDF/SHS_GM_Q3_LAS_LE9.pdf": {
|
| 513 |
+
"subject": "General Mathematics",
|
| 514 |
+
"subjectId": "gen-math",
|
| 515 |
+
"type": "learning_activity_sheet",
|
| 516 |
+
"content_domain": "general",
|
| 517 |
+
"quarter": 3,
|
| 518 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 3/Learning Activity Sheets/PDF/SHS_GM_Q3_LAS_LE9.pdf",
|
| 519 |
+
},
|
| 520 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 3/Lesson Exemplars/PDF/SHS_GM_Q3_LE7.pdf": {
|
| 521 |
+
"subject": "General Mathematics",
|
| 522 |
+
"subjectId": "gen-math",
|
| 523 |
+
"type": "lesson_exemplar",
|
| 524 |
+
"content_domain": "general",
|
| 525 |
+
"quarter": 3,
|
| 526 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 3/Lesson Exemplars/PDF/SHS_GM_Q3_LE7.pdf",
|
| 527 |
+
},
|
| 528 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 3/Lesson Exemplars/PDF/SHS_GM_Q3_LE8.pdf": {
|
| 529 |
+
"subject": "General Mathematics",
|
| 530 |
+
"subjectId": "gen-math",
|
| 531 |
+
"type": "lesson_exemplar",
|
| 532 |
+
"content_domain": "general",
|
| 533 |
+
"quarter": 3,
|
| 534 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 3/Lesson Exemplars/PDF/SHS_GM_Q3_LE8.pdf",
|
| 535 |
+
},
|
| 536 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 3/Lesson Exemplars/PDF/SHS_GM_Q3_LE9.pdf": {
|
| 537 |
+
"subject": "General Mathematics",
|
| 538 |
+
"subjectId": "gen-math",
|
| 539 |
+
"type": "lesson_exemplar",
|
| 540 |
+
"content_domain": "general",
|
| 541 |
+
"quarter": 3,
|
| 542 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 3/Lesson Exemplars/PDF/SHS_GM_Q3_LE9.pdf",
|
| 543 |
+
},
|
| 544 |
+
# SSHS Learning Resources: General Mathematics Quarter 4
|
| 545 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 4/Learning Activity Sheets/PDF/SHS_GM_Q4_LAS_LE10.pdf": {
|
| 546 |
+
"subject": "General Mathematics",
|
| 547 |
+
"subjectId": "gen-math",
|
| 548 |
+
"type": "learning_activity_sheet",
|
| 549 |
+
"content_domain": "general",
|
| 550 |
+
"quarter": 4,
|
| 551 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 4/Learning Activity Sheets/PDF/SHS_GM_Q4_LAS_LE10.pdf",
|
| 552 |
+
},
|
| 553 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 4/Learning Activity Sheets/PDF/SHS_GM_Q4_LAS_LE11.pdf": {
|
| 554 |
+
"subject": "General Mathematics",
|
| 555 |
+
"subjectId": "gen-math",
|
| 556 |
+
"type": "learning_activity_sheet",
|
| 557 |
+
"content_domain": "general",
|
| 558 |
+
"quarter": 4,
|
| 559 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 4/Learning Activity Sheets/PDF/SHS_GM_Q4_LAS_LE11.pdf",
|
| 560 |
+
},
|
| 561 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 4/Learning Activity Sheets/PDF/SHS_GM_Q4_LAS_LE12.pdf": {
|
| 562 |
+
"subject": "General Mathematics",
|
| 563 |
+
"subjectId": "gen-math",
|
| 564 |
+
"type": "learning_activity_sheet",
|
| 565 |
+
"content_domain": "general",
|
| 566 |
+
"quarter": 4,
|
| 567 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 4/Learning Activity Sheets/PDF/SHS_GM_Q4_LAS_LE12.pdf",
|
| 568 |
+
},
|
| 569 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 4/Lesson Exemplars/PDF/SHS_GM_Q4_LE10.pdf": {
|
| 570 |
+
"subject": "General Mathematics",
|
| 571 |
+
"subjectId": "gen-math",
|
| 572 |
+
"type": "lesson_exemplar",
|
| 573 |
+
"content_domain": "general",
|
| 574 |
+
"quarter": 4,
|
| 575 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 4/Lesson Exemplars/PDF/SHS_GM_Q4_LE10.pdf",
|
| 576 |
+
},
|
| 577 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 4/Lesson Exemplars/PDF/SHS_GM_Q4_LE11.pdf": {
|
| 578 |
+
"subject": "General Mathematics",
|
| 579 |
+
"subjectId": "gen-math",
|
| 580 |
+
"type": "lesson_exemplar",
|
| 581 |
+
"content_domain": "general",
|
| 582 |
+
"quarter": 4,
|
| 583 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 4/Lesson Exemplars/PDF/SHS_GM_Q4_LE11.pdf",
|
| 584 |
+
},
|
| 585 |
+
"curriculum/sshs_learning_resources/General Mathematics/Quarter 4/Lesson Exemplars/PDF/SHS_GM_Q4_LE12.pdf": {
|
| 586 |
+
"subject": "General Mathematics",
|
| 587 |
+
"subjectId": "gen-math",
|
| 588 |
+
"type": "lesson_exemplar",
|
| 589 |
+
"content_domain": "general",
|
| 590 |
+
"quarter": 4,
|
| 591 |
+
"storage_path": "curriculum/sshs_learning_resources/General Mathematics/Quarter 4/Lesson Exemplars/PDF/SHS_GM_Q4_LE12.pdf",
|
| 592 |
+
},
|
| 593 |
}
|
test_retrieval.py
CHANGED
|
@@ -1,39 +1,165 @@
|
|
| 1 |
-
import sys
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
| 4 |
+
|
| 5 |
+
import pytest
|
| 6 |
+
from rag.curriculum_rag import retrieve_lesson_pdf_context, retrieve_curriculum_context
|
| 7 |
+
from rag.firebase_storage_loader import infer_storage_metadata, PDF_METADATA
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test_infer_storage_metadata_sshs_curriculum():
|
| 11 |
+
"""Verify metadata inference parses subject, quarter (1-4), and resource types."""
|
| 12 |
+
# LAS
|
| 13 |
+
las_meta = infer_storage_metadata("curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Learning Activity Sheets/PDF/SHS_GM_Q1_LAS1.pdf")
|
| 14 |
+
assert las_meta["subject"] == "General Mathematics"
|
| 15 |
+
assert las_meta["quarter"] == 1
|
| 16 |
+
assert las_meta["type"] == "learning_activity_sheet"
|
| 17 |
+
|
| 18 |
+
# LE
|
| 19 |
+
le_meta = infer_storage_metadata("curriculum/sshs_learning_resources/General Mathematics/Quarter 2/Lesson Exemplars/PDF/SHS_GM_Q2_LE4.pdf")
|
| 20 |
+
assert le_meta["subject"] == "General Mathematics"
|
| 21 |
+
assert le_meta["quarter"] == 2
|
| 22 |
+
assert le_meta["type"] == "lesson_exemplar"
|
| 23 |
+
|
| 24 |
+
# Curriculum Guide
|
| 25 |
+
guide_meta = infer_storage_metadata("curriculum/sshs_learning_resources/General Mathematics/Curriculum & Budget of Work/PDF/GENERAL-MATHEMATICS-1.pdf")
|
| 26 |
+
assert guide_meta["subject"] == "General Mathematics"
|
| 27 |
+
assert guide_meta["type"] == "curriculum_guide"
|
| 28 |
+
|
| 29 |
+
# SDO Module
|
| 30 |
+
sdo_meta = infer_storage_metadata("curriculum/general_math/genmath_q2_mod1_simpleandcompoundinterests_v2.pdf")
|
| 31 |
+
assert sdo_meta["subject"] == "General Mathematics"
|
| 32 |
+
assert sdo_meta["quarter"] == 2
|
| 33 |
+
assert sdo_meta["type"] == "sdo_module"
|
| 34 |
+
|
| 35 |
+
# Stat Prob
|
| 36 |
+
stat_meta = infer_storage_metadata("curriculum/stat_prob/Full.pdf")
|
| 37 |
+
assert stat_meta["subject"] == "Statistics and Probability"
|
| 38 |
+
assert stat_meta["type"] == "sdo_module"
|
| 39 |
+
|
| 40 |
+
# Finite Math 1 & 2
|
| 41 |
+
fm1_meta = infer_storage_metadata("curriculum/sshs_learning_resources/Finite Mathematics/Finite Math 1/PDF/Finite Math 1_LAS.pdf")
|
| 42 |
+
assert "Finite Mathematics" in fm1_meta["subject"]
|
| 43 |
+
assert fm1_meta["quarter"] == 1
|
| 44 |
+
assert fm1_meta["type"] == "learning_activity_sheet"
|
| 45 |
+
|
| 46 |
+
fm2_meta = infer_storage_metadata("curriculum/sshs_learning_resources/Finite Mathematics/Finite Math 2/PDF/Finite Math 2_LE.pdf")
|
| 47 |
+
assert "Finite Mathematics" in fm2_meta["subject"]
|
| 48 |
+
assert fm2_meta["quarter"] == 2
|
| 49 |
+
assert fm2_meta["type"] == "lesson_exemplar"
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def test_exact_match_retrieval_full_path():
|
| 53 |
+
"""Exact match retrieval with full storage path."""
|
| 54 |
+
full_path = "curriculum/sshs_learning_resources/General Mathematics/Quarter 1/Learning Activity Sheets/PDF/SHS_GM_Q1_LAS1.pdf"
|
| 55 |
+
chunks, mode = retrieve_lesson_pdf_context(
|
| 56 |
+
topic="functions and relations piecewise functions",
|
| 57 |
+
subject="General Mathematics",
|
| 58 |
+
quarter=1,
|
| 59 |
+
storage_path=full_path,
|
| 60 |
+
top_k=5,
|
| 61 |
+
)
|
| 62 |
+
assert len(chunks) > 0
|
| 63 |
+
assert mode in ("exact", "hybrid")
|
| 64 |
+
for chunk in chunks:
|
| 65 |
+
assert chunk["storage_path"]
|
| 66 |
+
assert chunk["source_file"]
|
| 67 |
+
assert chunk["page"] >= 1
|
| 68 |
+
assert chunk["score"] > 0.0
|
| 69 |
+
assert chunk["content"]
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_exact_match_retrieval_relative_path():
|
| 73 |
+
"""Exact match retrieval with relative storage path."""
|
| 74 |
+
rel_path = "sshs_learning_resources/General Mathematics/Quarter 1/Learning Activity Sheets/PDF/SHS_GM_Q1_LAS1.pdf"
|
| 75 |
+
chunks, mode = retrieve_lesson_pdf_context(
|
| 76 |
+
topic="functions and relations piecewise functions",
|
| 77 |
+
subject="General Mathematics",
|
| 78 |
+
quarter=1,
|
| 79 |
+
storage_path=rel_path,
|
| 80 |
+
top_k=5,
|
| 81 |
+
)
|
| 82 |
+
assert len(chunks) > 0
|
| 83 |
+
assert mode in ("exact", "hybrid")
|
| 84 |
+
for chunk in chunks:
|
| 85 |
+
assert chunk["storage_path"]
|
| 86 |
+
assert chunk["source_file"]
|
| 87 |
+
assert chunk["page"] >= 1
|
| 88 |
+
assert chunk["score"] > 0.0
|
| 89 |
+
assert chunk["content"]
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def test_exact_match_retrieval_filename():
|
| 93 |
+
"""Exact match retrieval with filename only."""
|
| 94 |
+
filename = "SHS_GM_Q1_LAS1.pdf"
|
| 95 |
+
chunks, mode = retrieve_lesson_pdf_context(
|
| 96 |
+
topic="functions and relations piecewise functions",
|
| 97 |
+
subject="General Mathematics",
|
| 98 |
+
quarter=1,
|
| 99 |
+
storage_path=filename,
|
| 100 |
+
top_k=5,
|
| 101 |
+
)
|
| 102 |
+
assert len(chunks) > 0
|
| 103 |
+
assert mode in ("exact", "hybrid")
|
| 104 |
+
for chunk in chunks:
|
| 105 |
+
assert chunk["storage_path"]
|
| 106 |
+
assert chunk["source_file"]
|
| 107 |
+
assert chunk["page"] >= 1
|
| 108 |
+
assert chunk["score"] > 0.0
|
| 109 |
+
assert chunk["content"]
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def test_exact_match_retrieval_with_quarter_fallback():
|
| 113 |
+
"""Exact match fallback when chunk in vectorstore has quarter 0 or 1."""
|
| 114 |
+
chunks, mode = retrieve_lesson_pdf_context(
|
| 115 |
+
topic="matrices system of linear equations",
|
| 116 |
+
subject="Finite Mathematics",
|
| 117 |
+
quarter=2,
|
| 118 |
+
storage_path="Finite Math 2_LAS.pdf",
|
| 119 |
+
top_k=5,
|
| 120 |
+
)
|
| 121 |
+
assert len(chunks) > 0
|
| 122 |
+
assert mode in ("exact", "hybrid")
|
| 123 |
+
for chunk in chunks:
|
| 124 |
+
assert chunk["storage_path"]
|
| 125 |
+
assert chunk["source_file"]
|
| 126 |
+
assert chunk["page"] >= 1
|
| 127 |
+
assert chunk["score"] > 0.0
|
| 128 |
+
assert chunk["content"]
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
# Test retrieval with the same params as the frontend
|
| 133 |
+
try:
|
| 134 |
+
chunks, mode = retrieve_lesson_pdf_context(
|
| 135 |
+
topic="Represent real-life relationships as functions and interpret domain/range.",
|
| 136 |
+
subject="General Mathematics",
|
| 137 |
+
quarter=2,
|
| 138 |
+
lesson_title="Represent real-life relationships as functions and interpret domain/range.",
|
| 139 |
+
module_id="gen-math",
|
| 140 |
+
lesson_id="gm-q2-functions-graphs-l1",
|
| 141 |
+
competency_code="GM11-FG-1",
|
| 142 |
+
top_k=8,
|
| 143 |
+
)
|
| 144 |
+
print(f"Retrieved {len(chunks)} chunks, mode={mode}")
|
| 145 |
+
for i, chunk in enumerate(chunks[:3]):
|
| 146 |
+
print(f" Chunk {i}: score={chunk.get('score')}, domain={chunk.get('content_domain')}, source={chunk.get('source_file')}")
|
| 147 |
+
print(f" Content: {chunk.get('content', '')[:100]}...")
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"ERROR: {type(e).__name__}: {e}")
|
| 150 |
+
import traceback
|
| 151 |
+
traceback.print_exc()
|
| 152 |
+
|
| 153 |
+
# Also test without module/lesson filters
|
| 154 |
+
try:
|
| 155 |
+
chunks2 = retrieve_curriculum_context(
|
| 156 |
+
query="Represent real-life relationships as functions and interpret domain/range.",
|
| 157 |
+
subject="General Mathematics",
|
| 158 |
+
quarter=2,
|
| 159 |
+
top_k=8,
|
| 160 |
+
)
|
| 161 |
+
print(f"\nGeneral retrieval: {len(chunks2)} chunks")
|
| 162 |
+
except Exception as e:
|
| 163 |
+
print(f"\nGeneral ERROR: {type(e).__name__}: {e}")
|
| 164 |
+
import traceback
|
| 165 |
+
traceback.print_exc()
|