Spaces:
Runtime error
Runtime error
Commit
Β·
7275eb6
1
Parent(s):
420400a
Styling
Browse files
app.py
CHANGED
|
@@ -5,12 +5,25 @@ from transformers import AutoFeatureExtractor, AutoModelForAudioXVector
|
|
| 5 |
|
| 6 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
<div class="container">
|
| 11 |
<div class="row"><h1 style="text-align: center">The speakers are</h1></div>
|
| 12 |
-
<div class="row"><h1 class="display-1" style="text-align: center">{:.1f}%</h1></div>
|
| 13 |
<div class="row"><h1 style="text-align: center">similar</h1></div>
|
|
|
|
|
|
|
| 14 |
</div>
|
| 15 |
"""
|
| 16 |
|
|
@@ -21,7 +34,9 @@ EFFECTS = [
|
|
| 21 |
["silence", "1", "0.1", "0.1%", "-1", "0.1", "0.1%"],
|
| 22 |
]
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
| 26 |
model = AutoModelForAudioXVector.from_pretrained(model_name).to(device)
|
| 27 |
cosine_sim = torch.nn.CosineSimilarity(dim=-1)
|
|
@@ -33,9 +48,10 @@ def similarity_fn(mic_path1, file_path1, mic_path2, file_path2):
|
|
| 33 |
|
| 34 |
wav1, _ = apply_effects_file(mic_path1 if mic_path1 else file_path1, EFFECTS)
|
| 35 |
wav2, _ = apply_effects_file(mic_path2 if mic_path2 else file_path2, EFFECTS)
|
|
|
|
| 36 |
|
| 37 |
-
input1 = feature_extractor(wav1.squeeze(0), return_tensors="pt").input_values.to(device)
|
| 38 |
-
input2 = feature_extractor(wav2.squeeze(0), return_tensors="pt").input_values.to(device)
|
| 39 |
|
| 40 |
with torch.no_grad():
|
| 41 |
emb1 = model(input1).embeddings
|
|
@@ -44,7 +60,12 @@ def similarity_fn(mic_path1, file_path1, mic_path2, file_path2):
|
|
| 44 |
emb2 = torch.nn.functional.normalize(emb2, dim=-1).cpu()
|
| 45 |
similarity = cosine_sim(emb1, emb2).numpy()[0]
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
inputs = [
|
|
@@ -63,7 +84,8 @@ description = (
|
|
| 63 |
article = (
|
| 64 |
"<p style='text-align: center'>"
|
| 65 |
"<a href='https://huggingface.co/microsoft/unispeech-sat-large' target='_blank'>ποΈ Learn more about UniSpeech-SAT</a> | "
|
| 66 |
-
"<a href='https://arxiv.org/abs/2110.05752' target='_blank'>π
|
|
|
|
| 67 |
"</p>"
|
| 68 |
)
|
| 69 |
|
|
@@ -71,7 +93,7 @@ interface = gr.Interface(
|
|
| 71 |
fn=similarity_fn,
|
| 72 |
inputs=inputs,
|
| 73 |
outputs=output,
|
| 74 |
-
title="Speaker Verification with UniSpeech-SAT",
|
| 75 |
description=description,
|
| 76 |
article=article,
|
| 77 |
layout="horizontal",
|
|
|
|
| 5 |
|
| 6 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 7 |
|
| 8 |
+
STYLE = """
|
| 9 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha256-YvdLHPgkqJ8DVUxjjnGVlMMJtNimJ6dYkowFFvp4kKs=" crossorigin="anonymous">
|
| 10 |
+
"""
|
| 11 |
+
OUTPUT_OK = STYLE + """
|
| 12 |
+
<div class="container">
|
| 13 |
+
<div class="row"><h1 style="text-align: center">The speakers are</h1></div>
|
| 14 |
+
<div class="row"><h1 class="display-1 text-success" style="text-align: center">{:.1f}%</h1></div>
|
| 15 |
+
<div class="row"><h1 style="text-align: center">similar</h1></div>
|
| 16 |
+
<div class="row"><h1 class="text-success" style="text-align: center">Welcome, human!</h1></div>
|
| 17 |
+
<div class="row"><small style="text-align: center">(You must get 89% or more to be considered the same person)</small><div class="row">
|
| 18 |
+
</div>
|
| 19 |
+
"""
|
| 20 |
+
OUTPUT_FAIL = STYLE + """
|
| 21 |
<div class="container">
|
| 22 |
<div class="row"><h1 style="text-align: center">The speakers are</h1></div>
|
| 23 |
+
<div class="row"><h1 class="display-1 text-danger" style="text-align: center">{:.1f}%</h1></div>
|
| 24 |
<div class="row"><h1 style="text-align: center">similar</h1></div>
|
| 25 |
+
<div class="row"><h1 class="text-danger" style="text-align: center">You shall not pass!</h1></div>
|
| 26 |
+
<div class="row"><small style="text-align: center">(You must get 89% or more to be considered the same person)</small><div class="row">
|
| 27 |
</div>
|
| 28 |
"""
|
| 29 |
|
|
|
|
| 34 |
["silence", "1", "0.1", "0.1%", "-1", "0.1", "0.1%"],
|
| 35 |
]
|
| 36 |
|
| 37 |
+
THRESHOLD = 0.89
|
| 38 |
+
|
| 39 |
+
model_name = "microsoft/unispeech-sat-base-plus-sv"
|
| 40 |
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
| 41 |
model = AutoModelForAudioXVector.from_pretrained(model_name).to(device)
|
| 42 |
cosine_sim = torch.nn.CosineSimilarity(dim=-1)
|
|
|
|
| 48 |
|
| 49 |
wav1, _ = apply_effects_file(mic_path1 if mic_path1 else file_path1, EFFECTS)
|
| 50 |
wav2, _ = apply_effects_file(mic_path2 if mic_path2 else file_path2, EFFECTS)
|
| 51 |
+
print(wav1.shape, wav2.shape)
|
| 52 |
|
| 53 |
+
input1 = feature_extractor(wav1.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device)
|
| 54 |
+
input2 = feature_extractor(wav2.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device)
|
| 55 |
|
| 56 |
with torch.no_grad():
|
| 57 |
emb1 = model(input1).embeddings
|
|
|
|
| 60 |
emb2 = torch.nn.functional.normalize(emb2, dim=-1).cpu()
|
| 61 |
similarity = cosine_sim(emb1, emb2).numpy()[0]
|
| 62 |
|
| 63 |
+
if similarity >= THRESHOLD:
|
| 64 |
+
output = OUTPUT_OK.format(similarity * 100)
|
| 65 |
+
else:
|
| 66 |
+
output = OUTPUT_FAIL.format(similarity * 100)
|
| 67 |
+
|
| 68 |
+
return output
|
| 69 |
|
| 70 |
|
| 71 |
inputs = [
|
|
|
|
| 84 |
article = (
|
| 85 |
"<p style='text-align: center'>"
|
| 86 |
"<a href='https://huggingface.co/microsoft/unispeech-sat-large' target='_blank'>ποΈ Learn more about UniSpeech-SAT</a> | "
|
| 87 |
+
"<a href='https://arxiv.org/abs/2110.05752' target='_blank'>π UniSpeech-SAT paper</a> | "
|
| 88 |
+
"<a href='https://www.danielpovey.com/files/2018_icassp_xvectors.pdf' target='_blank'>π X-Vector paper</a>"
|
| 89 |
"</p>"
|
| 90 |
)
|
| 91 |
|
|
|
|
| 93 |
fn=similarity_fn,
|
| 94 |
inputs=inputs,
|
| 95 |
outputs=output,
|
| 96 |
+
title="Speaker Verification with UniSpeech-SAT + X-Vectors",
|
| 97 |
description=description,
|
| 98 |
article=article,
|
| 99 |
layout="horizontal",
|