Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,10 @@ import base64
|
|
| 4 |
from collections import defaultdict
|
| 5 |
from typing import List, Dict
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
from langsmith import Client
|
| 9 |
|
|
@@ -266,9 +270,6 @@ ls_client = Client()
|
|
| 266 |
LS_DATASET_NAME = "clare_user_events"
|
| 267 |
|
| 268 |
def log_event(data: Dict):
|
| 269 |
-
"""
|
| 270 |
-
所有事件(chat_turn / micro_quiz_start / like / dislike)统一写入 LangSmith Dataset。
|
| 271 |
-
"""
|
| 272 |
try:
|
| 273 |
inputs = {
|
| 274 |
"question": data.get("question"),
|
|
@@ -278,10 +279,11 @@ def log_event(data: Dict):
|
|
| 278 |
outputs = {
|
| 279 |
"answer": data.get("answer"),
|
| 280 |
}
|
|
|
|
| 281 |
metadata = {
|
| 282 |
k: v
|
| 283 |
for k, v in data.items()
|
| 284 |
-
if k not in ("question", "answer"
|
| 285 |
}
|
| 286 |
|
| 287 |
ls_client.create_example(
|
|
@@ -293,6 +295,8 @@ def log_event(data: Dict):
|
|
| 293 |
except Exception as e:
|
| 294 |
print("LangSmith log failed:", e)
|
| 295 |
|
|
|
|
|
|
|
| 296 |
# ===== Reference Formatting Helper =====
|
| 297 |
def format_references(
|
| 298 |
rag_chunks: List[Dict], max_files: int = 2, max_sections_per_file: int = 3
|
|
@@ -371,14 +375,15 @@ def is_academic_query(message: str) -> bool:
|
|
| 371 |
return False
|
| 372 |
|
| 373 |
return True
|
| 374 |
-
|
| 375 |
# ======= Chatbot like/dislike handler (per-message, ChatGPT-style) =======
|
| 376 |
-
def handle_like(
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
|
|
|
|
|
|
| 382 |
"""
|
| 383 |
data.index: 第几条消息被点
|
| 384 |
data.value: "like" / "dislike"
|
|
@@ -411,6 +416,7 @@ def handle_like(data: gr.LikeData,
|
|
| 411 |
except Exception as e:
|
| 412 |
print("handle_like error:", e)
|
| 413 |
|
|
|
|
| 414 |
# ================== Gradio App ==================
|
| 415 |
with gr.Blocks(
|
| 416 |
title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
|
|
@@ -567,15 +573,23 @@ with gr.Blocks(
|
|
| 567 |
avatar_images=(None, CLARE_LOGO_PATH),
|
| 568 |
show_label=False,
|
| 569 |
type="tuples",
|
|
|
|
| 570 |
)
|
| 571 |
|
| 572 |
# ChatGPT-style per-message like/dislike
|
| 573 |
chatbot.like(
|
| 574 |
fn=handle_like,
|
| 575 |
-
inputs=[
|
| 576 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
)
|
| 578 |
|
|
|
|
| 579 |
# 用户输入
|
| 580 |
user_input = gr.Textbox(
|
| 581 |
label="Your Input",
|
|
|
|
| 4 |
from collections import defaultdict
|
| 5 |
from typing import List, Dict
|
| 6 |
|
| 7 |
+
import time
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
|
| 11 |
import gradio as gr
|
| 12 |
from langsmith import Client
|
| 13 |
|
|
|
|
| 270 |
LS_DATASET_NAME = "clare_user_events"
|
| 271 |
|
| 272 |
def log_event(data: Dict):
|
|
|
|
|
|
|
|
|
|
| 273 |
try:
|
| 274 |
inputs = {
|
| 275 |
"question": data.get("question"),
|
|
|
|
| 279 |
outputs = {
|
| 280 |
"answer": data.get("answer"),
|
| 281 |
}
|
| 282 |
+
# ✅ 只排除 question / answer,其余都进 metadata
|
| 283 |
metadata = {
|
| 284 |
k: v
|
| 285 |
for k, v in data.items()
|
| 286 |
+
if k not in ("question", "answer")
|
| 287 |
}
|
| 288 |
|
| 289 |
ls_client.create_example(
|
|
|
|
| 295 |
except Exception as e:
|
| 296 |
print("LangSmith log failed:", e)
|
| 297 |
|
| 298 |
+
|
| 299 |
+
|
| 300 |
# ===== Reference Formatting Helper =====
|
| 301 |
def format_references(
|
| 302 |
rag_chunks: List[Dict], max_files: int = 2, max_sections_per_file: int = 3
|
|
|
|
| 375 |
return False
|
| 376 |
|
| 377 |
return True
|
|
|
|
| 378 |
# ======= Chatbot like/dislike handler (per-message, ChatGPT-style) =======
|
| 379 |
+
def handle_like(
|
| 380 |
+
data: "gr.LikeData", # 或者直接去掉类型标注也可以
|
| 381 |
+
history,
|
| 382 |
+
user_id_val,
|
| 383 |
+
mode_val,
|
| 384 |
+
model_name_val,
|
| 385 |
+
lang_pref,
|
| 386 |
+
):
|
| 387 |
"""
|
| 388 |
data.index: 第几条消息被点
|
| 389 |
data.value: "like" / "dislike"
|
|
|
|
| 416 |
except Exception as e:
|
| 417 |
print("handle_like error:", e)
|
| 418 |
|
| 419 |
+
|
| 420 |
# ================== Gradio App ==================
|
| 421 |
with gr.Blocks(
|
| 422 |
title="Clare – Hanbridge AI Teaching Assistant", css=CUSTOM_CSS
|
|
|
|
| 573 |
avatar_images=(None, CLARE_LOGO_PATH),
|
| 574 |
show_label=False,
|
| 575 |
type="tuples",
|
| 576 |
+
likeable=True,
|
| 577 |
)
|
| 578 |
|
| 579 |
# ChatGPT-style per-message like/dislike
|
| 580 |
chatbot.like(
|
| 581 |
fn=handle_like,
|
| 582 |
+
inputs=[
|
| 583 |
+
chatbot, # history
|
| 584 |
+
user_id_state,
|
| 585 |
+
learning_mode,
|
| 586 |
+
model_name,
|
| 587 |
+
language_preference,
|
| 588 |
+
],
|
| 589 |
+
outputs=[], # 我们这里只写日志,不改 UI
|
| 590 |
)
|
| 591 |
|
| 592 |
+
|
| 593 |
# 用户输入
|
| 594 |
user_input = gr.Textbox(
|
| 595 |
label="Your Input",
|