SarahXia0405 commited on
Commit
1d01b56
·
verified ·
1 Parent(s): fe9dde2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -6
app.py CHANGED
@@ -330,6 +330,48 @@ def format_references(
330
  return "\n".join(lines)
331
 
332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
  # ================== Gradio App ==================
334
  with gr.Blocks(
335
  title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
@@ -830,9 +872,14 @@ with gr.Blocks(
830
  cognitive_state = update_cognitive_state_from_message(message, cognitive_state)
831
 
832
  # RAG 始终基于 (预加载 Module10 + 可选上传)
833
- rag_context_text, rag_used_chunks = retrieve_relevant_chunks(
834
- message, rag_chunks or []
835
- )
 
 
 
 
 
836
 
837
  # 计时
838
  start_ts = time.time()
@@ -851,15 +898,18 @@ with gr.Blocks(
851
  end_ts = time.time()
852
  latency_ms = (end_ts - start_ts) * 1000.0
853
 
854
- # === 在这里附上 References ===
855
- ref_text = format_references(rag_used_chunks)
 
 
 
856
  if ref_text and new_history:
857
  last_user, last_assistant = new_history[-1]
858
  if "References (RAG context used):" not in (last_assistant or ""):
859
  last_assistant = f"{last_assistant}\n\n{ref_text}"
860
  new_history[-1] = [last_user, last_assistant]
861
  answer = last_assistant # 同步给日志
862
- # ==============================
863
 
864
  # 日志
865
  student_id = user_id_val or "ANON"
 
330
  return "\n".join(lines)
331
 
332
 
333
+
334
+ def is_academic_query(message: str) -> bool:
335
+ """
336
+ 粗略判断:这次学生输入是否是“学术相关 / 课程相关”问题。
337
+ 目的:避免像 "hi"、"thanks" 这种闲聊也去查 RAG + 打 reference。
338
+ """
339
+ if not message:
340
+ return False
341
+
342
+ m = message.strip().lower()
343
+ if not m:
344
+ return False
345
+
346
+ # 典型闲聊词
347
+ smalltalk_tokens = {
348
+ "hi", "hello", "hey", "yo",
349
+ "thanks", "thank", "thank you",
350
+ "ok", "okay",
351
+ "bye", "goodbye", "see you",
352
+ "haha", "lol"
353
+ }
354
+
355
+ tokens = m.split()
356
+
357
+ # 如果全是闲聊词而且没有问号 → 视为非学术问题
358
+ if "?" not in m and all(t in smalltalk_tokens for t in tokens):
359
+ return False
360
+
361
+ # 很短、又没有问号的输入,大概率也不是严肃学术问题
362
+ if len(tokens) <= 2 and "?" not in m:
363
+ return False
364
+
365
+ # 其他情况默认当作学术/课程相关问题
366
+ return True
367
+
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
  # ================== Gradio App ==================
376
  with gr.Blocks(
377
  title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
 
872
  cognitive_state = update_cognitive_state_from_message(message, cognitive_state)
873
 
874
  # RAG 始终基于 (预加载 Module10 + 可选上传)
875
+ # 只对“学术/课程相关”问题调用 RAG;闲聊不查文档,也不打 reference
876
+ if is_academic_query(message):
877
+ rag_context_text, rag_used_chunks = retrieve_relevant_chunks(
878
+ message, rag_chunks or []
879
+ )
880
+ else:
881
+ rag_context_text, rag_used_chunks = "", []
882
+
883
 
884
  # 计时
885
  start_ts = time.time()
 
898
  end_ts = time.time()
899
  latency_ms = (end_ts - start_ts) * 1000.0
900
 
901
+ # === 在这里附上 References(只在学术问题上附) ===
902
+ ref_text = ""
903
+ if is_academic_query(message) and rag_used_chunks:
904
+ ref_text = format_references(rag_used_chunks)
905
+
906
  if ref_text and new_history:
907
  last_user, last_assistant = new_history[-1]
908
  if "References (RAG context used):" not in (last_assistant or ""):
909
  last_assistant = f"{last_assistant}\n\n{ref_text}"
910
  new_history[-1] = [last_user, last_assistant]
911
  answer = last_assistant # 同步给日志
912
+ # ============================================
913
 
914
  # 日志
915
  student_id = user_id_val or "ANON"