Update app.py
Browse files48 unmodified lines49505152525354554 unmodified lines60616263646566676863646566676869707172736 unmodified lines808182838485868788899091929394959697989910110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115210010110210310410510610715415515615710810911011184 unmodified lines1961971982482492502512522532542552562572582592602612622632642652662672681992002012022032042052062072082092102702712722732742752762772782792802812822832842112122132142152162172182192202212222872232242252266 unmodified lines233234235300301302236237238158 unmodified lines39739839946740040140258 unmodified lines4614624635324644654664677 unmodified lines47547647754654754847847948048148248322 unmodified lines50650750857750948 unmodified lines
def associate_speakers_with_timestamps(transcription_result, diarization, tolerance=0.02, min_segment_duration=0.05, max_words_to_merge=20):def associate_speakers_with_timestamps(transcription_result, diarization, tolerance=0.02, min_segment_duration=0.05): word_segments = transcription_result['chunks'] diarization_segments = list(diarization.itertracks(yield_label=True)) speaker_transcription = []4 unmodified lines def flush_current_segment(): nonlocal current_speaker, current_text if current_speaker and current_text: speaker_transcription.append({ "speaker": current_speaker, "text": ' '.join(current_text), "start": word_segments[len(speaker_transcription)]['timestamp'][0], "end": word_segments[len(speaker_transcription) + len(current_text) - 1]['timestamp'][1] }) speaker_transcription.append((current_speaker, ' '.join(current_text))) current_text = []
for word in word_segments: word_start, word_end = word['timestamp'] word_text = word['text']
# Trouver le segment de diarisation correspondant matching_segment = None for segment, _, speaker in diarization_segments: if segment.start - tolerance <= word_start < segment.end + tolerance:6 unmodified lines flush_current_segment() current_speaker = speaker
# Gérer les pauses longues if word_start - last_word_end > 1.0: # Pause de plus d'une seconde flush_current_segment()
current_text.append(word_text) last_word_end = word_end else: # Si aucun segment ne correspond, attribuer au dernier locuteur connu if current_speaker: current_text.append(word_text) else: # Si c'est le premier mot sans correspondance, créer un nouveau segment current_speaker = "SPEAKER_UNKNOWN" current_text.append(word_text)
flush_current_segment()
def detect_interruptions(transcription, time_threshold=0.5): for i in range(len(transcription) - 1): current_end = transcription[i]['end'] next_start = transcription[i+1]['start'] if next_start - current_end < time_threshold: transcription[i]['text'] += ' [...]' transcription[i+1]['text'] = '[...] ' + transcription[i+1]['text'] return transcription
speaker_transcription = detect_interruptions(speaker_transcription)
def post_process_transcription(transcription, max_words): processed = [] current_speaker = None current_text = [] current_start = None current_end = None
for segment in transcription: if segment['speaker'] == current_speaker and len(' '.join(current_text + [segment['text']]).split()) <= max_words: current_text.append(segment['text']) current_end = segment['end'] else: if current_speaker: processed.append({ "speaker": current_speaker, "text": ' '.join(current_text), "start": current_start, "end": current_end }) current_speaker = segment['speaker'] current_text = [segment['text']] current_start = segment['start'] current_end = segment['end']
if current_speaker: processed.append({ "speaker": current_speaker, "text": ' '.join(current_text), "start": current_start, "end": current_end })
return processed
merged_transcription = post_process_transcription(speaker_transcription, max_words_to_merge)
speakers = sorted(set(segment['speaker'] for segment in merged_transcription)) metadata = { "speaker_count": len(speakers), "speakers": speakers } # Fusionner les segments courts du même locuteur merged_transcription = [] for speaker, text in speaker_transcription: if not merged_transcription or merged_transcription[-1][0] != speaker or len(text.split()) > 3: merged_transcription.append((speaker, text)) else: merged_transcription[-1] = (speaker, merged_transcription[-1][1] + " " + text)
return { "transcription": merged_transcription, "metadata": metadata } return merged_transcription def simplify_diarization_output(speaker_transcription): simplified = []84 unmodified lines progress(1.0, desc="Terminé!") return "Transcription terminée!", transcription_result['text'], speaker_transcription
def format_to_markdown(transcription_result, audio_duration=None, location=None, speaker_age=None, context=None, custom_speaker_names=None): if isinstance(transcription_result, dict): metadata = transcription_result.get("metadata", {}) transcription = transcription_result.get("transcription", []) else: metadata = {} transcription = transcription_result
speaker_count = metadata.get("speaker_count", "non spécifié") speakers = metadata.get("speakers", [])
metadata_text = "\n".join([ f"- **Date de traitement** : '{datetime.now().strftime('%d/%m/%Y %H:%M')}'", f"- **Durée de l'audio** : '{audio_duration if audio_duration else '[à remplir]'} secondes'", f"- **Lieu** : '{location if location else '[non spécifié]'}'", f"- **Âge de l'intervenant** : '{f'{speaker_age} ans' if speaker_age else '[non spécifié]'}'", f"- **Contexte** : '{context if context else '[non spécifié]'}'", f"- **Nombre d'interlocuteurs** : '{speaker_count}'", f"- **Interlocuteurs bruts** : '{', '.join(speakers)}'" ])
def format_to_markdown(transcription_text, speaker_transcription, audio_duration=None, location=None, speaker_age=None, context=None): metadata = { "Date de traitement": datetime.now().strftime('%d/%m/%Y %H:%M'), "Durée de l'audio": f"{audio_duration} secondes" if audio_duration else "[à remplir]", "Lieu": location if location else "[non spécifié]", "Âge de l'intervenant": f"{speaker_age} ans" if speaker_age else "[non spécifié]", "Contexte": context if context else "[non spécifié]" } metadata_text = "\n".join([f"- **{key}** : '{value}'" for key, value in metadata.items()]) try: formatted_transcription = [] for segment in transcription: speaker = segment['speaker'] text = segment['text'] start_time = format_time(segment['start']) end_time = format_time(segment['end'])
if custom_speaker_names and speaker in custom_speaker_names: display_speaker = custom_speaker_names[speaker] else: display_speaker = speaker
formatted_transcription.append(f"**[{start_time} - {end_time}] {display_speaker}**: {text}")
transcription_text = "\n\n".join(formatted_transcription) if isinstance(speaker_transcription, str): speaker_transcription = parse_simplified_diarization(speaker_transcription) if isinstance(speaker_transcription, list) and all(isinstance(item, tuple) and len(item) == 2 for item in speaker_transcription): formatted_transcription = [] for speaker, text in speaker_transcription: formatted_transcription.append(f"**{speaker}**: {text}") transcription_text = "\n\n".join(formatted_transcription) else: raise ValueError("Invalid speaker transcription format") except Exception as e: print(f"Error formatting speaker transcription: {e}") transcription_text = "Error formatting speaker transcription. Using raw transcription instead.\n\n" + str(transcription) transcription_text = "Error formatting speaker transcription. Using raw transcription instead.\n\n" + transcription_text
formatted_output = f"""# Transcription Formatée6 unmodified lines""" return formatted_output
def format_time(seconds): return f"{int(seconds // 60):02d}:{int(seconds % 60):02d}"
def _return_yt_html_embed(yt_url): video_id = yt_url.split("?v=")[-1] HTML_str = (158 unmodified lines audio_duration = gr.Textbox(label="⏱️ Durée de l'audio (mm:ss)") location = gr.Textbox(label="📍 Lieu de l'enregistrement") speaker_age = gr.Number(label="👤 Âge de l'intervenant principal") custom_speaker_names = gr.TextArea(label="Noms personnalisés des locuteurs (format: SPEAKER_00: Nom1, SPEAKER_01: Nom2)") context = gr.Textbox(label="📝 Contexte de l'enregistrement") format_button = gr.Button("✨ Générer la transcription formatée", elem_classes="button-secondary")58 unmodified lines - Modèles : - [Whisper-médium](https://huggingface.co/openai/whisper-medium) : Model size - 764M params - Tensor type F32 - - [speaker-diarization-3.1](https://huggingface.co/pyannote/speaker-diarization-3.1) : Model size - Unknow - Tensor type F32 - - Version : V.2.0.0-Bêta - Version : V.2.0.2-Bêta - Langues : FR, EN - Copyright : cc-by-nc-4.0 - [En savoir +](https://huggingface.co/spaces/Woziii/scribe/blob/main/README.md)7 unmodified lines )
format_button.click( format_to_markdown, inputs=[raw_output, speaker_output, audio_duration, location, speaker_age, context, custom_speaker_names], outputs=formatted_output format_to_markdown, inputs=[raw_output, speaker_output, audio_duration, location, speaker_age, context], outputs=formatted_output )
mic_transcribe_button.click(22 unmodified lines
if __name__ == "__main__": demo.queue().launch() demo.queue().launch()app.py+41-109