husseinelsaadi commited on
Commit
9f2b0ed
·
1 Parent(s): ca3e053

indendent updated

Browse files
Files changed (1) hide show
  1. backend/routes/interview_api.py +103 -103
backend/routes/interview_api.py CHANGED
@@ -1,107 +1,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
- })
 
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
+ })