| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- from flask import Flask, request, jsonify
- from duplicate_checker import QuestionDuplicateChecker
- app = Flask(__name__)
- # 初始化查重器(全局单例,避免重复加载索引)
- checker = QuestionDuplicateChecker()
- @app.route('/api/check_duplicate', methods=['POST'])
- def check_duplicate():
- """
- 题目查重 API 接口 (提交前预检模式)
- 参数: stem, options, answer, solution
- """
- data = request.get_json()
- if not data:
- return jsonify({"code": -1, "message": "Missing content"}), 400
- # 提取内容字段
- question_data = {
- "stem": data.get('stem', ''),
- "options": data.get('options', ''),
- "answer": data.get('answer', ''),
- "solution": data.get('solution', '')
- }
- if not question_data["stem"]:
- return jsonify({"code": -1, "message": "stem is required"}), 400
- # 执行基于内容的查重
- result = checker.check_duplicate_by_content(question_data)
- if result.get("status") == "error":
- return jsonify({"code": -1, "message": result.get("message")}), 500
- if result.get("is_duplicate"):
- item = result["top_similar"][0]
- gpt_info = " (经 GPT-4o 深度核验)" if result.get("gpt_checked") else ""
- return jsonify({
- "code": -1,
- "result": {
- "repeatIdList": [{
- "questionsId": item["id"],
- "repeatMsg": f"相似度: {item['similarity']}{gpt_info}。相似点: {item['similar_point']}"
- }]
- }
- })
- else:
- return jsonify({"code": 0, "result": "ok"})
- @app.route('/api/sync', methods=['POST'])
- def sync_index():
- """手动触发全量同步接口"""
- try:
- checker.sync_all_from_db()
- return jsonify({"code": 0, "result": "Sync completed"})
- except Exception as e:
- return jsonify({"code": -1, "message": str(e)}), 500
- @app.route('/api/confirm_repeat', methods=['POST'])
- def confirm_repeat():
- """
- 人工确认查重结果接口
- 参数: questionId, isRepeat (0: 无相似, 1: 有重复)
- """
- data = request.get_json()
- if not data:
- return jsonify({"code": -1, "message": "Missing JSON body"}), 400
-
- question_id = data.get('questionId')
- is_repeat = data.get('isRepeat')
- if question_id is None or is_repeat is None:
- return jsonify({"code": -1, "message": "Missing questionId or isRepeat"}), 400
- try:
- success = checker.confirm_repeat(int(question_id), int(is_repeat))
- if success:
- return jsonify({"code": 0, "result": "ok"})
- else:
- return jsonify({"code": -1, "message": "Failed to update"}), 500
- except Exception as e:
- return jsonify({"code": -1, "message": str(e)}), 500
- if __name__ == '__main__':
- # 启动服务,默认 5000 端口
- app.run(host='0.0.0.0', port=8888, debug=False)
|