import gradio as gr from pyannote.audio import Pipeline from transformers import pipeline asr = pipeline( "automatic-speech-recognition", model="facebook/wav2vec2-large-960h-lv60-self", feature_extractor="facebook/wav2vec2-large-960h-lv60-self", ) speaker_segmentation = Pipeline.from_pretrained("pyannote/speaker-segmentation") def segmentation(audio): speaker_output = speaker_segmentation(audio) text_output = asr(audio,return_timestamps="word") full_text = text_output['text'].lower() chunks = text_output['chunks'] diarized_output = "" i = 0 for turn, _, speaker in speaker_output.itertracks(yield_label=True): diarized = "" while i < len(chunks) and chunks[i]['timestamp'][1] <= turn.end: diarized += chunks[i]['text'].lower() + ' ' i += 1 if diarized != "": diarized_output += "{}: ''{}'' from {:.3f}-{:.3f}\n".format(speaker,diarized,turn.start,turn.end) return diarized_output, full_text title = "Speech Recognition with Speaker Segmentation" description = "Speaker Diarization is the act of attributing individual speakers to their corresponding parts in an audio recording. This space aims to distinguish the speakers with speaker segmentation and their speech with speech-to-text from a given input audio file. Pre-trained models used are Pyannote[1] for the Speaker Segmentation and Wav2Vec2[2] for the Automatic Speech Recognition." article = "

[1] Pyannote - Speaker Segmentation model (GitHub repo)

" article += "

[2] Facebook Wav2Vec2 (GitHub repo)

" article += "

Audio File Sources: 1 2 3 4 5

" inputs = gr.inputs.Audio(source="upload", type="filepath", label="Upload your audio file here:") outputs = [gr.outputs.Textbox(type="auto", label="Diarized Output"), gr.outputs.Textbox(type="auto",label="Full ASR Text for comparison")] examples = [["meeting_audio.wav"], ["noisy_london_interview.wav"], ["clean_london_interview.wav"], ["podcast_audio.wav"], ["air_traffic_control_audio.wav"],] app = gr.Interface(fn=segmentation, inputs=inputs, outputs=outputs, examples=examples, title=title, description=description, article=article, allow_flagging=False) app.launch()