SarahXia0405 commited on
Commit
99022f7
·
verified ·
1 Parent(s): 0e14d58

Update api/server.py

Browse files
Files changed (1) hide show
  1. api/server.py +25 -0
api/server.py CHANGED
@@ -102,6 +102,8 @@ def _get_session(user_id: str) -> Dict:
102
  # preload base reading
103
  "rag_chunks": list(MODULE10_CHUNKS_CACHE),
104
  "model_name": DEFAULT_MODEL,
 
 
105
  }
106
  return SESSIONS[user_id]
107
 
@@ -202,6 +204,25 @@ def chat(req: ChatReq):
202
  for c in (rag_used_chunks or [])
203
  ]
204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  return {
206
  "reply": answer,
207
  "session_status_md": render_session_status(
@@ -236,6 +257,10 @@ async def upload(
236
  with open(tmp_path, "wb") as f:
237
  f.write(content)
238
 
 
 
 
 
239
  # Update topics only for syllabus
240
  if doc_type == "Syllabus":
241
  class _F:
 
102
  # preload base reading
103
  "rag_chunks": list(MODULE10_CHUNKS_CACHE),
104
  "model_name": DEFAULT_MODEL,
105
+ # ✅ NEW: track last syllabus filename for refs fallback
106
+ "last_syllabus_file": None,
107
  }
108
  return SESSIONS[user_id]
109
 
 
204
  for c in (rag_used_chunks or [])
205
  ]
206
 
207
+ # ✅ NEW: Syllabus chat should show syllabus in refs (fallback if retrieval didn't hit it)
208
+ doc_type_norm = (req.doc_type or "").strip().lower()
209
+ if doc_type_norm == "syllabus":
210
+ syllabus_name = sess.get("last_syllabus_file") or "uploaded_syllabus"
211
+ # We consider it a hit if any ref source_file contains the syllabus filename
212
+ def _has_syllabus_ref() -> bool:
213
+ for r in refs:
214
+ sf = (r.get("source_file") or "").strip()
215
+ if not sf:
216
+ continue
217
+ # robust matching: exact basename containment OR endswith
218
+ if syllabus_name in sf or sf.endswith(syllabus_name):
219
+ return True
220
+ return False
221
+
222
+ if not _has_syllabus_ref():
223
+ # put syllabus at the front, preserve existing refs after
224
+ refs = [{"source_file": syllabus_name, "section": "syllabus_outline"}] + refs
225
+
226
  return {
227
  "reply": answer,
228
  "session_status_md": render_session_status(
 
257
  with open(tmp_path, "wb") as f:
258
  f.write(content)
259
 
260
+ # ✅ NEW: remember the latest syllabus filename for refs fallback
261
+ if doc_type == "Syllabus":
262
+ sess["last_syllabus_file"] = os.path.basename(file.filename) or safe_name
263
+
264
  # Update topics only for syllabus
265
  if doc_type == "Syllabus":
266
  class _F: