Heng2004 commited on
Commit
b781c31
Β·
verified Β·
1 Parent(s): 405e720

Update model_utils.py

Browse files
Files changed (1) hide show
  1. model_utils.py +48 -1
model_utils.py CHANGED
@@ -15,7 +15,10 @@ from loader import (
15
  load_manual_qa,
16
  rebuild_combined_qa,
17
  load_glossary,
18
- sync_download_manual_qa # <--- Import it
 
 
 
19
  )
20
 
21
  # -----------------------------
@@ -46,6 +49,49 @@ MAX_CONTEXT_ENTRIES = 4
46
  # -----------------------------
47
  # Embedding builders
48
  # -----------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  def _build_entry_embeddings() -> None:
50
  """
51
  Load pre-computed embeddings if available, otherwise build them.
@@ -113,6 +159,7 @@ def _build_glossary_embeddings() -> None:
113
  # Load data once at import time
114
  # -----------------------------
115
  sync_download_manual_qa()
 
116
  load_curriculum()
117
  load_manual_qa()
118
  load_glossary()
 
15
  load_manual_qa,
16
  rebuild_combined_qa,
17
  load_glossary,
18
+ sync_download_manual_qa, # <--- Import it
19
+ sync_download_cache, # <--- Add this import
20
+ sync_upload_cache, # <--- Add this import
21
+ CACHE_PATH # <--- Add this import
22
  )
23
 
24
  # -----------------------------
 
49
  # -----------------------------
50
  # Embedding builders
51
  # -----------------------------
52
+
53
+ # πŸ‘‡πŸ‘‡πŸ‘‡ ADD THIS NEW FUNCTION πŸ‘‡πŸ‘‡πŸ‘‡
54
+ def admin_force_rebuild_cache() -> str:
55
+ """
56
+ Forcedly re-calculate all embeddings and upload to cloud.
57
+ Triggered by Teacher Panel button.
58
+ """
59
+ status_msg = []
60
+
61
+ # 1. Compute Textbook
62
+ print("[ADMIN] Rebuilding Textbook Embeddings...")
63
+ texts = []
64
+ for e in qa_store.ENTRIES:
65
+ chapter = e.get("chapter_title", "") or ""
66
+ section = e.get("section_title", "") or ""
67
+ text = e.get("text", "") or ""
68
+ texts.append(f"{chapter}\n{section}\n{text}")
69
+
70
+ if texts:
71
+ qa_store.TEXT_EMBEDDINGS = embed_model.encode(texts, convert_to_tensor=True)
72
+ status_msg.append(f"βœ… Textbook ({len(texts)})")
73
+
74
+ # 2. Compute Glossary
75
+ print("[ADMIN] Rebuilding Glossary Embeddings...")
76
+ gloss_texts = [f"{i.get('term')} :: {i.get('definition')}" for i in qa_store.GLOSSARY]
77
+
78
+ if gloss_texts:
79
+ qa_store.GLOSSARY_EMBEDDINGS = embed_model.encode(
80
+ gloss_texts, convert_to_numpy=True, normalize_embeddings=True
81
+ )
82
+ status_msg.append(f"βœ… Glossary ({len(gloss_texts)})")
83
+
84
+ # 3. Save to Disk
85
+ print("[ADMIN] Saving to disk...")
86
+ torch.save({
87
+ "textbook": qa_store.TEXT_EMBEDDINGS,
88
+ "glossary": qa_store.GLOSSARY_EMBEDDINGS
89
+ }, CACHE_PATH)
90
+
91
+ # 4. Upload to Cloud
92
+ upload_status = sync_upload_cache()
93
+ return f"Rebuild Complete: {', '.join(status_msg)} | {upload_status}"
94
+
95
  def _build_entry_embeddings() -> None:
96
  """
97
  Load pre-computed embeddings if available, otherwise build them.
 
159
  # Load data once at import time
160
  # -----------------------------
161
  sync_download_manual_qa()
162
+ sync_download_cache() # <--- Add this line!
163
  load_curriculum()
164
  load_manual_qa()
165
  load_glossary()