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