错题处理失败时直接显示具体错误信息。
This commit is contained in:
@@ -19,6 +19,13 @@ from app.services.student_access import get_student_for_user
|
||||
router = APIRouter(tags=["wrong_questions"])
|
||||
|
||||
|
||||
def _short_error(exc: BaseException, prefix: str = "") -> str:
|
||||
msg = str(exc).strip() or type(exc).__name__
|
||||
if len(msg) > 500:
|
||||
msg = msg[:500] + "…"
|
||||
return f"{prefix}{msg}" if prefix else msg
|
||||
|
||||
|
||||
def _parse_mark_regions(raw: str | None) -> list[dict] | None:
|
||||
if not raw:
|
||||
return None
|
||||
@@ -43,6 +50,7 @@ def _wq_to_out(wq: WrongQuestion) -> WrongQuestionOut:
|
||||
solution_text=wq.solution_text,
|
||||
mark_regions=_parse_mark_regions(wq.mark_regions_json),
|
||||
has_annotated_image=bool(wq.annotated_image_path),
|
||||
error_message=wq.error_message,
|
||||
status=wq.status,
|
||||
created_at=wq.created_at,
|
||||
)
|
||||
@@ -74,6 +82,7 @@ async def _run_ai_pipeline(wq: WrongQuestion, db: Session, ocr_lines: list[dict]
|
||||
wq.solution_approach = approach
|
||||
wq.solution_text = solution_body if approach else solution_full
|
||||
wq.status = WrongQuestionStatus.solved
|
||||
wq.error_message = None
|
||||
|
||||
|
||||
def _process_wrong_question(question_id: uuid.UUID):
|
||||
@@ -88,22 +97,26 @@ def _process_wrong_question(question_id: uuid.UUID):
|
||||
if wq is None:
|
||||
return
|
||||
|
||||
wq.error_message = None
|
||||
image_full = Path(settings.UPLOAD_DIR) / wq.image_path
|
||||
try:
|
||||
ocr_result = ocr_service.run_ocr_with_regions(str(image_full))
|
||||
ocr_text = ocr_result["text"]
|
||||
ocr_lines = ocr_result["lines"]
|
||||
wq.ocr_raw_text = ocr_text or None
|
||||
wq.status = WrongQuestionStatus.ocr_done if ocr_text else WrongQuestionStatus.failed
|
||||
if not ocr_text:
|
||||
wq.status = WrongQuestionStatus.failed
|
||||
wq.error_message = "OCR 未识别到文字,请拍摄更清晰、光线充足的题目照片"
|
||||
db.commit()
|
||||
return
|
||||
wq.status = WrongQuestionStatus.ocr_done
|
||||
db.commit()
|
||||
except Exception:
|
||||
except Exception as exc:
|
||||
wq.status = WrongQuestionStatus.failed
|
||||
wq.error_message = _short_error(exc, "OCR 识别失败:")
|
||||
db.commit()
|
||||
return
|
||||
|
||||
if not ocr_text:
|
||||
return
|
||||
|
||||
import asyncio
|
||||
|
||||
loop = asyncio.new_event_loop()
|
||||
@@ -111,8 +124,9 @@ def _process_wrong_question(question_id: uuid.UUID):
|
||||
try:
|
||||
loop.run_until_complete(_run_ai_pipeline(wq, db, ocr_lines, ocr_text))
|
||||
db.commit()
|
||||
except Exception:
|
||||
wq.status = WrongQuestionStatus.ocr_done
|
||||
except Exception as exc:
|
||||
wq.status = WrongQuestionStatus.failed
|
||||
wq.error_message = _short_error(exc, "AI 处理失败:")
|
||||
db.commit()
|
||||
finally:
|
||||
loop.close()
|
||||
@@ -293,6 +307,7 @@ def retry_ocr(
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="错题不存在")
|
||||
|
||||
wq.status = WrongQuestionStatus.pending
|
||||
wq.error_message = None
|
||||
db.commit()
|
||||
background_tasks.add_task(_process_wrong_question, wq.id)
|
||||
return _wq_to_out(wq)
|
||||
|
||||
Reference in New Issue
Block a user