speech-test commited on
Commit
138b689
1 Parent(s): 04fa8a7
.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Datasource local storage ignored files
5
+ /dataSources/
6
+ /dataSources.local.xml
7
+ # Editor-based HTTP Client requests
8
+ /httpRequests/
.idea/inspectionProfiles/profiles_settings.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <component name="InspectionProjectProfileManager">
2
+ <settings>
3
+ <option name="USE_PROJECT_PROFILE" value="false" />
4
+ <version value="1.0" />
5
+ </settings>
6
+ </component>
.idea/misc.xml ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
4
+ </project>
.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/wavlm-speaker-verification.iml" filepath="$PROJECT_DIR$/.idea/wavlm-speaker-verification.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
.idea/wavlm-speaker-verification.iml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="PYTHON_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$" />
5
+ <orderEntry type="inheritedJdk" />
6
+ <orderEntry type="sourceFolder" forTests="false" />
7
+ </component>
8
+ </module>
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Wavlm Speaker Verification
3
- emoji: 📊
4
- colorFrom: purple
5
- colorTo: green
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: WavLM Speaker Verification
3
+ emoji: 🗣️
4
+ colorFrom: yellow
5
+ colorTo: red
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,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/wavlm-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
+
57
+ wav1, _ = apply_effects_file(path1, EFFECTS)
58
+ wav2, _ = apply_effects_file(path2, EFFECTS)
59
+ print(wav1.shape, wav2.shape)
60
+
61
+ input1 = feature_extractor(wav1.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device)
62
+ input2 = feature_extractor(wav2.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device)
63
+
64
+ with torch.no_grad():
65
+ emb1 = model(input1).embeddings
66
+ emb2 = model(input2).embeddings
67
+ emb1 = torch.nn.functional.normalize(emb1, dim=-1).cpu()
68
+ emb2 = torch.nn.functional.normalize(emb2, dim=-1).cpu()
69
+ similarity = cosine_sim(emb1, emb2).numpy()[0]
70
+
71
+ if similarity >= THRESHOLD:
72
+ output = OUTPUT_OK.format(similarity * 100)
73
+ else:
74
+ output = OUTPUT_FAIL.format(similarity * 100)
75
+
76
+ return output
77
+
78
+
79
+ inputs = [
80
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #1"),
81
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #2"),
82
+ ]
83
+ output = gr.outputs.HTML(label="")
84
+
85
+
86
+ description = (
87
+ "This demo will compare two speech samples and determine if they are from the same speaker. "
88
+ "Try it with your own voice!"
89
+ )
90
+ article = (
91
+ "<p style='text-align: center'>"
92
+ "<a href='https://huggingface.co/microsoft/wavlm-base-plus-sv' target='_blank'>🎙️ Learn more about WavLM</a> | "
93
+ "<a href='https://arxiv.org/abs/2110.13900' target='_blank'>📚 WavLM paper</a> | "
94
+ "<a href='https://www.danielpovey.com/files/2018_icassp_xvectors.pdf' target='_blank'>📚 X-Vector paper</a>"
95
+ "</p>"
96
+ )
97
+ examples = [
98
+ ["samples/cate_blanch.mp3", "samples/cate_blanch_2.mp3"],
99
+ ["samples/cate_blanch.mp3", "samples/cate_blanch_3.mp3"],
100
+ ["samples/cate_blanch_2.mp3", "samples/cate_blanch_3.mp3"],
101
+ ["samples/heath_ledger.mp3", "samples/heath_ledger_2.mp3"],
102
+ ["samples/heath_ledger.mp3", "samples/heath_ledger_3.mp3"],
103
+ ["samples/heath_ledger_2.mp3", "samples/heath_ledger_3.mp3"],
104
+ ["samples/russel_crowe.mp3", "samples/russel_crowe_2.mp3"],
105
+ ["samples/cate_blanch.mp3", "samples/kirsten_dunst.wav"],
106
+ ["samples/russel_crowe.mp3", "samples/kirsten_dunst.wav"],
107
+ ["samples/russel_crowe_2.mp3", "samples/kirsten_dunst.wav"],
108
+ ["samples/leonardo_dicaprio.mp3", "samples/denzel_washington.mp3"],
109
+ ["samples/heath_ledger.mp3", "samples/denzel_washington.mp3"],
110
+ ["samples/heath_ledger_2.mp3", "samples/denzel_washington.mp3"],
111
+ ["samples/leonardo_dicaprio.mp3", "samples/russel_crowe.mp3"],
112
+ ["samples/leonardo_dicaprio.mp3", "samples/russel_crowe_2.mp3"],
113
+ ["samples/naomi_watts.mp3", "samples/denzel_washington.mp3"],
114
+ ["samples/naomi_watts.mp3", "samples/leonardo_dicaprio.mp3"],
115
+ ["samples/naomi_watts.mp3", "samples/cate_blanch_2.mp3"],
116
+ ["samples/naomi_watts.mp3", "samples/kirsten_dunst.wav"],
117
+ ]
118
+
119
+ interface = gr.Interface(
120
+ fn=similarity_fn,
121
+ inputs=inputs,
122
+ outputs=output,
123
+ title="Voice Authentication with WavLM + X-Vectors",
124
+ description=description,
125
+ article=article,
126
+ layout="horizontal",
127
+ theme="huggingface",
128
+ allow_flagging=False,
129
+ live=False,
130
+ examples=examples,
131
+ )
132
+ 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
Binary file (1.29 MB). View file
 
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