Spaces:
Paused
Paused
Commit
·
330157f
1
Parent(s):
308d699
audio updated
Browse files
backend/routes/interview_api.py
CHANGED
@@ -77,64 +77,48 @@ def start_interview():
|
|
77 |
logging.error(f"Error in start_interview: {e}")
|
78 |
return jsonify({"error": "Internal server error"}), 500
|
79 |
|
80 |
-
|
81 |
@interview_api.route("/transcribe_audio", methods=["POST"])
|
82 |
@login_required
|
83 |
def transcribe_audio():
|
84 |
-
"""Transcribe uploaded audio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
try:
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
audio_file.seek(0, 2) # Seek to end
|
92 |
-
file_size = audio_file.tell()
|
93 |
-
audio_file.seek(0) # Seek back to start
|
94 |
-
|
95 |
-
if file_size == 0:
|
96 |
-
logging.error("Received empty audio file")
|
97 |
-
return jsonify({"error": "Empty audio file received."}), 400
|
98 |
-
|
99 |
-
logging.info(f"Received audio file: {file_size} bytes")
|
100 |
-
|
101 |
-
# Use /tmp directory which is writable in Hugging Face Spaces
|
102 |
-
temp_dir = "/tmp/interview_temp"
|
103 |
-
os.makedirs(temp_dir, exist_ok=True)
|
104 |
-
|
105 |
-
# Keep original extension for better compatibility
|
106 |
-
original_filename = audio_file.filename or "recording.webm"
|
107 |
-
file_extension = os.path.splitext(original_filename)[1] or ".webm"
|
108 |
-
filename = f"user_audio_{uuid.uuid4().hex}{file_extension}"
|
109 |
-
path = os.path.join(temp_dir, filename)
|
110 |
-
|
111 |
-
# Save the file
|
112 |
-
audio_file.save(path)
|
113 |
-
|
114 |
-
# Verify file was saved
|
115 |
-
if not os.path.exists(path) or os.path.getsize(path) == 0:
|
116 |
-
logging.error(f"Failed to save audio file or file is empty: {path}")
|
117 |
-
return jsonify({"error": "Failed to save audio file."}), 500
|
118 |
-
|
119 |
-
logging.info(f"Audio file saved: {path} ({os.path.getsize(path)} bytes)")
|
120 |
-
|
121 |
-
# Transcribe the audio
|
122 |
-
transcript = whisper_stt(path)
|
123 |
-
|
124 |
-
# Clean up
|
125 |
-
try:
|
126 |
-
os.remove(path)
|
127 |
-
except Exception as e:
|
128 |
-
logging.warning(f"Could not remove temp file {path}: {e}")
|
129 |
-
|
130 |
-
if not transcript or not transcript.strip():
|
131 |
-
return jsonify({"error": "No speech detected in audio. Please try again."}), 400
|
132 |
-
|
133 |
-
return jsonify({"transcript": transcript})
|
134 |
-
|
135 |
except Exception as e:
|
136 |
-
logging.error(f"
|
137 |
-
return jsonify({"error": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
@interview_api.route("/process_answer", methods=["POST"])
|
140 |
@login_required
|
|
|
77 |
logging.error(f"Error in start_interview: {e}")
|
78 |
return jsonify({"error": "Internal server error"}), 500
|
79 |
|
80 |
+
import subprocess
|
81 |
@interview_api.route("/transcribe_audio", methods=["POST"])
|
82 |
@login_required
|
83 |
def transcribe_audio():
|
84 |
+
"""Transcribe uploaded .webm audio using ffmpeg conversion and Faster-Whisper"""
|
85 |
+
audio_file = request.files.get("audio")
|
86 |
+
if not audio_file:
|
87 |
+
return jsonify({"error": "No audio file received."}), 400
|
88 |
+
|
89 |
+
temp_dir = "/tmp/interview_temp"
|
90 |
+
os.makedirs(temp_dir, exist_ok=True)
|
91 |
+
|
92 |
+
original_path = os.path.join(temp_dir, f"user_audio_{uuid.uuid4().hex}.webm")
|
93 |
+
wav_path = original_path.replace(".webm", ".wav")
|
94 |
+
|
95 |
+
audio_file.save(original_path)
|
96 |
+
|
97 |
+
# Convert to WAV using ffmpeg
|
98 |
try:
|
99 |
+
subprocess.run(
|
100 |
+
["ffmpeg", "-y", "-i", original_path, wav_path],
|
101 |
+
stdout=subprocess.DEVNULL,
|
102 |
+
stderr=subprocess.DEVNULL
|
103 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
except Exception as e:
|
105 |
+
logging.error(f"FFmpeg conversion failed: {e}")
|
106 |
+
return jsonify({"error": "Failed to convert audio"}), 500
|
107 |
+
|
108 |
+
# Transcribe
|
109 |
+
transcript = whisper_stt(wav_path)
|
110 |
+
|
111 |
+
# Cleanup
|
112 |
+
try:
|
113 |
+
os.remove(original_path)
|
114 |
+
os.remove(wav_path)
|
115 |
+
except:
|
116 |
+
pass
|
117 |
+
|
118 |
+
if not transcript or not transcript.strip():
|
119 |
+
return jsonify({"error": "No speech detected in audio. Please try again."}), 400
|
120 |
+
|
121 |
+
return jsonify({"transcript": transcript})
|
122 |
|
123 |
@interview_api.route("/process_answer", methods=["POST"])
|
124 |
@login_required
|
backend/templates/interview.html
CHANGED
@@ -757,7 +757,8 @@
|
|
757 |
console.log('Processing', this.audioChunks.length, 'audio chunks');
|
758 |
|
759 |
// Create blob from audio chunks
|
760 |
-
const audioBlob = new Blob(this.audioChunks, { type: 'audio/webm' });
|
|
|
761 |
console.log('Created audio blob:', audioBlob.size, 'bytes');
|
762 |
|
763 |
if (audioBlob.size === 0) {
|
|
|
757 |
console.log('Processing', this.audioChunks.length, 'audio chunks');
|
758 |
|
759 |
// Create blob from audio chunks
|
760 |
+
const audioBlob = new Blob(this.audioChunks, { type: 'audio/webm;codecs=opus' });
|
761 |
+
|
762 |
console.log('Created audio blob:', audioBlob.size, 'bytes');
|
763 |
|
764 |
if (audioBlob.size === 0) {
|