aliabd HF staff commited on
Commit
9427306
β€’
1 Parent(s): 718cf4c

Upload with huggingface_hub

Browse files
.gitattributes CHANGED
@@ -29,3 +29,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
29
  *.zip filter=lfs diff=lfs merge=lfs -text
30
  *.zst filter=lfs diff=lfs merge=lfs -text
31
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
29
  *.zip filter=lfs diff=lfs merge=lfs -text
30
  *.zst filter=lfs diff=lfs merge=lfs -text
31
  *tfevents* filter=lfs diff=lfs merge=lfs -text
32
+ samples/kirsten_dunst.wav filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: Unispeech Speaker Verification
3
- emoji: 🐨
4
- colorFrom: pink
5
- colorTo: green
6
  sdk: gradio
7
  sdk_version: 3.3.1
 
8
  app_file: app.py
9
  pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+
2
  ---
3
+ title: unispeech-speaker-verification
4
+ emoji: πŸ”₯
5
+ colorFrom: indigo
6
+ colorTo: indigo
7
  sdk: gradio
8
  sdk_version: 3.3.1
9
+
10
  app_file: app.py
11
  pinned: false
12
  ---
 
 
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ 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 = (
12
+ STYLE
13
+ + """
14
+ <div class="container">
15
+ <div class="row"><h1 style="text-align: center">The speakers are</h1></div>
16
+ <div class="row"><h1 class="display-1 text-success" style="text-align: center">{:.1f}%</h1></div>
17
+ <div class="row"><h1 style="text-align: center">similar</h1></div>
18
+ <div class="row"><h1 class="text-success" style="text-align: center">Welcome, human!</h1></div>
19
+ <div class="row"><small style="text-align: center">(You must get at least 85% to be considered the same person)</small><div class="row">
20
+ </div>
21
+ """
22
+ )
23
+ OUTPUT_FAIL = (
24
+ STYLE
25
+ + """
26
+ <div class="container">
27
+ <div class="row"><h1 style="text-align: center">The speakers are</h1></div>
28
+ <div class="row"><h1 class="display-1 text-danger" style="text-align: center">{:.1f}%</h1></div>
29
+ <div class="row"><h1 style="text-align: center">similar</h1></div>
30
+ <div class="row"><h1 class="text-danger" style="text-align: center">You shall not pass!</h1></div>
31
+ <div class="row"><small style="text-align: center">(You must get at least 85% to be considered the same person)</small><div class="row">
32
+ </div>
33
+ """
34
+ )
35
+
36
+ EFFECTS = [
37
+ ["remix", "-"],
38
+ ["channels", "1"],
39
+ ["rate", "16000"],
40
+ ["gain", "-1.0"],
41
+ ["silence", "1", "0.1", "0.1%", "-1", "0.1", "0.1%"],
42
+ ["trim", "0", "10"],
43
+ ]
44
+
45
+ THRESHOLD = 0.85
46
+
47
+ model_name = "microsoft/unispeech-sat-base-plus-sv"
48
+ feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
49
+ model = AutoModelForAudioXVector.from_pretrained(model_name).to(device)
50
+ cosine_sim = torch.nn.CosineSimilarity(dim=-1)
51
+
52
+
53
+ def similarity_fn(path1, path2):
54
+ if not (path1 and path2):
55
+ return '<b style="color:red">ERROR: Please record audio for *both* speakers!</b>'
56
+ wav1, _ = apply_effects_file(path1, EFFECTS)
57
+ wav2, _ = apply_effects_file(path2, EFFECTS)
58
+ print(wav1.shape, wav2.shape)
59
+
60
+ input1 = feature_extractor(wav1.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device)
61
+ input2 = feature_extractor(wav2.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device)
62
+
63
+ with torch.no_grad():
64
+ emb1 = model(input1).embeddings
65
+ emb2 = model(input2).embeddings
66
+ emb1 = torch.nn.functional.normalize(emb1, dim=-1).cpu()
67
+ emb2 = torch.nn.functional.normalize(emb2, dim=-1).cpu()
68
+ similarity = cosine_sim(emb1, emb2).numpy()[0]
69
+
70
+ if similarity >= THRESHOLD:
71
+ output = OUTPUT_OK.format(similarity * 100)
72
+ else:
73
+ output = OUTPUT_FAIL.format(similarity * 100)
74
+
75
+ return output
76
+
77
+
78
+ inputs = [
79
+ gr.Audio(source="microphone", type="filepath", optional=True, label="Speaker #1"),
80
+ gr.Audio(source="microphone", type="filepath", optional=True, label="Speaker #2"),
81
+ ]
82
+ output = gr.HTML(label="")
83
+
84
+
85
+ description = (
86
+ "This demo will compare two speech samples and determine if they are from the same speaker. "
87
+ "Try it with your own voice!"
88
+ )
89
+ article = (
90
+ "<p style='text-align: center'>"
91
+ "<a href='https://huggingface.co/microsoft/unispeech-sat-large-sv' target='_blank'>πŸŽ™οΈ Learn more about UniSpeech-SAT</a> | "
92
+ "<a href='https://arxiv.org/abs/2110.05752' target='_blank'>πŸ“š UniSpeech-SAT paper</a> | "
93
+ "<a href='https://www.danielpovey.com/files/2018_icassp_xvectors.pdf' target='_blank'>πŸ“š X-Vector paper</a>"
94
+ "</p>"
95
+ )
96
+ examples = [
97
+ ["samples/cate_blanch.mp3", "samples/cate_blanch_2.mp3"],
98
+ ["samples/cate_blanch.mp3", "samples/cate_blanch_3.mp3"],
99
+ ["samples/cate_blanch_2.mp3", "samples/cate_blanch_3.mp3"],
100
+ ["samples/heath_ledger.mp3", "samples/heath_ledger_2.mp3"],
101
+ ["samples/cate_blanch.mp3", "samples/kirsten_dunst.wav"],
102
+ ]
103
+
104
+ demo = gr.Interface(
105
+ fn=similarity_fn,
106
+ inputs=inputs,
107
+ outputs=output,
108
+ title="Voice Authentication with UniSpeech-SAT + X-Vectors",
109
+ description=description,
110
+ article=article,
111
+ layout="horizontal",
112
+ theme="huggingface",
113
+ allow_flagging="never",
114
+ live=False,
115
+ examples=examples,
116
+ )
117
+
118
+ if __name__ == "__main__":
119
+ demo.launch()
120
+
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/heath_ledger.mp3 ADDED
Binary file (28.4 kB). View file
 
samples/heath_ledger_2.mp3 ADDED
Binary file (18 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