Bagus commited on
Commit
7a9eea4
β€’
1 Parent(s): 3c25873

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/[email protected]/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_3.mp3"],
102
+ ["samples/russel_crowe.mp3", "samples/russel_crowe_2.mp3"],
103
+ ["samples/cate_blanch.mp3", "samples/kirsten_dunst.wav"],
104
+ ["samples/russel_crowe.mp3", "samples/kirsten_dunst.wav"],
105
+ ["samples/russel_crowe_2.mp3", "samples/kirsten_dunst.wav"],
106
+ ["samples/heath_ledger.mp3", "samples/denzel_washington.mp3"],
107
+ ["samples/leonardo_dicaprio.mp3", "samples/russel_crowe.mp3"],
108
+ ["samples/leonardo_dicaprio.mp3", "samples/russel_crowe_2.mp3"],
109
+ ["samples/naomi_watts.mp3", "samples/denzel_washington.mp3"],
110
+ ["samples/naomi_watts.mp3", "samples/leonardo_dicaprio.mp3"],
111
+ ["samples/naomi_watts.mp3", "samples/cate_blanch_2.mp3"],
112
+ ["samples/naomi_watts.mp3", "samples/kirsten_dunst.wav"],
113
+ ]
114
+
115
+ interface = gr.Interface(
116
+ fn=similarity_fn,
117
+ inputs=inputs,
118
+ outputs=output,
119
+ title="Voice Authentication with WavLM + X-Vectors",
120
+ description=description,
121
+ article=article,
122
+ layout="horizontal",
123
+ theme="huggingface",
124
+ allow_flagging=False,
125
+ live=False,
126
+ examples=examples,
127
+ )
128
+ interface.launch(enable_queue=True)