chakshudhannawat commited on
Commit
04cdce2
Β·
1 Parent(s): 101a944

Deployment

Browse files
README.md CHANGED
@@ -1,12 +1,39 @@
1
  ---
2
- title: WavLM SpeakerVerification
3
- emoji: πŸš€
4
- colorFrom: pink
5
- colorTo: green
6
  sdk: gradio
7
- sdk_version: 4.24.0
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
+ title: Speaker Verification Demo
3
+ emoji: 😻
4
+ colorFrom: yellow
5
+ colorTo: red
6
  sdk: gradio
 
7
  app_file: app.py
8
  pinned: false
9
  ---
10
 
11
+ # Configuration
12
+
13
+ title: string
14
+ Display title for the Space
15
+
16
+ emoji: string
17
+ Space emoji (emoji-only character allowed)
18
+
19
+ colorFrom: string
20
+ Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
21
+
22
+ colorTo: string
23
+ Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
24
+
25
+ sdk: string
26
+ Can be either gradio or streamlit
27
+
28
+ sdk_version : string
29
+ Only applicable for streamlit SDK.
30
+ See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
31
+
32
+
33
+ app_file: string
34
+ Path to your main application file (which contains either gradio or streamlit Python code).
35
+ Path is relative to the root of the repository.
36
+
37
+
38
+ pinned: boolean Whether the Space stays on top of your list.
39
+
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torchaudio
4
+ # from torchaudio.sox_effects import apply_effects_file
5
+ from transformers import AutoFeatureExtractor, AutoModelForAudioXVector
6
+
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+
9
+ STYLE = """
10
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha256-YvdLHPgkqJ8DVUxjjnGVlMMJtNimJ6dYkowFFvp4kKs=" crossorigin="anonymous">
11
+ """
12
+ OUTPUT_OK = (
13
+ STYLE
14
+ + """
15
+ <div class="container">
16
+ <div class="row"><h1 style="text-align: center">The speakers are</h1></div>
17
+ <div class="row"><h1 class="display-1 text-success" style="text-align: center">{:.1f}%</h1></div>
18
+ <div class="row"><h1 style="text-align: center">similar</h1></div>
19
+ <div class="row"><h1 class="text-success" style="text-align: center">Welcome, human!</h1></div>
20
+ <div class="row"><small style="text-align: center">(You must get at least 80% to be considered the same person)</small><div class="row">
21
+ </div>
22
+ """
23
+ )
24
+ OUTPUT_FAIL = (
25
+ STYLE
26
+ + """
27
+ <div class="container">
28
+ <div class="row"><h1 style="text-align: center">The speakers are</h1></div>
29
+ <div class="row"><h1 class="display-1 text-danger" style="text-align: center">{:.1f}%</h1></div>
30
+ <div class="row"><h1 style="text-align: center">similar</h1></div>
31
+ <div class="row"><h1 class="text-danger" style="text-align: center">You shall not pass!</h1></div>
32
+ <div class="row"><small style="text-align: center">(You must get at least 80% to be considered the same person)</small><div class="row">
33
+ </div>
34
+ """
35
+ )
36
+
37
+ EFFECTS = [
38
+ ["remix", "-"],
39
+ ["channels", "1"],
40
+ ["rate", "16000"],
41
+ ["gain", "-1.0"],
42
+ ["silence", "1", "0.1", "0.1%", "-1", "0.1", "0.1%"],
43
+ ["trim", "0", "10"],
44
+ ]
45
+
46
+ THRESHOLD = 0.80
47
+
48
+ model_name = "microsoft/wavlm-base-plus-sv"
49
+ feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
50
+ model = AutoModelForAudioXVector.from_pretrained(model_name).to(device)
51
+ cosine_sim = torch.nn.CosineSimilarity(dim=-1)
52
+
53
+
54
+ def similarity_fn(path1, path2):
55
+ if not (path1 and path2):
56
+ return '<b style="color:red">ERROR: Please record audio for *both* speakers!</b>'
57
+
58
+ # wav1, _ = apply_effects_file(path1, EFFECTS)
59
+ # wav2, _ = apply_effects_file(path2, EFFECTS)
60
+ wav1, _ = torchaudio.load(path1)
61
+ wav2, _ = torchaudio.load(path2)
62
+ print(wav1.shape, wav2.shape)
63
+
64
+ input1 = feature_extractor(wav1.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device)
65
+ input2 = feature_extractor(wav2.squeeze(0), return_tensors="pt", sampling_rate=16000).input_values.to(device)
66
+
67
+ with torch.no_grad():
68
+ emb1 = model(input1).embeddings
69
+ emb2 = model(input2).embeddings
70
+ emb1 = torch.nn.functional.normalize(emb1, dim=-1).cpu()
71
+ emb2 = torch.nn.functional.normalize(emb2, dim=-1).cpu()
72
+ similarity = cosine_sim(emb1, emb2).numpy()[0]
73
+
74
+ if similarity >= THRESHOLD:
75
+ output = OUTPUT_OK.format(similarity * 100)
76
+ else:
77
+ output = OUTPUT_FAIL.format(similarity * 100)
78
+
79
+ return output
80
+
81
+
82
+ inputs = [
83
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #1"),
84
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #2"),
85
+ ]
86
+ output = gr.outputs.HTML(label="")
87
+
88
+
89
+ description = (
90
+ "This demo will compare two speech samples and determine if they are from the same speaker. "
91
+ "Try it with your own voice!"
92
+ )
93
+ article = (
94
+ "<p style='text-align: center'>"
95
+ "<a href='https://huggingface.co/microsoft/wavlm-base-plus-sv' target='_blank'>πŸŽ™οΈ Learn more about WavLM</a> | "
96
+ "<a href='https://arxiv.org/abs/2110.13900' target='_blank'>πŸ“š WavLM paper</a> | "
97
+ "<a href='https://www.danielpovey.com/files/2018_icassp_xvectors.pdf' target='_blank'>πŸ“š X-Vector paper</a>"
98
+ "</p>"
99
+ )
100
+ examples = [
101
+ ["samples/denzel_washington.mp3", "samples/denzel_washington.mp3"],
102
+ ["samples/heath_ledger_2.mp3", "samples/heath_ledger_3.mp3"],
103
+ ["samples/heath_ledger_3.mp3", "samples/denzel_washington.mp3"],
104
+ ["samples/denzel_washington.mp3", "samples/heath_ledger_2.mp3"],
105
+ ]
106
+
107
+ interface = gr.Interface(
108
+ fn=similarity_fn,
109
+ inputs=inputs,
110
+ outputs=output,
111
+ title="Voice Authentication with WavLM + X-Vectors",
112
+ description=description,
113
+ article=article,
114
+ layout="horizontal",
115
+ theme="huggingface",
116
+ allow_flagging=False,
117
+ live=False,
118
+ examples=examples,
119
+ )
120
+ interface.launch(enable_queue=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ git+https://github.com/huggingface/transformers
2
+ torchaudio
3
+
samples/denzel_washington.mp3 ADDED
Binary file (26.6 kB). View file
 
samples/heath_ledger_2.mp3 ADDED
Binary file (18 kB). View file
 
samples/heath_ledger_3.mp3 ADDED
Binary file (28.4 kB). View file