aliabd HF staff commited on
Commit
f7fd586
β€’
1 Parent(s): 7ed7cde

Upload with huggingface_hub

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