Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -328,7 +328,6 @@ def format_references(
|
|
| 328 |
def is_academic_query(message: str) -> bool:
|
| 329 |
"""
|
| 330 |
判断当前学生输入是否是“学术/课程相关”问题。
|
| 331 |
-
目的:避免像 hi、thanks、who are you 这类寒暄或自我介绍请求也触发 RAG 和 reference。
|
| 332 |
"""
|
| 333 |
if not message:
|
| 334 |
return False
|
|
@@ -390,7 +389,7 @@ with gr.Blocks(
|
|
| 390 |
cognitive_state_state = gr.State({"confusion": 0, "mastery": 0})
|
| 391 |
rag_chunks_state = gr.State(preloaded_chunks or [])
|
| 392 |
|
| 393 |
-
#
|
| 394 |
last_question_state = gr.State("")
|
| 395 |
last_answer_state = gr.State("")
|
| 396 |
|
|
@@ -540,20 +539,15 @@ with gr.Blocks(
|
|
| 540 |
height=450,
|
| 541 |
avatar_images=(None, CLARE_LOGO_PATH),
|
| 542 |
show_label=False,
|
| 543 |
-
bubble_full_width=False,
|
| 544 |
-
type="
|
|
|
|
| 545 |
)
|
| 546 |
|
| 547 |
-
#
|
| 548 |
-
gr.Markdown(
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
"👍 Helpful", size="sm", interactive=False
|
| 552 |
-
)
|
| 553 |
-
thumb_down_btn = gr.Button(
|
| 554 |
-
"👎 Not helpful", size="sm", interactive=False
|
| 555 |
-
)
|
| 556 |
-
|
| 557 |
feedback_toggle_btn = gr.Button(
|
| 558 |
"Give detailed feedback", size="sm", variant="secondary", interactive=False
|
| 559 |
)
|
|
@@ -709,8 +703,6 @@ with gr.Blocks(
|
|
| 709 |
learning_mode: gr.update(interactive=False),
|
| 710 |
model_name: gr.update(interactive=False),
|
| 711 |
docs_btn: gr.update(interactive=False),
|
| 712 |
-
thumb_up_btn: gr.update(interactive=False),
|
| 713 |
-
thumb_down_btn: gr.update(interactive=False),
|
| 714 |
feedback_toggle_btn: gr.update(interactive=False),
|
| 715 |
feedback_text: gr.update(visible=False, value=""),
|
| 716 |
feedback_submit_btn: gr.update(interactive=False, visible=False),
|
|
@@ -744,8 +736,6 @@ with gr.Blocks(
|
|
| 744 |
learning_mode: gr.update(interactive=True),
|
| 745 |
model_name: gr.update(interactive=False),
|
| 746 |
docs_btn: gr.update(interactive=True),
|
| 747 |
-
thumb_up_btn: gr.update(interactive=True),
|
| 748 |
-
thumb_down_btn: gr.update(interactive=True),
|
| 749 |
feedback_toggle_btn: gr.update(interactive=True),
|
| 750 |
feedback_text: gr.update(visible=False, value=""),
|
| 751 |
feedback_submit_btn: gr.update(interactive=True, visible=False),
|
|
@@ -773,8 +763,6 @@ with gr.Blocks(
|
|
| 773 |
learning_mode,
|
| 774 |
model_name,
|
| 775 |
docs_btn,
|
| 776 |
-
thumb_up_btn,
|
| 777 |
-
thumb_down_btn,
|
| 778 |
feedback_toggle_btn,
|
| 779 |
feedback_text,
|
| 780 |
feedback_submit_btn,
|
|
@@ -806,8 +794,6 @@ with gr.Blocks(
|
|
| 806 |
language_preference: gr.update(interactive=False),
|
| 807 |
learning_mode: gr.update(interactive=False),
|
| 808 |
docs_btn: gr.update(interactive=False),
|
| 809 |
-
thumb_up_btn: gr.update(interactive=False),
|
| 810 |
-
thumb_down_btn: gr.update(interactive=False),
|
| 811 |
feedback_toggle_btn: gr.update(interactive=False),
|
| 812 |
feedback_text: gr.update(visible=False, value=""),
|
| 813 |
feedback_submit_btn: gr.update(interactive=False, visible=False),
|
|
@@ -835,8 +821,6 @@ with gr.Blocks(
|
|
| 835 |
language_preference,
|
| 836 |
learning_mode,
|
| 837 |
docs_btn,
|
| 838 |
-
thumb_up_btn,
|
| 839 |
-
thumb_down_btn,
|
| 840 |
feedback_toggle_btn,
|
| 841 |
feedback_text,
|
| 842 |
feedback_submit_btn,
|
|
@@ -993,7 +977,7 @@ with gr.Blocks(
|
|
| 993 |
|
| 994 |
new_status = render_session_status(mode_val, weaknesses, cognitive_state)
|
| 995 |
|
| 996 |
-
# 将当前这一轮的 Q/A 存入 state
|
| 997 |
return "", new_history, weaknesses, cognitive_state, new_status, message, answer
|
| 998 |
|
| 999 |
user_input.submit(
|
|
@@ -1154,76 +1138,6 @@ with gr.Blocks(
|
|
| 1154 |
[feedback_text, feedback_submit_btn],
|
| 1155 |
)
|
| 1156 |
|
| 1157 |
-
def send_thumb_up(last_q, last_a, user_id_val, mode_val, model_name_val, lang_pref):
|
| 1158 |
-
if not last_q and not last_a:
|
| 1159 |
-
print("No last QA to log for thumbs_up.")
|
| 1160 |
-
return
|
| 1161 |
-
try:
|
| 1162 |
-
log_event(
|
| 1163 |
-
{
|
| 1164 |
-
"experiment_id": "RESP_AI_W10",
|
| 1165 |
-
"student_id": user_id_val or "ANON",
|
| 1166 |
-
"event_type": "thumbs_up",
|
| 1167 |
-
"timestamp": time.time(),
|
| 1168 |
-
"question": last_q,
|
| 1169 |
-
"answer": last_a,
|
| 1170 |
-
"model_name": model_name_val,
|
| 1171 |
-
"language": lang_pref,
|
| 1172 |
-
"learning_mode": mode_val,
|
| 1173 |
-
}
|
| 1174 |
-
)
|
| 1175 |
-
print("[Feedback] thumbs_up logged.")
|
| 1176 |
-
except Exception as e:
|
| 1177 |
-
print("thumb_up log error:", e)
|
| 1178 |
-
|
| 1179 |
-
def send_thumb_down(last_q, last_a, user_id_val, mode_val, model_name_val, lang_pref):
|
| 1180 |
-
if not last_q and not last_a:
|
| 1181 |
-
print("No last QA to log for thumbs_down.")
|
| 1182 |
-
return
|
| 1183 |
-
try:
|
| 1184 |
-
log_event(
|
| 1185 |
-
{
|
| 1186 |
-
"experiment_id": "RESP_AI_W10",
|
| 1187 |
-
"student_id": user_id_val or "ANON",
|
| 1188 |
-
"event_type": "thumbs_down",
|
| 1189 |
-
"timestamp": time.time(),
|
| 1190 |
-
"question": last_q,
|
| 1191 |
-
"answer": last_a,
|
| 1192 |
-
"model_name": model_name_val,
|
| 1193 |
-
"language": lang_pref,
|
| 1194 |
-
"learning_mode": mode_val,
|
| 1195 |
-
}
|
| 1196 |
-
)
|
| 1197 |
-
print("[Feedback] thumbs_down logged.")
|
| 1198 |
-
except Exception as e:
|
| 1199 |
-
print("thumb_down log error:", e)
|
| 1200 |
-
|
| 1201 |
-
thumb_up_btn.click(
|
| 1202 |
-
send_thumb_up,
|
| 1203 |
-
[
|
| 1204 |
-
last_question_state,
|
| 1205 |
-
last_answer_state,
|
| 1206 |
-
user_id_state,
|
| 1207 |
-
learning_mode,
|
| 1208 |
-
model_name,
|
| 1209 |
-
language_preference,
|
| 1210 |
-
],
|
| 1211 |
-
[],
|
| 1212 |
-
)
|
| 1213 |
-
|
| 1214 |
-
thumb_down_btn.click(
|
| 1215 |
-
send_thumb_down,
|
| 1216 |
-
[
|
| 1217 |
-
last_question_state,
|
| 1218 |
-
last_answer_state,
|
| 1219 |
-
user_id_state,
|
| 1220 |
-
learning_mode,
|
| 1221 |
-
model_name,
|
| 1222 |
-
language_preference,
|
| 1223 |
-
],
|
| 1224 |
-
[],
|
| 1225 |
-
)
|
| 1226 |
-
|
| 1227 |
def submit_detailed_feedback(
|
| 1228 |
text, last_q, last_a, user_id_val, mode_val, model_name_val, lang_pref
|
| 1229 |
):
|
|
@@ -1273,6 +1187,54 @@ with gr.Blocks(
|
|
| 1273 |
[feedback_text],
|
| 1274 |
)
|
| 1275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1276 |
# ===== Export / Summary =====
|
| 1277 |
export_btn.click(
|
| 1278 |
lambda h, c, m, w, cog: export_conversation(h, c, m, w, cog),
|
|
@@ -1319,7 +1281,7 @@ with gr.Blocks(
|
|
| 1319 |
|
| 1320 |
if __name__ == "__main__":
|
| 1321 |
demo.launch(
|
| 1322 |
-
share=True, #
|
| 1323 |
server_name="0.0.0.0",
|
| 1324 |
server_port=7860,
|
| 1325 |
)
|
|
|
|
| 328 |
def is_academic_query(message: str) -> bool:
|
| 329 |
"""
|
| 330 |
判断当前学生输入是否是“学术/课程相关”问题。
|
|
|
|
| 331 |
"""
|
| 332 |
if not message:
|
| 333 |
return False
|
|
|
|
| 389 |
cognitive_state_state = gr.State({"confusion": 0, "mastery": 0})
|
| 390 |
rag_chunks_state = gr.State(preloaded_chunks or [])
|
| 391 |
|
| 392 |
+
# 最近一次回答(用于详细反馈)
|
| 393 |
last_question_state = gr.State("")
|
| 394 |
last_answer_state = gr.State("")
|
| 395 |
|
|
|
|
| 539 |
height=450,
|
| 540 |
avatar_images=(None, CLARE_LOGO_PATH),
|
| 541 |
show_label=False,
|
| 542 |
+
bubble_full_width=False,
|
| 543 |
+
type="tuples", # 明确使用 tuples 历史格式
|
| 544 |
+
likeable=True, # 每条消息下方显示 👍 / 👎
|
| 545 |
)
|
| 546 |
|
| 547 |
+
# 详细文字反馈(针对“最后一条回答”)
|
| 548 |
+
gr.Markdown(
|
| 549 |
+
"#### Optional: detailed feedback on Clare’s last answer"
|
| 550 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 551 |
feedback_toggle_btn = gr.Button(
|
| 552 |
"Give detailed feedback", size="sm", variant="secondary", interactive=False
|
| 553 |
)
|
|
|
|
| 703 |
learning_mode: gr.update(interactive=False),
|
| 704 |
model_name: gr.update(interactive=False),
|
| 705 |
docs_btn: gr.update(interactive=False),
|
|
|
|
|
|
|
| 706 |
feedback_toggle_btn: gr.update(interactive=False),
|
| 707 |
feedback_text: gr.update(visible=False, value=""),
|
| 708 |
feedback_submit_btn: gr.update(interactive=False, visible=False),
|
|
|
|
| 736 |
learning_mode: gr.update(interactive=True),
|
| 737 |
model_name: gr.update(interactive=False),
|
| 738 |
docs_btn: gr.update(interactive=True),
|
|
|
|
|
|
|
| 739 |
feedback_toggle_btn: gr.update(interactive=True),
|
| 740 |
feedback_text: gr.update(visible=False, value=""),
|
| 741 |
feedback_submit_btn: gr.update(interactive=True, visible=False),
|
|
|
|
| 763 |
learning_mode,
|
| 764 |
model_name,
|
| 765 |
docs_btn,
|
|
|
|
|
|
|
| 766 |
feedback_toggle_btn,
|
| 767 |
feedback_text,
|
| 768 |
feedback_submit_btn,
|
|
|
|
| 794 |
language_preference: gr.update(interactive=False),
|
| 795 |
learning_mode: gr.update(interactive=False),
|
| 796 |
docs_btn: gr.update(interactive=False),
|
|
|
|
|
|
|
| 797 |
feedback_toggle_btn: gr.update(interactive=False),
|
| 798 |
feedback_text: gr.update(visible=False, value=""),
|
| 799 |
feedback_submit_btn: gr.update(interactive=False, visible=False),
|
|
|
|
| 821 |
language_preference,
|
| 822 |
learning_mode,
|
| 823 |
docs_btn,
|
|
|
|
|
|
|
| 824 |
feedback_toggle_btn,
|
| 825 |
feedback_text,
|
| 826 |
feedback_submit_btn,
|
|
|
|
| 977 |
|
| 978 |
new_status = render_session_status(mode_val, weaknesses, cognitive_state)
|
| 979 |
|
| 980 |
+
# 将当前这一轮的 Q/A 存入 state,详细反馈用(针对“最后一条回答”)
|
| 981 |
return "", new_history, weaknesses, cognitive_state, new_status, message, answer
|
| 982 |
|
| 983 |
user_input.submit(
|
|
|
|
| 1138 |
[feedback_text, feedback_submit_btn],
|
| 1139 |
)
|
| 1140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1141 |
def submit_detailed_feedback(
|
| 1142 |
text, last_q, last_a, user_id_val, mode_val, model_name_val, lang_pref
|
| 1143 |
):
|
|
|
|
| 1187 |
[feedback_text],
|
| 1188 |
)
|
| 1189 |
|
| 1190 |
+
# ===== Per-message thumbs (像 ChatGPT 一样) =====
|
| 1191 |
+
def handle_like(
|
| 1192 |
+
data: gr.LikeData,
|
| 1193 |
+
history,
|
| 1194 |
+
user_id_val,
|
| 1195 |
+
mode_val,
|
| 1196 |
+
model_name_val,
|
| 1197 |
+
lang_pref,
|
| 1198 |
+
):
|
| 1199 |
+
"""
|
| 1200 |
+
data: gr.LikeData (自动注入)
|
| 1201 |
+
history: Chatbot 历史 (list of [user, assistant])
|
| 1202 |
+
"""
|
| 1203 |
+
if data is None or history is None:
|
| 1204 |
+
return
|
| 1205 |
+
|
| 1206 |
+
idx = data.index
|
| 1207 |
+
if not isinstance(idx, int) or idx < 0 or idx >= len(history):
|
| 1208 |
+
return
|
| 1209 |
+
|
| 1210 |
+
question, answer = history[idx]
|
| 1211 |
+
event_type = "thumbs_up" if data.liked else "thumbs_down"
|
| 1212 |
+
|
| 1213 |
+
try:
|
| 1214 |
+
log_event(
|
| 1215 |
+
{
|
| 1216 |
+
"experiment_id": "RESP_AI_W10",
|
| 1217 |
+
"student_id": user_id_val or "ANON",
|
| 1218 |
+
"event_type": event_type,
|
| 1219 |
+
"timestamp": time.time(),
|
| 1220 |
+
"question": question,
|
| 1221 |
+
"answer": answer,
|
| 1222 |
+
"model_name": model_name_val,
|
| 1223 |
+
"language": lang_pref,
|
| 1224 |
+
"learning_mode": mode_val,
|
| 1225 |
+
"turn_index": idx,
|
| 1226 |
+
}
|
| 1227 |
+
)
|
| 1228 |
+
print(f"[Feedback] {event_type} logged for turn {idx}.")
|
| 1229 |
+
except Exception as e:
|
| 1230 |
+
print("like log error:", e)
|
| 1231 |
+
|
| 1232 |
+
chatbot.like(
|
| 1233 |
+
handle_like,
|
| 1234 |
+
inputs=[chatbot, user_id_state, learning_mode, model_name, language_preference],
|
| 1235 |
+
outputs=[],
|
| 1236 |
+
)
|
| 1237 |
+
|
| 1238 |
# ===== Export / Summary =====
|
| 1239 |
export_btn.click(
|
| 1240 |
lambda h, c, m, w, cog: export_conversation(h, c, m, w, cog),
|
|
|
|
| 1281 |
|
| 1282 |
if __name__ == "__main__":
|
| 1283 |
demo.launch(
|
| 1284 |
+
share=True, # 这个环境必须 share 才能访问
|
| 1285 |
server_name="0.0.0.0",
|
| 1286 |
server_port=7860,
|
| 1287 |
)
|