Resund commited on
Commit
6d67c6f
Β·
1 Parent(s): 81db8b7

Create new file

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/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
+
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/unispeech-sat-large-sv' target='_blank'>πŸŽ™οΈ Learn more about UniSpeech-SAT</a> | "
93
+ "<a href='https://arxiv.org/abs/2110.05752' target='_blank'>πŸ“š UniSpeech-SAT 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/victor.mp3", "samples/phil.mp3"],
99
+ ]
100
+
101
+ interface = gr.Interface(
102
+ fn=similarity_fn,
103
+ inputs=inputs,
104
+ outputs=output,
105
+ title="Voice Authentication with UniSpeech-SAT + X-Vectors",
106
+ description=description,
107
+ article=article,
108
+ layout="horizontal",
109
+ theme="huggingface",
110
+ allow_flagging=False,
111
+ live=False,
112
+ examples=examples,
113
+ )
114
+ interface.launch(enable_queue=True)