Spaces:
Paused
Paused
Commit
·
9f2b0ed
1
Parent(s):
ca3e053
indendent updated
Browse files- backend/routes/interview_api.py +103 -103
backend/routes/interview_api.py
CHANGED
|
@@ -1,107 +1,107 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import json
|
| 4 |
+
from flask import Blueprint, request, jsonify, url_for
|
| 5 |
+
from flask_login import login_required, current_user
|
| 6 |
+
from backend.models.database import db, Job, Application
|
| 7 |
+
from backend.services.interview_engine import (
|
| 8 |
+
generate_first_question,
|
| 9 |
+
edge_tts_to_file_sync,
|
| 10 |
+
whisper_stt,
|
| 11 |
+
evaluate_answer
|
| 12 |
+
)
|
| 13 |
|
| 14 |
+
interview_api = Blueprint("interview_api", __name__)
|
| 15 |
|
| 16 |
+
@interview_api.route("/start_interview", methods=["POST"])
|
| 17 |
+
@login_required
|
| 18 |
+
def start_interview():
|
| 19 |
+
data = request.get_json()
|
| 20 |
+
job_id = data.get("job_id")
|
| 21 |
+
|
| 22 |
+
job = Job.query.get_or_404(job_id)
|
| 23 |
+
application = Application.query.filter_by(
|
| 24 |
+
user_id=current_user.id,
|
| 25 |
+
job_id=job_id
|
| 26 |
+
).first()
|
| 27 |
+
|
| 28 |
+
if not application or not application.extracted_features:
|
| 29 |
+
return jsonify({"error": "No application/profile data found."}), 400
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
profile = json.loads(application.extracted_features)
|
| 33 |
+
except:
|
| 34 |
+
return jsonify({"error": "Invalid profile JSON"}), 500
|
| 35 |
+
|
| 36 |
+
question = generate_first_question(profile, job)
|
| 37 |
+
|
| 38 |
+
# Create static/audio directory if it doesn't exist
|
| 39 |
+
audio_dir = os.path.join("static", "audio")
|
| 40 |
+
os.makedirs(audio_dir, exist_ok=True)
|
| 41 |
+
|
| 42 |
+
audio_filename = f"q_{uuid.uuid4().hex}.wav"
|
| 43 |
+
audio_path = os.path.join(audio_dir, audio_filename)
|
| 44 |
+
|
| 45 |
+
# Generate audio
|
| 46 |
+
edge_tts_to_file_sync(question, audio_path)
|
| 47 |
+
|
| 48 |
+
return jsonify({
|
| 49 |
+
"question": question,
|
| 50 |
+
"audio_url": url_for("static", filename=f"audio/{audio_filename}")
|
| 51 |
+
})
|
| 52 |
|
| 53 |
+
@interview_api.route("/transcribe_audio", methods=["POST"])
|
| 54 |
+
@login_required
|
| 55 |
+
def transcribe_audio():
|
| 56 |
+
audio_file = request.files.get("audio")
|
| 57 |
+
if not audio_file:
|
| 58 |
+
return jsonify({"error": "No audio file received."}), 400
|
| 59 |
+
|
| 60 |
+
# Create temp directory if it doesn't exist
|
| 61 |
+
temp_dir = "temp"
|
| 62 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 63 |
+
|
| 64 |
+
filename = f"user_audio_{uuid.uuid4().hex}.wav"
|
| 65 |
+
path = os.path.join(temp_dir, filename)
|
| 66 |
+
audio_file.save(path)
|
| 67 |
+
|
| 68 |
+
transcript = whisper_stt(path)
|
| 69 |
+
|
| 70 |
+
# Clean up
|
| 71 |
+
try:
|
| 72 |
+
os.remove(path)
|
| 73 |
+
except:
|
| 74 |
+
pass
|
| 75 |
+
|
| 76 |
+
return jsonify({"transcript": transcript})
|
| 77 |
|
| 78 |
+
@interview_api.route("/process_answer", methods=["POST"])
|
| 79 |
+
@login_required
|
| 80 |
+
def process_answer():
|
| 81 |
+
data = request.get_json()
|
| 82 |
+
answer = data.get("answer", "")
|
| 83 |
+
question_idx = data.get("questionIndex", 0)
|
| 84 |
+
|
| 85 |
+
# Generate next question (simplified for now)
|
| 86 |
+
next_question = f"Follow-up question {question_idx + 2}: Can you elaborate on your experience with relevant technologies?"
|
| 87 |
+
|
| 88 |
+
# Create audio for next question
|
| 89 |
+
audio_dir = os.path.join("static", "audio")
|
| 90 |
+
os.makedirs(audio_dir, exist_ok=True)
|
| 91 |
+
|
| 92 |
+
audio_filename = f"q_{uuid.uuid4().hex}.wav"
|
| 93 |
+
audio_path = os.path.join(audio_dir, audio_filename)
|
| 94 |
+
|
| 95 |
+
edge_tts_to_file_sync(next_question, audio_path)
|
| 96 |
+
|
| 97 |
+
return jsonify({
|
| 98 |
+
"success": True,
|
| 99 |
+
"nextQuestion": next_question,
|
| 100 |
+
"audioUrl": url_for("static", filename=f"audio/{audio_filename}"),
|
| 101 |
+
"evaluation": {
|
| 102 |
+
"score": "medium",
|
| 103 |
+
"feedback": "Good answer, but be more specific."
|
| 104 |
+
},
|
| 105 |
+
"isComplete": question_idx >= 2,
|
| 106 |
+
"summary": []
|
| 107 |
+
})
|