import os import uuid import json from flask import Blueprint, request, jsonify, url_for from flask_login import login_required, current_user from backend.models.database import db, Job, Application from backend.services.interview_engine import ( generate_first_question, edge_tts_to_file_sync, whisper_stt ) interview_api = Blueprint("interview_api", __name__) @interview_api.route("/api/start_interview", methods=["POST"]) @login_required def start_interview(): data = request.get_json() job_id = data.get("job_id") job = Job.query.get_or_404(job_id) application = Application.query.filter_by(user_id=current_user.id, job_id=job_id).first() if not application or not application.extracted_features: return jsonify({"error": "No application/profile data found."}), 400 try: profile = json.loads(application.extracted_features) except: return jsonify({"error": "Invalid profile JSON"}), 500 question = generate_first_question(profile, job) audio_filename = f"q_{uuid.uuid4().hex}.wav" audio_path = os.path.join("static", "audio", audio_filename) os.makedirs(os.path.dirname(audio_path), exist_ok=True) edge_tts_to_file_sync(question, audio_path) return jsonify({ "question": question, "audio_url": url_for("static", filename=f"audio/{audio_filename}") }) @interview_api.route("/api/transcribe_audio", methods=["POST"]) @login_required def transcribe_audio(): audio_file = request.files.get("audio") if not audio_file: return jsonify({"error": "No audio file received."}), 400 filename = f"user_audio_{uuid.uuid4().hex}.wav" path = os.path.join("temp", filename) os.makedirs("temp", exist_ok=True) audio_file.save(path) transcript = whisper_stt(path) os.remove(path) return jsonify({"transcript": transcript}) @interview_api.route("/api/process_answer", methods=["POST"]) @login_required def process_answer(): data = request.get_json() answer = data.get("answer", "") question_idx = data.get("questionIndex", 0) # For now: just return a fake next question fake_next_question = f"Follow-up question {question_idx + 2}: Why should we hire you?" audio_filename = f"q_{uuid.uuid4().hex}.wav" audio_path = os.path.join("static", "audio", audio_filename) os.makedirs(os.path.dirname(audio_path), exist_ok=True) edge_tts_to_file_sync(fake_next_question, audio_path) return jsonify({ "success": True, "nextQuestion": fake_next_question, "audioUrl": url_for("static", filename=f"audio/{audio_filename}"), "evaluation": { "score": "medium", "feedback": "Good answer, but be more specific." }, "isComplete": question_idx >= 2, "summary": [] # optional: you can fill this later })