Spaces:
Runtime error
Runtime error
speech-test
commited on
Commit
β’
420400a
1
Parent(s):
bcff2ae
First iteration
Browse files- README.md +2 -2
- app.py +82 -0
- requirements.txt +2 -0
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
title: Unispeech Speaker Verification
|
3 |
emoji: π»
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
app_file: app.py
|
8 |
pinned: false
|
|
|
1 |
---
|
2 |
title: Unispeech Speaker Verification
|
3 |
emoji: π»
|
4 |
+
colorFrom: indigo
|
5 |
+
colorTo: blue
|
6 |
sdk: gradio
|
7 |
app_file: app.py
|
8 |
pinned: false
|
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from torchaudio.sox_effects import apply_effects_file
|
4 |
+
from transformers import AutoFeatureExtractor, AutoModelForAudioXVector
|
5 |
+
|
6 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
7 |
+
|
8 |
+
OUTPUT = """
|
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 |
+
<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 |
+
|
17 |
+
EFFECTS = [
|
18 |
+
["channels", "1"],
|
19 |
+
["rate", "16000"],
|
20 |
+
["gain", "-3.0"],
|
21 |
+
["silence", "1", "0.1", "0.1%", "-1", "0.1", "0.1%"],
|
22 |
+
]
|
23 |
+
|
24 |
+
model_name = "anton-l/unispeech-sat-base-plus-sv"
|
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)
|
28 |
+
|
29 |
+
|
30 |
+
def similarity_fn(mic_path1, file_path1, mic_path2, file_path2):
|
31 |
+
if not ((mic_path1 or file_path1) and (mic_path2 or file_path2)):
|
32 |
+
return '<b style="color:red">ERROR: Please record or upload audio for *both* speakers!</b>'
|
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
|
42 |
+
emb2 = model(input2).embeddings
|
43 |
+
emb1 = torch.nn.functional.normalize(emb1, dim=-1).cpu()
|
44 |
+
emb2 = torch.nn.functional.normalize(emb2, dim=-1).cpu()
|
45 |
+
similarity = cosine_sim(emb1, emb2).numpy()[0]
|
46 |
+
|
47 |
+
return OUTPUT.format(similarity * 100)
|
48 |
+
|
49 |
+
|
50 |
+
inputs = [
|
51 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #1"),
|
52 |
+
gr.inputs.Audio(source="upload", type="filepath", optional=True, label="or"),
|
53 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #2"),
|
54 |
+
gr.inputs.Audio(source="upload", type="filepath", optional=True, label="or"),
|
55 |
+
]
|
56 |
+
output = gr.outputs.HTML(label="")
|
57 |
+
|
58 |
+
|
59 |
+
description = (
|
60 |
+
"Speaker Verification demo based on "
|
61 |
+
"UniSpeech-SAT: Universal Speech Representation Learning with Speaker Aware Pre-Training"
|
62 |
+
)
|
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'>π Article on ArXiv</a>"
|
67 |
+
"</p>"
|
68 |
+
)
|
69 |
+
|
70 |
+
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",
|
78 |
+
theme="huggingface",
|
79 |
+
allow_flagging=False,
|
80 |
+
live=False,
|
81 |
+
)
|
82 |
+
interface.launch(enable_queue=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
git+https://github.com/huggingface/transformers
|
2 |
+
torchaudio
|