Spaces:
Sleeping
Sleeping
Commit ·
1f41a8a
1
Parent(s): 330157f
update whisper
Browse filesFiles changed (2)
6 unmodified lines7891011121312 unmodified lines2627282829303132143 unmodified lines1761771781781791801811821831841851861831841851861891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272281871881891901911921931941951962301971981992002012022032042056 unmodified linesimport loggingimport tempfileimport shutilimport torch
# Initialize modelschat_groq_api = os.getenv("GROQ_API_KEY")12 unmodified lines global whisper_model if whisper_model is None: try: device = "cuda" if os.system("nvidia-smi") == 0 else "cpu" device = "cuda" if torch.cuda.is_available() else "cpu" compute_type = "float16" if device == "cuda" else "int8" whisper_model = WhisperModel("base", device=device, compute_type=compute_type) logging.info(f"Whisper model loaded on {device} with {compute_type}")143 unmodified lines return None
def whisper_stt(audio_path): """Speech-to-text using Faster-Whisper with better error handling""" try: if not audio_path or not os.path.exists(audio_path): logging.error(f"Audio file does not exist: {audio_path}") return "" # Check if file has content file_size = os.path.getsize(audio_path) if file_size == 0:
if os.path.getsize(audio_path) == 0: logging.error(f"Audio file is empty: {audio_path}") return "" logging.info(f"Processing audio file: {audio_path} ({file_size} bytes)") # If the file is WebM, try to convert it to WAV if audio_path.endswith('.webm'): wav_path = audio_path.replace('.webm', '.wav') converted_path = convert_webm_to_wav(audio_path, wav_path) if converted_path: audio_path = converted_path else: logging.warning("Could not convert WebM to WAV, trying with original file") model = load_whisper_model() # Add timeout and better error handling try: segments, info = model.transcribe( audio_path, language="en", # Specify language for better performance task="transcribe", vad_filter=True, # Voice activity detection vad_parameters=dict(min_silence_duration_ms=500) ) transcript_parts = [] for segment in segments: if hasattr(segment, 'text') and segment.text.strip(): transcript_parts.append(segment.text.strip()) transcript = " ".join(transcript_parts) if transcript: logging.info(f"Transcription successful: '{transcript[:100]}...'") else: logging.warning("No speech detected in audio file") return transcript.strip() except Exception as e: logging.error(f"Error during transcription: {e}")
# Convert WebM to WAV using ffmpeg (ensure ffmpeg is available) converted_path = audio_path.replace(".webm", ".wav") subprocess.run([ "ffmpeg", "-y", "-i", audio_path, "-ar", "16000", "-ac", "1", converted_path ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if not os.path.exists(converted_path) or os.path.getsize(converted_path) == 0: logging.error(f"Conversion failed or produced empty file: {converted_path}") return ""
model = load_whisper_model() segments, _ = model.transcribe(converted_path) transcript = " ".join(segment.text for segment in segments) return transcript.strip()
except Exception as e: logging.error(f"Error in STT: {e}") return ""694 unmodified lines69569669769869869970070170270370455 unmodified lines760761762760763764765766767694 unmodified lines delete options.mimeType; }
this.mediaRecorder = new MediaRecorder(stream, options); this.mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
this.audioChunks = [];
this.mediaRecorder.ondataavailable = (event) => {55 unmodified lines console.log('Processing', this.audioChunks.length, 'audio chunks');
// Create blob from audio chunks const audioBlob = new Blob(this.audioChunks, { type: 'audio/webm;codecs=opus' }); const audioBlob = new Blob(this.audioChunks, { type: 'audio/webm' }); formData.append('audio', audioBlob, 'recording.webm');
console.log('Created audio blob:', audioBlob.size, 'bytes');
backend/services/interview_engine.py+19-47
backend/templates/interview.html+6-2