Spaces:
Runtime error
Runtime error
Commit
·
3442365
0
Parent(s):
Duplicate from abidlabs/same-person-or-different
Browse files- .gitattributes +28 -0
- README.md +7 -0
- app.py +108 -0
- requirements.txt +2 -0
- samples/cate_blanch.mp3 +0 -0
- samples/cate_blanch_2.mp3 +0 -0
- samples/cate_blanch_3.mp3 +0 -0
- samples/denzel_washington.mp3 +0 -0
- samples/heath_ledger.mp3 +0 -0
- samples/heath_ledger_2.mp3 +0 -0
- samples/heath_ledger_3.mp3 +0 -0
- samples/kirsten_dunst.wav +3 -0
- samples/leonardo_dicaprio.mp3 +0 -0
- samples/naomi_watts.mp3 +0 -0
- samples/russel_crowe.mp3 +0 -0
- samples/russel_crowe_2.mp3 +0 -0
.gitattributes
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
20 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
samples/kirsten_dunst.wav filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: same-person-or-different
|
3 |
+
app_file: app.py
|
4 |
+
sdk: gradio
|
5 |
+
sdk_version: 3.32.0
|
6 |
+
duplicated_from: abidlabs/same-person-or-different
|
7 |
+
---
|
app.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
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_OK = (
|
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 text-success" style="text-align: center">{:.1f}%</h1></div>
|
13 |
+
<div class="row"><h1 style="text-align: center">similar</h1></div>
|
14 |
+
<div class="row"><h1 class="text-success" style="text-align: center">Welcome, human!</h1></div>
|
15 |
+
<div class="row"><small style="text-align: center">(You must get at least 85% to be considered the same person)</small><div class="row">
|
16 |
+
</div>
|
17 |
+
"""
|
18 |
+
)
|
19 |
+
OUTPUT_FAIL = (
|
20 |
+
"""
|
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 at least 85% to be considered the same person)</small><div class="row">
|
27 |
+
</div>
|
28 |
+
"""
|
29 |
+
)
|
30 |
+
|
31 |
+
EFFECTS = [
|
32 |
+
["remix", "-"],
|
33 |
+
["channels", "1"],
|
34 |
+
["rate", "16000"],
|
35 |
+
["gain", "-1.0"],
|
36 |
+
["silence", "1", "0.1", "0.1%", "-1", "0.1", "0.1%"],
|
37 |
+
["trim", "0", "10"],
|
38 |
+
]
|
39 |
+
|
40 |
+
THRESHOLD = 0.85
|
41 |
+
|
42 |
+
model_name = "microsoft/unispeech-sat-base-plus-sv"
|
43 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
44 |
+
model = AutoModelForAudioXVector.from_pretrained(model_name).to(device)
|
45 |
+
cosine_sim = torch.nn.CosineSimilarity(dim=-1)
|
46 |
+
|
47 |
+
|
48 |
+
def similarity_fn(path1, path2):
|
49 |
+
if not (path1 and path2):
|
50 |
+
return '<b style="color:red">ERROR: Please record audio for *both* speakers!</b>'
|
51 |
+
|
52 |
+
wav1, _ = apply_effects_file(path1, EFFECTS)
|
53 |
+
wav2, _ = apply_effects_file(path2, EFFECTS)
|
54 |
+
print(wav1.shape, wav2.shape)
|
55 |
+
|
56 |
+
input1 = feature_extractor(wav1.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device)
|
57 |
+
input2 = feature_extractor(wav2.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device)
|
58 |
+
|
59 |
+
with torch.no_grad():
|
60 |
+
emb1 = model(input1).embeddings
|
61 |
+
emb2 = model(input2).embeddings
|
62 |
+
emb1 = torch.nn.functional.normalize(emb1, dim=-1).cpu()
|
63 |
+
emb2 = torch.nn.functional.normalize(emb2, dim=-1).cpu()
|
64 |
+
similarity = cosine_sim(emb1, emb2).numpy()[0]
|
65 |
+
|
66 |
+
if similarity >= THRESHOLD:
|
67 |
+
output = OUTPUT_OK.format(similarity * 100)
|
68 |
+
else:
|
69 |
+
output = OUTPUT_FAIL.format(similarity * 100)
|
70 |
+
|
71 |
+
return output
|
72 |
+
|
73 |
+
|
74 |
+
inputs = [
|
75 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #1"),
|
76 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #2"),
|
77 |
+
]
|
78 |
+
output = gr.outputs.HTML(label="")
|
79 |
+
|
80 |
+
|
81 |
+
description = (
|
82 |
+
"This demo from Microsoft will compare two speech samples and determine if they are from the same speaker. "
|
83 |
+
"Try it with your own voice!"
|
84 |
+
)
|
85 |
+
article = (
|
86 |
+
"<p style='text-align: center'>"
|
87 |
+
"<a href='https://huggingface.co/microsoft/unispeech-sat-large-sv' target='_blank'>🎙️ Learn more about UniSpeech-SAT</a> | "
|
88 |
+
"<a href='https://arxiv.org/abs/2110.05752' target='_blank'>📚 UniSpeech-SAT paper</a> | "
|
89 |
+
"<a href='https://www.danielpovey.com/files/2018_icassp_xvectors.pdf' target='_blank'>📚 X-Vector paper</a>"
|
90 |
+
"</p>"
|
91 |
+
)
|
92 |
+
examples = [
|
93 |
+
["samples/cate_blanch.mp3", "samples/cate_blanch_2.mp3"],
|
94 |
+
["samples/cate_blanch.mp3", "samples/kirsten_dunst.wav"],
|
95 |
+
]
|
96 |
+
|
97 |
+
interface = gr.Interface(
|
98 |
+
fn=similarity_fn,
|
99 |
+
inputs=inputs,
|
100 |
+
outputs=output,
|
101 |
+
description=description,
|
102 |
+
layout="horizontal",
|
103 |
+
theme="huggingface",
|
104 |
+
allow_flagging=False,
|
105 |
+
live=False,
|
106 |
+
examples=examples,
|
107 |
+
)
|
108 |
+
interface.launch(enable_queue=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
git+https://github.com/huggingface/transformers
|
2 |
+
torchaudio
|
samples/cate_blanch.mp3
ADDED
Binary file (67.2 kB). View file
|
|
samples/cate_blanch_2.mp3
ADDED
Binary file (35.8 kB). View file
|
|
samples/cate_blanch_3.mp3
ADDED
Binary file (43.6 kB). View file
|
|
samples/denzel_washington.mp3
ADDED
Binary file (26.6 kB). View file
|
|
samples/heath_ledger.mp3
ADDED
Binary file (28.4 kB). View file
|
|
samples/heath_ledger_2.mp3
ADDED
Binary file (18 kB). View file
|
|
samples/heath_ledger_3.mp3
ADDED
Binary file (55.8 kB). View file
|
|
samples/kirsten_dunst.wav
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7ab77b27959f0f43126c94c4de3baa23b3f92c3c2e26ba322af2d6cd688f3233
|
3 |
+
size 1287798
|
samples/leonardo_dicaprio.mp3
ADDED
Binary file (71.8 kB). View file
|
|
samples/naomi_watts.mp3
ADDED
Binary file (35.3 kB). View file
|
|
samples/russel_crowe.mp3
ADDED
Binary file (52.2 kB). View file
|
|
samples/russel_crowe_2.mp3
ADDED
Binary file (66.6 kB). View file
|
|