Fix invalid wav file error when converting from microphone to file (#1987)

* ;q
:wq

* Add demo

* Add unit test

* Fix test

* Slight refactor

* Lint

* Delete some recordings - import module not method
This commit is contained in:
Freddy Boulton 2022-08-09 16:40:31 -04:00 committed by GitHub
parent c49a4264df
commit 393c1f47d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 147 additions and 13 deletions

View File

@ -0,0 +1,2 @@
git+https://github.com/huggingface/transformers
torchaudio

View File

@ -0,0 +1,120 @@
import gradio as gr
import torch
from torchaudio.sox_effects import apply_effects_file
from transformers import AutoFeatureExtractor, AutoModelForAudioXVector
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
STYLE = """
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha256-YvdLHPgkqJ8DVUxjjnGVlMMJtNimJ6dYkowFFvp4kKs=" crossorigin="anonymous">
"""
OUTPUT_OK = (
STYLE
+ """
<div class="container">
<div class="row"><h1 style="text-align: center">The speakers are</h1></div>
<div class="row"><h1 class="display-1 text-success" style="text-align: center">{:.1f}%</h1></div>
<div class="row"><h1 style="text-align: center">similar</h1></div>
<div class="row"><h1 class="text-success" style="text-align: center">Welcome, human!</h1></div>
<div class="row"><small style="text-align: center">(You must get at least 85% to be considered the same person)</small><div class="row">
</div>
"""
)
OUTPUT_FAIL = (
STYLE
+ """
<div class="container">
<div class="row"><h1 style="text-align: center">The speakers are</h1></div>
<div class="row"><h1 class="display-1 text-danger" style="text-align: center">{:.1f}%</h1></div>
<div class="row"><h1 style="text-align: center">similar</h1></div>
<div class="row"><h1 class="text-danger" style="text-align: center">You shall not pass!</h1></div>
<div class="row"><small style="text-align: center">(You must get at least 85% to be considered the same person)</small><div class="row">
</div>
"""
)
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/unispeech-sat-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 '<b style="color:red">ERROR: Please record audio for *both* speakers!</b>'
wav1, _ = apply_effects_file(path1, EFFECTS)
wav2, _ = apply_effects_file(path2, 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.Audio(source="microphone", type="filepath", optional=True, label="Speaker #1"),
gr.Audio(source="microphone", type="filepath", optional=True, label="Speaker #2"),
]
output = gr.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 = (
"<p style='text-align: center'>"
"<a href='https://huggingface.co/microsoft/unispeech-sat-large-sv' target='_blank'>🎙️ Learn more about UniSpeech-SAT</a> | "
"<a href='https://arxiv.org/abs/2110.05752' target='_blank'>📚 UniSpeech-SAT paper</a> | "
"<a href='https://www.danielpovey.com/files/2018_icassp_xvectors.pdf' target='_blank'>📚 X-Vector paper</a>"
"</p>"
)
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_2.mp3"],
["samples/cate_blanch.mp3", "samples/kirsten_dunst.wav"],
]
demo = gr.Interface(
fn=similarity_fn,
inputs=inputs,
outputs=output,
title="Voice Authentication with UniSpeech-SAT + X-Vectors",
description=description,
article=article,
layout="horizontal",
theme="huggingface",
allow_flagging="never",
live=False,
examples=examples,
)
if __name__ == "__main__":
demo.launch()

View File

@ -2079,20 +2079,20 @@ class Audio(Changeable, Clearable, Playable, Streamable, IOComponent):
file_obj = processing_utils.decode_base64_to_file(
file_data, file_path=file_name
)
if crop_min != 0 or crop_max != 100:
sample_rate, data = processing_utils.audio_from_file(
file_obj.name, crop_min=crop_min, crop_max=crop_max
)
sample_rate, data = processing_utils.audio_from_file(
file_obj.name, crop_min=crop_min, crop_max=crop_max
)
if self.type == "numpy":
return sample_rate, data
elif self.type in ["file", "filepath"]:
processing_utils.audio_to_file(sample_rate, data, file_obj.name)
if self.type == "file":
warnings.warn(
"The 'file' type has been deprecated. Set parameter 'type' to 'filepath' instead.",
)
return file_obj
elif self.type == "filepath":
return file_obj.name
elif self.type == "numpy":
return processing_utils.audio_from_file(file_obj.name)
if self.type == "file":
warnings.warn(
"The 'file' type has been deprecated. Set parameter 'type' to 'filepath' instead.",
)
return file_obj
else:
return file_obj.name
else:
raise ValueError(
"Unknown type: "

File diff suppressed because one or more lines are too long

View File

@ -12,6 +12,7 @@ import numpy as np
import pandas as pd
import PIL
import pytest
from scipy.io import wavfile
import gradio as gr
from gradio import media_data
@ -1902,5 +1903,12 @@ def test_dataframe_postprocess_only_dates():
}
def test_audio_preprocess_can_be_read_by_scipy():
x_wav = deepcopy(media_data.BASE64_MICROPHONE)
audio_input = gr.Audio(type="filepath")
output = audio_input.preprocess(x_wav)
wavfile.read(output)
if __name__ == "__main__":
unittest.main()