Spaces:
Sleeping
Sleeping
Commit ·
1f41a8a
1
Parent(s): 330157f
update whisper
Browse filesFiles changed (2)
6 unmodified lines78910111212 unmodified lines25262728293031143 unmodified lines1751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322336 unmodified linesimport loggingimport tempfileimport shutil# Initialize modelschat_groq_api = os.getenv("GROQ_API_KEY")12 unmodified linesglobal whisper_modelif whisper_model is None:try:device = "cuda" if os.system("nvidia-smi") == 0 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 linesreturn Nonedef 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 contentfile_size = os.path.getsize(audio_path)if file_size == 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 WAVif 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_pathelse:logging.warning("Could not convert WebM to WAV, trying with original file")model = load_whisper_model()# Add timeout and better error handlingtry:segments, info = model.transcribe(audio_path,language="en", # Specify language for better performancetask="transcribe",vad_filter=True, # Voice activity detectionvad_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}")return ""except Exception as e:logging.error(f"Error in STT: {e}")return ""6 unmodified lines7891011121312 unmodified lines26272829303132143 unmodified lines1761771781791801811821831841851861871881891901911921931941951961971981992002012022032042056 unmodified linesimport loggingimport tempfileimport shutilimport torch# Initialize modelschat_groq_api = os.getenv("GROQ_API_KEY")12 unmodified linesglobal whisper_modelif whisper_model is None:try: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 linesreturn Nonedef whisper_stt(audio_path):try:if not audio_path or not os.path.exists(audio_path):logging.error(f"Audio file does not exist: {audio_path}")return ""if os.path.getsize(audio_path) == 0:logging.error(f"Audio file is empty: {audio_path}")return ""# 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 lines69569669769869970070155 unmodified lines757758759760761762763694 unmodified linesdelete options.mimeType;}this.mediaRecorder = new MediaRecorder(stream, options);this.audioChunks = [];this.mediaRecorder.ondataavailable = (event) => {55 unmodified linesconsole.log('Processing', this.audioChunks.length, 'audio chunks');// Create blob from audio chunksconst audioBlob = new Blob(this.audioChunks, { type: 'audio/webm;codecs=opus' });console.log('Created audio blob:', audioBlob.size, 'bytes');694 unmodified lines69569669769869970070170270370455 unmodified lines760761762763764765766767694 unmodified linesdelete options.mimeType;}this.mediaRecorder = new MediaRecorder(stream, {mimeType: 'audio/webm;codecs=opus'});this.audioChunks = [];this.mediaRecorder.ondataavailable = (event) => {55 unmodified linesconsole.log('Processing', this.audioChunks.length, 'audio chunks');// Create blob from audio chunksconst 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