patrickvonplaten commited on
Commit
ddfaced
Β·
1 Parent(s): 7eb3d7c
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Asv
3
- emoji: πŸ“‰
4
  colorFrom: blue
5
- colorTo: blue
6
  sdk: gradio
7
  app_file: app.py
8
  pinned: false
@@ -12,7 +12,7 @@ pinned: false
12
 
13
  `title`: _string_
14
  Display title for the Space
15
-
16
  `emoji`: _string_
17
  Space emoji (emoji-only character allowed)
18
 
 
1
  ---
2
+ title: Unispeech Speaker Verification
3
+ emoji: πŸ’»
4
  colorFrom: blue
5
+ colorTo: purple
6
  sdk: gradio
7
  app_file: app.py
8
  pinned: false
 
12
 
13
  `title`: _string_
14
  Display title for the Space
15
+
16
  `emoji`: _string_
17
  Space emoji (emoji-only character allowed)
18
 
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ 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 at least 85% 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 at least 85% to be considered the same person)</small><div class="row">
27
+ </div>
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
+
73
+ inputs = [
74
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #1"),
75
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #2"),
76
+ ]
77
+ output = gr.outputs.HTML(label="")
78
+
79
+
80
+ description = (
81
+ "This demo will compare two speech samples and determine if they are from the same speaker. "
82
+ "Try it with your own voice!"
83
+ )
84
+ article = (
85
+ "<p style='text-align: center'>"
86
+ "<a href='https://huggingface.co/microsoft/unispeech-sat-large-sv' 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
+
92
+ interface = gr.Interface(
93
+ fn=similarity_fn,
94
+ inputs=inputs,
95
+ outputs=output,
96
+ title="Voice Authentication with UniSpeech-SAT + X-Vectors",
97
+ description=description,
98
+ article=article,
99
+ layout="horizontal",
100
+ theme="huggingface",
101
+ allow_flagging=False,
102
+ live=False,
103
+ examples=[
104
+ ["samples/cate_blanch.wav", "samples/cate_blanch_2.wav"],
105
+ ["samples/cate_blanch.wav", "samples/cate_blanch_3.wav"],
106
+ ["samples/cate_blanch_2.wav", "samples/cate_blanch_3.wav"],
107
+ ["samples/naomi_watts.wav", "samples/cate_blanch_3.wav"],
108
+ ["samples/naomi_watts.wav", "samples/denzel_wash.wav"],
109
+ ["samples/arnold_schwarz.wav", "samples/denzel_wash.wav"],
110
+ ["samples/arnold_schwarz.wav", "samples/cate_blanch.wav"],
111
+ ["samples/arnold_schwarz.wav", "samples/cate_blanch_2.wav"],
112
+ ["samples/arnold_schwarz.wav", "samples/heath_ledger_2.wav"],
113
+ ["samples/dicaprio_2.wav", "samples/crowe.wav"],
114
+ ["samples/KirstenDunst.wav", "samples/crowe.wav"],
115
+ ["samples/cate_blanch_3.wav", "samples/KirstenDunst.wav"],
116
+ ["samples/Zendaya.wav", "samples/cate_blanch_3.wav"],
117
+ ["samples/dicaprio.wav", "samples/denzel_wash.wav"],
118
+ ["samples/cate_blanch_2.wav", "samples/cate_blanch_3.wav"],
119
+ ["samples/cate_blanch_2.wav", "samples/cate_blanch_3.wav"],
120
+ ["samples/crowe.wav", "samples/crowe_2.wav"],
121
+ ["samples/crowe.wav", "samples/crowe_3.wav"],
122
+ ["samples/dicaprio.wav", "samples/dicaprio_2.wav"],
123
+ ["samples/heath_ledger.wav", "samples/heath_ledger_2.wav"],
124
+ ["samples/heath_ledger.wav", "samples/heath_ledger_3.wav"],
125
+ ["samples/heath_ledger.wav", "samples/heath_ledger_4.wav"],
126
+ ["samples/heath_ledger_3.wav", "samples/heath_ledger_2.wav"],
127
+ ["samples/heath_ledger_4.wav", "samples/heath_ledger_2.wav"],
128
+ ["samples/heath_ledger_3.wav", "samples/heath_ledger_4.wav"],
129
+ ]
130
+ )
131
+ interface.launch(enable_queue=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ git+https://github.com/huggingface/transformers
2
+ torchaudio
samples/KirstenDunst.wav ADDED
Binary file (1.29 MB). View file
 
samples/TobeyMaguire.wav ADDED
Binary file (1.62 MB). View file
 
samples/TomHolland.wav ADDED
Binary file (723 kB). View file
 
samples/Zendaya.wav ADDED
Binary file (1.09 MB). View file
 
samples/arnold_schwarz.wav ADDED
Binary file (13.3 kB). View file
 
samples/cate_blanch.wav ADDED
Binary file (74.7 kB). View file
 
samples/cate_blanch_2.wav ADDED
Binary file (35.1 kB). View file
 
samples/cate_blanch_3.wav ADDED
Binary file (43.7 kB). View file
 
samples/crowe.wav ADDED
Binary file (66.6 kB). View file
 
samples/crowe_2.wav ADDED
Binary file (110 kB). View file
 
samples/crowe_3.wav ADDED
Binary file (52.2 kB). View file
 
samples/denzel_wash.wav ADDED
Binary file (77.5 kB). View file
 
samples/dicaprio.wav ADDED
Binary file (75.5 kB). View file
 
samples/dicaprio_2.wav ADDED
Binary file (32 kB). View file
 
samples/heath_ledger.wav ADDED
Binary file (21.7 kB). View file
 
samples/heath_ledger_2.wav ADDED
Binary file (58.7 kB). View file
 
samples/heath_ledger_3.wav ADDED
Binary file (38.8 kB). View file
 
samples/heath_ledger_4.wav ADDED
Binary file (18.2 kB). View file
 
samples/naomi_watts.wav ADDED
Binary file (23.5 kB). View file