v1.3 W4/W5:讲清失败记卡点(read/plan/calc/check) + 语音讲一遍(录音落盘+三项自评,256 测试)

This commit is contained in:
2026-11-24 10:02:23 +08:00
parent 53699b4e42
commit 5fcbe086fd
13 changed files with 433 additions and 8 deletions
+77 -3
View File
@@ -1022,15 +1022,59 @@ def skip(conn, gid: str, cid: str, mid: str) -> dict:
"note": "跳过不算错也不算对,明天再来一次;连续跳过的会进「顽固题」"}
def explain(conn, gid: str, cid: str, mid: str, channel: str, text: str) -> dict:
#: 语音讲清时,家长/孩子勾的三件事——判不了语义,就让人来判。
EXPLAIN_CHECKS = {
"wrong_reason": "说清了错在哪",
"key_steps": "说出了正确的走法",
"next_time": "说了下次怎么防",
}
def explain(conn, gid: str, cid: str, mid: str, channel: str, text: str,
checks: dict | None = None, audio_b64: str | None = None,
audio_ext: str | None = None) -> dict:
get_child(conn, gid, cid)
m = conn.execute("SELECT * FROM mistakes WHERE id=? AND child_id=?", (mid, cid)).fetchone()
_need(m, "NO_MISTAKE", "没有这道错题")
if channel not in ("ai_voice", "voice", "steps", "find_error", "make_question", "three_lines"):
if channel not in ("ai_voice", "voice", "steps", "find_error", "make_question", "three_lines", "audio"):
raise ApiError(400, "BAD_CHANNEL", "不支持的讲解方式")
if m["status"] != "to_explain":
raise ApiError(409, "NOT_TO_EXPLAIN", "这道题还没到「讲清」这一步(要先连对两次)")
score = scheduler.score_explain(text, channel)
audio_path = None
if channel == "audio":
# 语音:落一份音频;判分不靠关键词,靠三项勾选(说没说清,家长比关键词更准)
if not audio_b64:
raise ApiError(400, "NO_AUDIO", "没有收到录音")
ext = (audio_ext or "webm").lower().lstrip(".")
if ext not in AUDIO_EXTS:
raise ApiError(400, "BAD_AUDIO_EXT", "不支持的音频格式")
try:
blob = base64.b64decode(str(audio_b64).split(",")[-1], validate=False)
except Exception: # noqa: BLE001
raise ApiError(400, "BAD_AUDIO", "录音不是合法的 base64")
if not blob:
raise ApiError(400, "NO_AUDIO", "录音是空的")
if len(blob) > AUDIO_MAX_BYTES:
raise ApiError(400, "AUDIO_TOO_BIG", "录音太大了(上限 8MB)")
def _flag(v):
try:
return bool(int(v))
except (TypeError, ValueError):
return bool(v)
hit = {k: _flag((checks or {}).get(k)) for k in EXPLAIN_CHECKS}
# 三项都勾了才算说清;勾多少记多少,前端据此显示「差在哪一项」
score = {
"rubric_wrong_reason": "说清了" if hit["wrong_reason"] else "没说清错在哪",
"rubric_key_steps": "说了" if hit["key_steps"] else "没说出正确走法",
"rubric_next_time": "说了" if hit["next_time"] else "没说下次怎么防",
"passed": all(hit.values()),
}
AUDIO_DIR.mkdir(parents=True, exist_ok=True)
audio_path = AUDIO_DIR / f"{mid}_{int(time.time())}.{ext}"
audio_path.write_bytes(blob)
text = "(语音 %.1f 秒)" % (len(blob) / 16000.0)
else:
score = scheduler.score_explain(text, channel)
state = scheduler.on_explain(dict(m), bool(score["passed"]))
with tx(conn):
conn.execute(
@@ -1051,9 +1095,36 @@ def explain(conn, gid: str, cid: str, mid: str, channel: str, text: str) -> dict
"status_zh": scheduler.STATUS_ZH.get(state["status"], state["status"]),
"hint": "过了就毕业;没过就换一条路,7 天内还有机会" if not score["passed"] else "这题你不用再做了",
"retreat": retreat,
"ask_stuck_at": (not score["passed"]) and not m["stuck_at"],
"audio": (str(audio_path.relative_to(UPLOAD_DIR)) if audio_path else None),
"checks": ({k: int(v) for k, v in (checks or {}).items() if k in EXPLAIN_CHECKS}
if channel == "audio" else None),
}
#: 讲清失败时问「刚才卡在哪」,只有四个键,不让家长打字。
STUCK_AT = {
"read": "题没读懂",
"plan": "不知道从哪下手",
"calc": "会做,算错了",
"check": "没检查",
}
def set_stuck_at(conn, gid: str, cid: str, mid: str, value) -> dict:
"""记一次卡点。只在讲清失败后问,入库时不问(避免每道题都弹一次)。"""
get_child(conn, gid, cid)
m = conn.execute("SELECT id, stuck_at FROM mistakes WHERE id=? AND child_id=?", (mid, cid)).fetchone()
_need(m, "NO_MISTAKE", "没有这道错题")
key = str(value or "").strip()
if key not in STUCK_AT:
raise ApiError(400, "BAD_STUCK_AT", "卡点只能是 read / plan / calc / check 之一")
with tx(conn):
conn.execute("UPDATE mistakes SET stuck_at=? WHERE id=?", (key, mid))
log_event(conn, "stuck_at", cid, mistake_id=mid, stuck_at=key)
return {"mistake_id": mid, "stuck_at": key, "stuck_at_zh": STUCK_AT[key]}
def _apply(conn, mid: str, state: dict) -> None:
fields = ", ".join(f"{k}=?" for k in state)
conn.execute(f"UPDATE mistakes SET {fields} WHERE id=?", (*state.values(), mid))
@@ -1421,6 +1492,9 @@ def demo_data(conn, gid: str, nickname: str = "皮皮", grade: int = 2) -> dict:
# ---------------------------------------------------------------- 拍照识题
UPLOAD_DIR = Path(__file__).resolve().parent.parent / "data" / "uploads"
AUDIO_DIR = UPLOAD_DIR / "audio"
AUDIO_MAX_BYTES = 8 * 1024 * 1024 # 8MB,够录 2 分钟
AUDIO_EXTS = ("webm", "mp3", "m4a", "aac", "wav", "ogg")
def _drop_upload(path: Path) -> None: