app.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from flask import Flask, request, jsonify
  2. from duplicate_checker import QuestionDuplicateChecker
  3. app = Flask(__name__)
  4. # 初始化查重器(全局单例,避免重复加载索引)
  5. checker = QuestionDuplicateChecker()
  6. @app.route('/api/check_duplicate', methods=['POST'])
  7. def check_duplicate():
  8. """
  9. 题目查重 API 接口 (提交前预检模式)
  10. 参数: stem, options, answer, solution
  11. """
  12. data = request.get_json()
  13. if not data:
  14. return jsonify({"code": -1, "message": "Missing content"}), 400
  15. # 提取内容字段
  16. question_data = {
  17. "stem": data.get('stem', ''),
  18. "options": data.get('options', ''),
  19. "answer": data.get('answer', ''),
  20. "solution": data.get('solution', '')
  21. }
  22. if not question_data["stem"]:
  23. return jsonify({"code": -1, "message": "stem is required"}), 400
  24. # 执行基于内容的查重
  25. result = checker.check_duplicate_by_content(question_data)
  26. if result.get("status") == "error":
  27. return jsonify({"code": -1, "message": result.get("message")}), 500
  28. if result.get("is_duplicate"):
  29. item = result["top_similar"][0]
  30. gpt_info = " (经 GPT-4o 深度核验)" if result.get("gpt_checked") else ""
  31. return jsonify({
  32. "code": -1,
  33. "result": {
  34. "repeatIdList": [{
  35. "questionsId": item["id"],
  36. "repeatMsg": f"相似度: {item['similarity']}{gpt_info}。相似点: {item['similar_point']}"
  37. }]
  38. }
  39. })
  40. else:
  41. return jsonify({"code": 0, "result": "ok"})
  42. @app.route('/api/sync', methods=['POST'])
  43. def sync_index():
  44. """手动触发全量同步接口"""
  45. try:
  46. checker.sync_all_from_db()
  47. return jsonify({"code": 0, "result": "Sync completed"})
  48. except Exception as e:
  49. return jsonify({"code": -1, "message": str(e)}), 500
  50. @app.route('/api/confirm_repeat', methods=['POST'])
  51. def confirm_repeat():
  52. """
  53. 人工确认查重结果接口
  54. 参数: questionId, isRepeat (0: 无相似, 1: 有重复)
  55. """
  56. data = request.get_json()
  57. if not data:
  58. return jsonify({"code": -1, "message": "Missing JSON body"}), 400
  59. question_id = data.get('questionId')
  60. is_repeat = data.get('isRepeat')
  61. if question_id is None or is_repeat is None:
  62. return jsonify({"code": -1, "message": "Missing questionId or isRepeat"}), 400
  63. try:
  64. success = checker.confirm_repeat(int(question_id), int(is_repeat))
  65. if success:
  66. return jsonify({"code": 0, "result": "ok"})
  67. else:
  68. return jsonify({"code": -1, "message": "Failed to update"}), 500
  69. except Exception as e:
  70. return jsonify({"code": -1, "message": str(e)}), 500
  71. if __name__ == '__main__':
  72. # 启动服务,默认 5000 端口
  73. app.run(host='0.0.0.0', port=8888, debug=False)