# AI帮你面试 - 核心应用程序 from flask import Flask, render_template, request, jsonify import os from dotenv import load_dotenv # 加载环境变量 load_dotenv() # 创建Flask应用实例 app = Flask(__name__) # 配置API密钥 API_KEY = os.getenv('AI_API_KEY', '') # 从环境变量获取API密钥 # 定义面试流程数据 interview_process = [ {"id": 1, "title": "自我介绍", "description": "请简要介绍您自己,包括教育背景和工作经验", "duration": "3分钟"}, {"id": 2, "title": "技术能力评估", "description": "回答技术相关问题,展示专业知识", "duration": "15分钟"}, {"id": 3, "title": "项目经验分享", "description": "分享您参与的重要项目和成果", "duration": "10分钟"}, {"id": 4, "title": "问题与解答", "description": "您可以向面试官提问", "duration": "7分钟"} ] # API密钥验证装饰器 def require_api_key(func): def wrapper(*args, **kwargs): # 从请求头获取API密钥 api_key = request.headers.get('X-API-Key') # 调试信息(实际生产环境应移除) print(f"[DEBUG] Received API Key: '{api_key}'") print(f"[DEBUG] Received API Key Length: {len(api_key) if api_key else 0}") print(f"[DEBUG] Expected API Key: '{API_KEY}'") print(f"[DEBUG] Expected API Key Length: {len(API_KEY)}") print(f"[DEBUG] Match: {api_key == API_KEY}") # 检查是否有不可见字符 if api_key: print(f"[DEBUG] Received API Key Hex: {[hex(ord(c)) for c in api_key]}") print(f"[DEBUG] Expected API Key Hex: {[hex(ord(c)) for c in API_KEY]}") # 验证API密钥 if not api_key or api_key != API_KEY: return jsonify({ 'success': False, 'error': 'Unauthorized: Invalid API Key' }), 401 return func(*args, **kwargs) # 保留原函数的元数据 wrapper.__name__ = func.__name__ wrapper.__doc__ = func.__doc__ return wrapper @app.route('/') def home(): """首页路由,显示应用主界面""" return render_template('index.html', app_name='AI帮你面试') @app.route('/start_interview') def start_interview(): """开始面试路由,显示面试流程""" return render_template('interview.html', process=interview_process) @app.route('/analyze_answer', methods=['POST']) @require_api_key # 添加API密钥验证 def analyze_answer(): """AI分析回答的路由""" data = request.get_json() answer = data.get('answer', '') question_id = data.get('question_id', 1) # 模拟AI分析过程 analysis = { 'question_id': question_id, 'content_analysis': { 'completeness': 85, 'relevance': 90, 'depth': 75 }, 'sentiment_analysis': { 'confidence': 92, 'sentiment': 'positive' }, 'suggestions': [ '可以提供更多具体的项目成果数据', '注意控制回答时间,保持简洁明了', '可以结合实例说明技术能力' ], 'overall_score': 85 } return jsonify(analysis) @app.route('/get_results') @require_api_key # 添加API密钥验证 def get_results(): """获取面试结果的路由""" results = { 'overall_score': 88, 'category_scores': { 'professional_knowledge': 90, 'communication_skills': 85, 'problem_solving': 87, 'experience_relevance': 92 }, 'strengths': [ '技术知识扎实', '项目经验丰富', '表达清晰流畅' ], 'improvement_areas': [ '可以更详细地描述团队协作经历', '需要加强行业趋势了解', '回答可以更加结构化' ], 'final_recommendation': '推荐进入下一轮面试' } return jsonify(results) if __name__ == '__main__': """应用程序入口点""" app.run(debug=True, host='0.0.0.0', port=5000)