Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,30 +22,10 @@ from clare_core import (
|
|
| 22 |
get_empty_input_prompt,
|
| 23 |
summarize_conversation,
|
| 24 |
)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
course_outline,
|
| 30 |
-
weaknesses,
|
| 31 |
-
cognitive_state,
|
| 32 |
-
model_name_val,
|
| 33 |
-
language_pref_val,
|
| 34 |
-
learning_mode_val,
|
| 35 |
-
doc_type_val,
|
| 36 |
-
):
|
| 37 |
-
# 先根据 Auto / English / 中文 决定本轮用什么语言
|
| 38 |
-
resolved_lang = detect_language(message or "", language_pref_val)
|
| 39 |
-
|
| 40 |
-
# ---------- 空输入防护 ----------
|
| 41 |
-
if not message or not message.strip():
|
| 42 |
-
# 不更新弱项和认知状态,只给一个友好提示
|
| 43 |
-
empty_msg = get_empty_input_prompt(resolved_lang)
|
| 44 |
-
new_history = chat_history + [("", empty_msg)]
|
| 45 |
-
status_text = render_session_status(learning_mode_val, weaknesses or [], cognitive_state)
|
| 46 |
-
# 清空输入框,更新聊天记录 & 状态栏,其余状态不变
|
| 47 |
-
return "", new_history, weaknesses, cognitive_state, status_text
|
| 48 |
-
|
| 49 |
|
| 50 |
with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
| 51 |
gr.Markdown(
|
|
@@ -90,29 +70,39 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 90 |
label="File type",
|
| 91 |
)
|
| 92 |
|
| 93 |
-
# 状态:课程大纲 + 学生弱项 + 认知状态
|
| 94 |
course_outline_state = gr.State(DEFAULT_COURSE_TOPICS)
|
| 95 |
weakness_state = gr.State([])
|
| 96 |
cognitive_state_state = gr.State({"confusion": 0, "mastery": 0})
|
|
|
|
| 97 |
|
| 98 |
-
# 上传 syllabus 时更新课程大纲
|
| 99 |
-
def
|
|
|
|
| 100 |
if file is None:
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
syllabus_file.change(
|
| 113 |
-
fn=
|
| 114 |
inputs=[syllabus_file, doc_type],
|
| 115 |
-
outputs=[course_outline_state],
|
| 116 |
)
|
| 117 |
|
| 118 |
# 左侧聊天,右侧 Session 状态栏
|
|
@@ -157,19 +147,27 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 157 |
course_outline,
|
| 158 |
weaknesses,
|
| 159 |
cognitive_state,
|
|
|
|
| 160 |
model_name_val,
|
| 161 |
language_pref_val,
|
| 162 |
learning_mode_val,
|
| 163 |
doc_type_val,
|
| 164 |
):
|
| 165 |
# 1) 决定本轮语言(Auto / English / 中文)
|
| 166 |
-
resolved_lang = detect_language(message, language_pref_val)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
#
|
| 169 |
weaknesses = update_weaknesses_from_message(message, weaknesses or [])
|
| 170 |
cognitive_state = update_cognitive_state_from_message(message, cognitive_state)
|
| 171 |
|
| 172 |
-
#
|
| 173 |
dup = find_similar_past_question(message, chat_history)
|
| 174 |
if dup is not None:
|
| 175 |
past_q, past_a, sim = dup
|
|
@@ -195,7 +193,10 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 195 |
status_text = render_session_status(learning_mode_val, weaknesses, cognitive_state)
|
| 196 |
return "", new_history, weaknesses, cognitive_state, status_text
|
| 197 |
|
| 198 |
-
#
|
|
|
|
|
|
|
|
|
|
| 199 |
answer, new_history = chat_with_clare(
|
| 200 |
message=message,
|
| 201 |
history=chat_history,
|
|
@@ -206,6 +207,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 206 |
course_outline=course_outline,
|
| 207 |
weaknesses=weaknesses,
|
| 208 |
cognitive_state=cognitive_state,
|
|
|
|
| 209 |
)
|
| 210 |
|
| 211 |
status_text = render_session_status(learning_mode_val, weaknesses, cognitive_state)
|
|
@@ -219,6 +221,7 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 219 |
course_outline_state,
|
| 220 |
weakness_state,
|
| 221 |
cognitive_state_state,
|
|
|
|
| 222 |
model_name,
|
| 223 |
language_preference,
|
| 224 |
learning_mode,
|
|
@@ -231,12 +234,30 @@ with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
|
| 231 |
def clear_all():
|
| 232 |
empty_state = {"confusion": 0, "mastery": 0}
|
| 233 |
status_text = render_session_status("Concept Explainer", [], empty_state)
|
| 234 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
clear_btn.click(
|
| 237 |
clear_all,
|
| 238 |
None,
|
| 239 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
queue=False,
|
| 241 |
)
|
| 242 |
|
|
|
|
| 22 |
get_empty_input_prompt,
|
| 23 |
summarize_conversation,
|
| 24 |
)
|
| 25 |
+
from rag_engine import (
|
| 26 |
+
build_rag_chunks_from_file,
|
| 27 |
+
retrieve_relevant_chunks,
|
| 28 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
with gr.Blocks(title="Clare – Hanbridge AI Teaching Assistant") as demo:
|
| 31 |
gr.Markdown(
|
|
|
|
| 70 |
label="File type",
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# 状态:课程大纲 + 学生弱项 + 认知状态 + RAG chunks
|
| 74 |
course_outline_state = gr.State(DEFAULT_COURSE_TOPICS)
|
| 75 |
weakness_state = gr.State([])
|
| 76 |
cognitive_state_state = gr.State({"confusion": 0, "mastery": 0})
|
| 77 |
+
rag_chunks_state = gr.State([]) # Session 级 RAG 向量库
|
| 78 |
|
| 79 |
+
# 上传 syllabus 时更新课程大纲 + RAG chunks
|
| 80 |
+
def update_course_and_rag(file, doc_type_val):
|
| 81 |
+
# 更新课程大纲(保持原有逻辑)
|
| 82 |
if file is None:
|
| 83 |
+
topics = DEFAULT_COURSE_TOPICS
|
| 84 |
+
else:
|
| 85 |
+
if doc_type_val == "Syllabus":
|
| 86 |
+
try:
|
| 87 |
+
file_path = file.name
|
| 88 |
+
if file_path.lower().endswith(".docx"):
|
| 89 |
+
topics = parse_syllabus_docx(file_path)
|
| 90 |
+
else:
|
| 91 |
+
topics = DEFAULT_COURSE_TOPICS
|
| 92 |
+
except Exception:
|
| 93 |
+
topics = DEFAULT_COURSE_TOPICS
|
| 94 |
+
else:
|
| 95 |
+
topics = DEFAULT_COURSE_TOPICS
|
| 96 |
+
|
| 97 |
+
# 构建 RAG chunks
|
| 98 |
+
rag_chunks = build_rag_chunks_from_file(file, doc_type_val)
|
| 99 |
+
|
| 100 |
+
return topics, rag_chunks
|
| 101 |
|
| 102 |
syllabus_file.change(
|
| 103 |
+
fn=update_course_and_rag,
|
| 104 |
inputs=[syllabus_file, doc_type],
|
| 105 |
+
outputs=[course_outline_state, rag_chunks_state],
|
| 106 |
)
|
| 107 |
|
| 108 |
# 左侧聊天,右侧 Session 状态栏
|
|
|
|
| 147 |
course_outline,
|
| 148 |
weaknesses,
|
| 149 |
cognitive_state,
|
| 150 |
+
rag_chunks,
|
| 151 |
model_name_val,
|
| 152 |
language_pref_val,
|
| 153 |
learning_mode_val,
|
| 154 |
doc_type_val,
|
| 155 |
):
|
| 156 |
# 1) 决定本轮语言(Auto / English / 中文)
|
| 157 |
+
resolved_lang = detect_language(message or "", language_pref_val)
|
| 158 |
+
|
| 159 |
+
# 2) 空输入防护
|
| 160 |
+
if not message or not message.strip():
|
| 161 |
+
empty_msg = get_empty_input_prompt(resolved_lang)
|
| 162 |
+
new_history = chat_history + [("", empty_msg)]
|
| 163 |
+
status_text = render_session_status(learning_mode_val, weaknesses or [], cognitive_state)
|
| 164 |
+
return "", new_history, weaknesses, cognitive_state, status_text
|
| 165 |
|
| 166 |
+
# 3) 更新弱项 & 认知状态
|
| 167 |
weaknesses = update_weaknesses_from_message(message, weaknesses or [])
|
| 168 |
cognitive_state = update_cognitive_state_from_message(message, cognitive_state)
|
| 169 |
|
| 170 |
+
# 4) Same Question Check(session 内复用答案)
|
| 171 |
dup = find_similar_past_question(message, chat_history)
|
| 172 |
if dup is not None:
|
| 173 |
past_q, past_a, sim = dup
|
|
|
|
| 193 |
status_text = render_session_status(learning_mode_val, weaknesses, cognitive_state)
|
| 194 |
return "", new_history, weaknesses, cognitive_state, status_text
|
| 195 |
|
| 196 |
+
# 5) RAG:基于上传文档做检索
|
| 197 |
+
rag_context = retrieve_relevant_chunks(message, rag_chunks or [])
|
| 198 |
+
|
| 199 |
+
# 6) 正常调用 Clare(带上 RAG context)
|
| 200 |
answer, new_history = chat_with_clare(
|
| 201 |
message=message,
|
| 202 |
history=chat_history,
|
|
|
|
| 207 |
course_outline=course_outline,
|
| 208 |
weaknesses=weaknesses,
|
| 209 |
cognitive_state=cognitive_state,
|
| 210 |
+
rag_context=rag_context,
|
| 211 |
)
|
| 212 |
|
| 213 |
status_text = render_session_status(learning_mode_val, weaknesses, cognitive_state)
|
|
|
|
| 221 |
course_outline_state,
|
| 222 |
weakness_state,
|
| 223 |
cognitive_state_state,
|
| 224 |
+
rag_chunks_state,
|
| 225 |
model_name,
|
| 226 |
language_preference,
|
| 227 |
learning_mode,
|
|
|
|
| 234 |
def clear_all():
|
| 235 |
empty_state = {"confusion": 0, "mastery": 0}
|
| 236 |
status_text = render_session_status("Concept Explainer", [], empty_state)
|
| 237 |
+
return (
|
| 238 |
+
[], # chatbot
|
| 239 |
+
[], # weaknesses
|
| 240 |
+
empty_state, # cognitive_state
|
| 241 |
+
[], # rag_chunks
|
| 242 |
+
"", # export_box
|
| 243 |
+
"", # quiz_box
|
| 244 |
+
"", # summary_box
|
| 245 |
+
status_text, # session_status
|
| 246 |
+
)
|
| 247 |
|
| 248 |
clear_btn.click(
|
| 249 |
clear_all,
|
| 250 |
None,
|
| 251 |
+
[
|
| 252 |
+
chatbot,
|
| 253 |
+
weakness_state,
|
| 254 |
+
cognitive_state_state,
|
| 255 |
+
rag_chunks_state,
|
| 256 |
+
export_box,
|
| 257 |
+
quiz_box,
|
| 258 |
+
summary_box,
|
| 259 |
+
session_status,
|
| 260 |
+
],
|
| 261 |
queue=False,
|
| 262 |
)
|
| 263 |
|