moki09 commited on
Commit
9223220
·
1 Parent(s): 6461bf9

create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import ssl
3
+ ssl._create_default_https_context = ssl._create_unverified_context
4
+
5
+ import gradio as gr
6
+ import wespeaker
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 = (STYLE + """
12
+ <div class="container">
13
+ <div class="row"><h1 style="text-align: center">The speakers are</h1></div>
14
+ <div class="row"><h1 class="display-1 text-success" style="text-align: center">{:.1f}%</h1></div>
15
+ <div class="row"><h1 style="text-align: center">similar</h1></div>
16
+ <div class="row"><h1 class="text-success" style="text-align: center">Welcome, brother!</h1></div>
17
+ </div>
18
+ """)
19
+ OUTPUT_FAIL = (STYLE + """
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">Warning! stranger!</h1></div>
25
+
26
+ </div>
27
+ """)
28
+
29
+ OUTPUT_ERROR = (STYLE + """
30
+ <div class="container">
31
+ <div class="row"><h1 style="text-align: center">Input Error</h1></div>
32
+ <div class="row"><h1 class="text-danger" style="text-align: center">{}!</h1></div>
33
+ </div>
34
+ """)
35
+
36
+ cn_model = wespeaker.load_model("chinese")
37
+ en_model = wespeaker.load_model("english")
38
+
39
+
40
+ def speaker_verification(audio_path1, audio_path2, lang='CN'):
41
+ if audio_path1 == None or audio_path2 == None:
42
+ output = OUTPUT_ERROR.format('Please enter two audios')
43
+ return output
44
+ if lang == 'EN':
45
+ model = en_model
46
+ elif lang == 'CN':
47
+ model = cn_model
48
+ else:
49
+ output = OUTPUT_ERROR.format('Please select a language')
50
+ return output
51
+ cos_score = model.compute_similarity(audio_path1, audio_path2)
52
+
53
+ if cos_score >= 0.83:
54
+ output = OUTPUT_OK.format(cos_score * 100)
55
+ else:
56
+ output = OUTPUT_FAIL.format(cos_score * 100)
57
+
58
+ return output
59
+
60
+
61
+ # input
62
+ inputs = [
63
+ gr.inputs.Audio(source="microphone",
64
+ type="filepath",
65
+ optional=True,
66
+ label='Speaker#1'),
67
+ gr.inputs.Audio(source="microphone",
68
+ type="filepath",
69
+ optional=True,
70
+ label='Speaker#2'),
71
+ gr.Radio(['EN', 'CN'], label='Language'),
72
+ ]
73
+
74
+ output = gr.outputs.HTML(label="")
75
+
76
+ # description
77
+ description = ("<p>VoicePrint Demo ! Note: We recommend that the audio length be greater than 2s !</p>")
78
+
79
+
80
+ examples = [
81
+ ['examples/BAC009S0764W0228.wav', 'examples/BAC009S0764W0328.wav', 'CN'],
82
+ ['examples/BAC009S0913W0133.wav', 'examples/BAC009S0764W0228.wav', 'CN'],
83
+ ['examples/00001_spk1.wav', 'examples/00003_spk2.wav', 'EN'],
84
+ ['examples/00010_spk2.wav', 'examples/00024_spk1.wav', 'EN'],
85
+ ['examples/00001_spk1.wav', 'examples/00024_spk1.wav', 'EN'],
86
+ ['examples/00010_spk2.wav', 'examples/00003_spk2.wav', 'EN'],
87
+ ]
88
+
89
+ interface = gr.Interface(
90
+ fn=speaker_verification,
91
+ inputs=inputs,
92
+ outputs=output,
93
+ title="VoicePrint Recognition-声纹识别",
94
+ description=description,
95
+ examples=examples,
96
+ theme="huggingface",
97
+ )
98
+
99
+ interface.launch(enable_queue=True,server_name="0.0.0.0",server_port=10001)