Update app.py
Browse files46 unmodified lines47484950515253545556575859606162636465666768697071727374757677787980818283848586878846 unmodified lines)def associate_speakers_with_timestamps(transcription_result, diarization):word_segments = transcription_result['chunks']diarization_segments = list(diarization.itertracks(yield_label=True))speaker_transcription = []current_segment_index = 0previous_speaker = Nonefor word in word_segments:word_start, word_end = word['timestamp']# Trouver le segment de diarisation correspondantwhile current_segment_index < len(diarization_segments) - 1 and diarization_segments[current_segment_index][0].end <= word_start:current_segment_index += 1current_segment, _, current_speaker = diarization_segments[current_segment_index]# Vérifier s'il y a un chevauchement avec le segment précédentif current_segment_index > 0:previous_segment, _, previous_speaker = diarization_segments[current_segment_index - 1]if previous_segment.end > word_start and previous_speaker != current_speaker:word_text = f"[{word['text']}]"else:word_text = word['text']else:word_text = word['text']# Ajouter le mot au segment du locuteur actuelif not speaker_transcription or speaker_transcription[-1][0] != current_speaker:speaker_transcription.append((current_speaker, word_text))else:speaker_transcription[-1] = (current_speaker, speaker_transcription[-1][1] + " " + word_text)return speaker_transcriptiondef merge_short_segments(speaker_transcription, min_words=3):merged = []for speaker, text in speaker_transcription:46 unmodified lines474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810946 unmodified lines)def associate_speakers_with_timestamps(transcription_result, diarization):word_segments = transcription_result['chunks']diarization_segments = list(diarization.itertracks(yield_label=True))# Pré-calcul des chevauchementsoverlaps = set()for i in range(1, len(diarization_segments)):if diarization_segments[i-1][0].end > diarization_segments[i][0].start:overlaps.add(diarization_segments[i][0].start)speaker_transcription = []current_speaker = Nonecurrent_text = []diar_iter = iter(diarization_segments)current_segment, _, current_speaker = next(diar_iter)for word in word_segments:word_start, _ = word['timestamp']while word_start >= current_segment.end:if current_text:speaker_transcription.append((current_speaker, ' '.join(current_text)))current_text = []current_segment, _, current_speaker = next(diar_iter, (None, None, None))if current_segment is None:breakif current_segment is None:breakif word_start in overlaps:current_text.append(f"[{word['text']}]")else:current_text.append(word['text'])if current_text:speaker_transcription.append((current_speaker, ' '.join(current_text)))return speaker_transcriptiondef merge_short_segments(speaker_transcription, min_words=3):def merge_group(group):speaker, texts = groupmerged = []current = []for text in texts:current.extend(text.split())if len(current) >= min_words:merged.append(' '.join(current))current = []if current:merged.append(' '.join(current))return [(speaker, text) for text in merged]return [item for group in groupby(speaker_transcription, key=lambda x: x[0]) for item in merge_group(group)]def merge_short_segments(speaker_transcription, min_words=3):merged = []for speaker, text in speaker_transcription:
app.py+46-25