Upload folder using huggingface_hub
Browse filesFiles changed (2)
186 unmodified lines18718818919019119219319441 unmodified lines23623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627738 unmodified lines316317318319320321186 unmodified linesprint(f"🎯 Final: {len(filtered_segments)} segments for Whisper")return filtered_segmentsdef run_transcription(self, audio_path, diar_json):"""FIXED: Transcription with proper timestamp conversion and error handling"""# FIX: Load and standardize audioaudio, sr = torchaudio.load(audio_path)41 unmodified linesreduced = chunktry:result = self.asr_pipeline(reduced)except Exception as e:print(f"⚠️ Whisper failed on segment {start:.2f}-{end:.2f}: {e}")continuetokens = []segment_text = ""if "chunks" in result:for word_info in result["chunks"]:# FIX: Convert relative timestamps to absolutetimestamp = word_info.get("timestamp")text = word_info.get("text", "").strip()if text:if timestamp and isinstance(timestamp, (list, tuple)) and len(timestamp) == 2:rel_start, rel_end = timestamp# Validate timestamps are reasonableif 0 <= rel_start < rel_end <= (end - start):abs_start = start + rel_start # Convert to absolute timeabs_end = start + rel_end # Convert to absolute timeelse:# Invalid timestamps, use segment boundariesabs_start = startabs_end = endelse:# No timestamps from Whisper, use segment boundariesabs_start = startabs_end = endtokens.append({"start": abs_start, # Store absolute time"end": abs_end, # Store absolute time"text": text,"tag": "w"})segment_text += text + " "38 unmodified linesdiar_json = self.run_diarization(audio_path)merged_segments, speakers = self.run_transcription(audio_path, diar_json)# NEW: Combine ASR segments with NSE events if providedif nse_events:print(f"🔊 Combining {len(merged_segments)} ASR segments with {len(nse_events)} NSE events")186 unmodified lines18718818919019119219319419519619719819920020120220320420520620720820921021121241 unmodified lines25425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431538 unmodified lines354355356357358359360361362186 unmodified linesprint(f"🎯 Final: {len(filtered_segments)} segments for Whisper")return filtered_segmentsdef map_speaker_labels(self, segments, original_speakers=['A', 'B', 'C', 'D']):"""NEW: Map SPEAKER_XX labels to A, B, C, D format to match original"""unique_speakers = list(set([seg['speaker'] for seg in segments]))speaker_map = {}# Create mapping from SPEAKER_00 -> A, SPEAKER_01 -> B, etc.for i, spk in enumerate(sorted(unique_speakers)):if i < len(original_speakers):speaker_map[spk] = original_speakers[i]else:speaker_map[spk] = f"SPK_{i}"# Apply mapping to all segmentsfor seg in segments:seg['speaker'] = speaker_map[seg['speaker']]return segments, list(speaker_map.values())def run_transcription(self, audio_path, diar_json):"""FIXED: Transcription with proper word-level timestamp extraction"""# FIX: Load and standardize audioaudio, sr = torchaudio.load(audio_path)41 unmodified linesreduced = chunktry:# FIX: Force word-level timestamps and better configurationresult = self.asr_pipeline(reduced,return_timestamps="word", # FORCE word-level timestampsgenerate_kwargs={"task": "transcribe","language": "en"})except Exception as e:print(f"⚠️ Whisper failed on segment {start:.2f}-{end:.2f}: {e}")continuetokens = []segment_text = ""# FIXED: Proper word-level timestamp extractionif "chunks" in result:for chunk_info in result["chunks"]:timestamp = chunk_info.get("timestamp")text = chunk_info.get("text", "").strip()if text and timestamp:chunk_start, chunk_end = timestamp# Validate and convert to absolute timeif 0 <= chunk_start <= chunk_end <= (end - start):abs_start = start + chunk_startabs_end = start + chunk_endelse:# Fallback: use segment boundariesabs_start = startabs_end = end# NEW: Split into individual words with distributed timestampswords = text.split()if len(words) == 1:# Single word - use original timestamptokens.append({"start": abs_start,"end": abs_end,"text": text,"tag": "w"})else:# Multiple words - distribute time evenlyword_duration = (abs_end - abs_start) / len(words)for i, word in enumerate(words):word_start = abs_start + (i * word_duration)word_end = word_start + word_durationtokens.append({"start": word_start,"end": word_end,"text": word,"tag": "w"})segment_text += text + " "38 unmodified linesdiar_json = self.run_diarization(audio_path)merged_segments, speakers = self.run_transcription(audio_path, diar_json)# NEW: Map speaker labels to match original format (A, B, C, D)merged_segments, speakers = self.map_speaker_labels(merged_segments)# NEW: Combine ASR segments with NSE events if providedif nse_events:print(f"🔊 Combining {len(merged_segments)} ASR segments with {len(nse_events)} NSE events")
4 unmodified lines5674 unmodified linesnoisereducejiwerlibrosa4 unmodified lines567894 unmodified linesnoisereducejiwerlibrosawebrtcvadresampy
asr_diarization/pipeline.py+66-25
requirements.txt+2