import os import gradio as gr import torch import pydub import torchaudio from torchaudio.sox_effects import apply_effects_tensor import numpy as np from transformers import AutoFeatureExtractor, AutoModelForAudioXVector device = torch.device("cuda" if torch.cuda.is_available() else "cpu") def load_audio(file_name): audio = pydub.AudioSegment.from_file(file_name) arr = np.array(audio.get_array_of_samples(), dtype=np.float32) arr = arr / (1 << (8 * audio.sample_width - 1)) return arr.astype(np.float32), audio.frame_rate STYLE = """ """ OUTPUT_OK = ( STYLE + """

The speakers are

{:.1f}%

similar

Welcome, human!

(You must get at least 85% to be considered the same person)
""" ) OUTPUT_FAIL = ( STYLE + """

The speakers are

{:.1f}%

similar

You shall not pass!

(You must get at least 85% to be considered the same person)
""" ) EFFECTS = [ ["remix", "-"], ["channels", "1"], ["rate", "16000"], ["gain", "-1.0"], ["silence", "1", "0.1", "0.1%", "-1", "0.1", "0.1%"], ["trim", "0", "10"], ] THRESHOLD = 0.85 model_name = "microsoft/wavlm-base-plus-sv" feature_extractor = AutoFeatureExtractor.from_pretrained(model_name) model = AutoModelForAudioXVector.from_pretrained(model_name).to(device) cosine_sim = torch.nn.CosineSimilarity(dim=-1) def similarity_fn(path1, path2): if not (path1 and path2): return 'ERROR: Please record audio for *both* speakers!' wav1, sr1 = load_audio(path1) print(wav1, wav1.shape, wav1.dtype) wav1, _ = apply_effects_tensor(torch.tensor(wav1).unsqueeze(0), sr1, EFFECTS) wav2, sr2 = load_audio(path2) wav2, _ = apply_effects_tensor(torch.tensor(wav2).unsqueeze(0), sr2, EFFECTS) print(wav1.shape, wav2.shape) input1 = feature_extractor(wav1.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device) input2 = feature_extractor(wav2.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device) with torch.no_grad(): emb1 = model(input1).embeddings emb2 = model(input2).embeddings emb1 = torch.nn.functional.normalize(emb1, dim=-1).cpu() emb2 = torch.nn.functional.normalize(emb2, dim=-1).cpu() similarity = cosine_sim(emb1, emb2).numpy()[0] if similarity >= THRESHOLD: output = OUTPUT_OK.format(similarity * 100) else: output = OUTPUT_FAIL.format(similarity * 100) return output inputs = [ gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #1"), gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #2"), ] output = gr.outputs.HTML(label="") description = ( "This demo will compare two speech samples and determine if they are from the same speaker. " "Try it with your own voice!" ) article = ( "

" "🎙️ Learn more about WavLM | " "📚 WavLM paper | " "📚 X-Vector paper" "

" ) examples = [ ["samples/cate_blanch.mp3", "samples/cate_blanch_2.mp3"], ["samples/cate_blanch.mp3", "samples/cate_blanch_3.mp3"], ["samples/cate_blanch_2.mp3", "samples/cate_blanch_3.mp3"], ["samples/heath_ledger.mp3", "samples/heath_ledger_3.mp3"], ["samples/russel_crowe.mp3", "samples/russel_crowe_2.mp3"], ["samples/cate_blanch.mp3", "samples/kirsten_dunst.wav"], ["samples/russel_crowe.mp3", "samples/kirsten_dunst.wav"], ["samples/russel_crowe_2.mp3", "samples/kirsten_dunst.wav"], ["samples/heath_ledger.mp3", "samples/denzel_washington.mp3"], ["samples/leonardo_dicaprio.mp3", "samples/russel_crowe.mp3"], ["samples/leonardo_dicaprio.mp3", "samples/russel_crowe_2.mp3"], ["samples/naomi_watts.mp3", "samples/denzel_washington.mp3"], ["samples/naomi_watts.mp3", "samples/leonardo_dicaprio.mp3"], ["samples/naomi_watts.mp3", "samples/cate_blanch_2.mp3"], ["samples/naomi_watts.mp3", "samples/kirsten_dunst.wav"], ] interface = gr.Interface( fn=similarity_fn, inputs=inputs, outputs=output, title="Voice Authentication with WavLM + X-Vectors", description=description, article=article, layout="horizontal", theme="huggingface", allow_flagging=False, live=False, examples=examples, ) interface.launch(enable_queue=True)