Spaces:
Runtime error
Runtime error
Upload with huggingface_hub
Browse files- DESCRIPTION.md +1 -0
- README.md +5 -6
- packages.txt +1 -0
- requirements.txt +3 -0
- run.py +106 -0
- samples/cate_blanch.mp3 +0 -0
- samples/cate_blanch_2.mp3 +0 -0
- samples/heath_ledger.mp3 +0 -0
DESCRIPTION.md
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
This demo identifies if two speakers are the same person using Gradio's Audio and HTML components.
|
README.md
CHANGED
@@ -1,12 +1,11 @@
|
|
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.6
|
8 |
-
app_file:
|
9 |
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
|
2 |
---
|
3 |
+
title: same-person-or-different_main
|
4 |
+
emoji: π₯
|
5 |
+
colorFrom: indigo
|
6 |
colorTo: indigo
|
7 |
sdk: gradio
|
8 |
sdk_version: 3.6
|
9 |
+
app_file: run.py
|
10 |
pinned: false
|
11 |
---
|
|
|
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
ffmpeg
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
git+https://github.com/huggingface/transformers
|
2 |
+
torchaudio
|
3 |
+
https://gradio-main-build.s3.amazonaws.com/c3bec6153737855510542e8154391f328ac72606/gradio-3.6-py3-none-any.whl
|
run.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from torchaudio.sox_effects import apply_effects_file
|
4 |
+
from transformers import AutoFeatureExtractor, AutoModelForAudioXVector
|
5 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
6 |
+
|
7 |
+
OUTPUT_OK = (
|
8 |
+
"""
|
9 |
+
<div class="container">
|
10 |
+
<div class="row"><h1 style="text-align: center">The speakers are</h1></div>
|
11 |
+
<div class="row"><h1 class="display-1 text-success" style="text-align: center">{:.1f}%</h1></div>
|
12 |
+
<div class="row"><h1 style="text-align: center">similar</h1></div>
|
13 |
+
<div class="row"><h1 class="text-success" style="text-align: center">Welcome, human!</h1></div>
|
14 |
+
<div class="row"><small style="text-align: center">(You must get at least 85% to be considered the same person)</small><div class="row">
|
15 |
+
</div>
|
16 |
+
"""
|
17 |
+
)
|
18 |
+
OUTPUT_FAIL = (
|
19 |
+
"""
|
20 |
+
<div class="container">
|
21 |
+
<div class="row"><h1 style="text-align: center">The speakers are</h1></div>
|
22 |
+
<div class="row"><h1 class="display-1 text-danger" style="text-align: center">{:.1f}%</h1></div>
|
23 |
+
<div class="row"><h1 style="text-align: center">similar</h1></div>
|
24 |
+
<div class="row"><h1 class="text-danger" style="text-align: center">You shall not pass!</h1></div>
|
25 |
+
<div class="row"><small style="text-align: center">(You must get at least 85% to be considered the same person)</small><div class="row">
|
26 |
+
</div>
|
27 |
+
"""
|
28 |
+
)
|
29 |
+
|
30 |
+
EFFECTS = [
|
31 |
+
["remix", "-"],
|
32 |
+
["channels", "1"],
|
33 |
+
["rate", "16000"],
|
34 |
+
["gain", "-1.0"],
|
35 |
+
["silence", "1", "0.1", "0.1%", "-1", "0.1", "0.1%"],
|
36 |
+
["trim", "0", "10"],
|
37 |
+
]
|
38 |
+
|
39 |
+
THRESHOLD = 0.85
|
40 |
+
|
41 |
+
model_name = "microsoft/unispeech-sat-base-plus-sv"
|
42 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
43 |
+
model = AutoModelForAudioXVector.from_pretrained(model_name).to(device)
|
44 |
+
cosine_sim = torch.nn.CosineSimilarity(dim=-1)
|
45 |
+
|
46 |
+
|
47 |
+
def similarity_fn(path1, path2):
|
48 |
+
if not (path1 and path2):
|
49 |
+
return '<b style="color:red">ERROR: Please record audio for *both* speakers!</b>'
|
50 |
+
|
51 |
+
wav1, _ = apply_effects_file(path1, EFFECTS)
|
52 |
+
wav2, _ = apply_effects_file(path2, EFFECTS)
|
53 |
+
print(wav1.shape, wav2.shape)
|
54 |
+
|
55 |
+
input1 = feature_extractor(wav1.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device)
|
56 |
+
input2 = feature_extractor(wav2.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device)
|
57 |
+
|
58 |
+
with torch.no_grad():
|
59 |
+
emb1 = model(input1).embeddings
|
60 |
+
emb2 = model(input2).embeddings
|
61 |
+
emb1 = torch.nn.functional.normalize(emb1, dim=-1).cpu()
|
62 |
+
emb2 = torch.nn.functional.normalize(emb2, dim=-1).cpu()
|
63 |
+
similarity = cosine_sim(emb1, emb2).numpy()[0]
|
64 |
+
|
65 |
+
if similarity >= THRESHOLD:
|
66 |
+
output = OUTPUT_OK.format(similarity * 100)
|
67 |
+
else:
|
68 |
+
output = OUTPUT_FAIL.format(similarity * 100)
|
69 |
+
|
70 |
+
return output
|
71 |
+
|
72 |
+
inputs = [
|
73 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #1"),
|
74 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #2"),
|
75 |
+
]
|
76 |
+
output = gr.outputs.HTML(label="")
|
77 |
+
|
78 |
+
|
79 |
+
description = (
|
80 |
+
"This demo from Microsoft will compare two speech samples and determine if they are from the same speaker. "
|
81 |
+
"Try it with your own voice!"
|
82 |
+
)
|
83 |
+
article = (
|
84 |
+
"<p style='text-align: center'>"
|
85 |
+
"<a href='https://huggingface.co/microsoft/unispeech-sat-large-sv' target='_blank'>ποΈ Learn more about UniSpeech-SAT</a> | "
|
86 |
+
"<a href='https://arxiv.org/abs/2110.05752' target='_blank'>π UniSpeech-SAT paper</a> | "
|
87 |
+
"<a href='https://www.danielpovey.com/files/2018_icassp_xvectors.pdf' target='_blank'>π X-Vector paper</a>"
|
88 |
+
"</p>"
|
89 |
+
)
|
90 |
+
examples = [
|
91 |
+
["samples/cate_blanch.mp3", "samples/cate_blanch_2.mp3"],
|
92 |
+
["samples/cate_blanch.mp3", "samples/heath_ledger.mp3"],
|
93 |
+
]
|
94 |
+
|
95 |
+
interface = gr.Interface(
|
96 |
+
fn=similarity_fn,
|
97 |
+
inputs=inputs,
|
98 |
+
outputs=output,
|
99 |
+
layout="horizontal",
|
100 |
+
theme="huggingface",
|
101 |
+
allow_flagging=False,
|
102 |
+
live=False,
|
103 |
+
examples=examples,
|
104 |
+
cache_examples=False
|
105 |
+
)
|
106 |
+
interface.launch()
|
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/heath_ledger.mp3
ADDED
Binary file (28.4 kB). View file
|
|