app.py 3.0 KB

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