File size: 6,410 Bytes
4fa8cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import gradio as gr
import torch
import random
import whisper
import re
from nemo.collections.asr.models import EncDecSpeakerLabelModel

# from transformers import Wav2Vec2Processor, Wav2Vec2Tokenizer


device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


def audio_to_text(audio):
    model = whisper.load_model("base.en")

    audio = whisper.load_audio(audio)
    result = model.transcribe(audio)

    return result["text"]


random_sentences = [
    "the keep brown",
    "jump over table",
    "green mango fruit",
    "how much money",
    "please audio speaker",
    "nothing is better",
    "garden banana orange",
    "tiger animal king",
    "laptop mouse monitor"
]

additional_random_sentences = [
    "sunrise over mountains"
    "whispering gentle breeze"
    "garden of roses"
    "melodies in rain"
    "laughing with friends"
    "silent midnight moon"
    "skipping in meadow"
    "ocean waves crashing"
    "exploring hidden caves"
    "serenading under stars"
]


# Define a Gradio interface with text inputs for both speakers
def get_random_sentence():
    return random.choice(random_sentences)


text_inputs = [
    gr.inputs.Textbox(label="Speak the Words given below:", default=get_random_sentence, lines=1),
]

STYLE = """
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha256-YvdLHPgkqJ8DVUxjjnGVlMMJtNimJ6dYkowFFvp4kKs=" crossorigin="anonymous">
"""

OUTPUT_ERROR = (
        STYLE
        + """
    <div class="container">
        <div class="row"><h1 style="text-align: center">Spoken Words Did Not Match to the OTP, </h1></div>
        <div class="row"><h1 class="text-danger" style="text-align: center">Please Speak Clearly!!!!</h1></div>
        <div class="row"><h1 class="display-1 text-success" style="text-align: center">Words Spoken 1: {}</h1></div>
        <div class="row"><h1 class="display-1 text-success" style="text-align: center">Words Spoken 2: {}</h1></div>              
    </div>
"""
)

OUTPUT_OK = (
        STYLE
        + """
    <div class="container">
        <div class="row"><h1 style="text-align: center">The provided samples are</h1></div>
        <div class="row"><h1 class="text-success" style="text-align: center">Same Speakers!!!</h1></div>
        <div class="row"><h1 class="text-success" style="text-align: center">Authentication Successfull!!!</h1></div>

    </div>
"""
)
OUTPUT_FAIL = (
        STYLE
        + """
    <div class="container">
        <div class="row"><h1 style="text-align: center">The provided samples are from </h1></div>
        <div class="row"><h1 class="text-danger" style="text-align: center">Different Speakers!!!</h1></div> 
        <div class="row"><h1 class="text-danger" style="text-align: center">Authentication Failed!!!</h1></div>       
    </div>
"""
)

THRESHOLD = 0.80

model_name = "nvidia/speakerverification_en_titanet_large"
model = EncDecSpeakerLabelModel.from_pretrained(model_name).to(device)


def clean_sentence(sentence):
    # Remove commas and full stops using regular expression
    cleaned_sentence = re.sub(r'[,.?!]', '', sentence)
    # Convert the sentence to lowercase
    cleaned_sentence = cleaned_sentence.lower()
    cleaned_sentence = cleaned_sentence.strip()
    return cleaned_sentence


def compare_samples(text, path1, path2):
    if not (path1 and path2):
        return '<b style="color:red">ERROR: Please record audio for *both* speakers!</b>'

    cls1 = audio_to_text(path1)
    cls2 = audio_to_text(path2)
    
    myText = clean_sentence(text)
    Spoken1 = clean_sentence(cls1)
    Spoken2 = clean_sentence(cls2)

    print("OTP Given:", myText)
    print("Spoken 1:", Spoken1)
    print("Spoken 2:", Spoken2)

    if Spoken1 == Spoken2 == myText:
        embs1 = model.get_embedding(path1).squeeze()
        embs2 = model.get_embedding(path2).squeeze()

        # Length Normalize
        X = embs1 / torch.linalg.norm(embs1)
        Y = embs2 / torch.linalg.norm(embs2)

        # Score
        similarity_score = torch.dot(X, Y) / ((torch.dot(X, X) * torch.dot(Y, Y)) ** 0.5)
        similarity_score = (similarity_score + 1) / 2

        # Decision
        if similarity_score >= THRESHOLD:
            return OUTPUT_OK
        else:
            return OUTPUT_FAIL
    else:
        return OUTPUT_ERROR.format(Spoken1, Spoken2)


#
# def compare_samples1(path1, path2):
#     if not (path1 and path2):
#         return '<b style="color:red">ERROR: Please record audio for *both* speakers!</b>'
#
#     embs1 = model.get_embedding(path1).squeeze()
#     embs2 = model.get_embedding(path2).squeeze()
#
#     # Length Normalize
#     X = embs1 / torch.linalg.norm(embs1)
#     Y = embs2 / torch.linalg.norm(embs2)
#
#     # Score
#     similarity_score = torch.dot(X, Y) / ((torch.dot(X, X) * torch.dot(Y, Y)) ** 0.5)
#     similarity_score = (similarity_score + 1) / 2
#
#     # Decision
#     if similarity_score >= THRESHOLD:
#         return OUTPUT_OK.format(similarity_score * 100)
#     else:
#         return OUTPUT_FAIL.format(similarity_score * 100)


inputs = [
    *text_inputs,
    gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #1"),
    gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker #2"),
]

# upload_inputs = [
#     gr.inputs.Audio(source="upload", type="filepath", optional=True, label="Speaker #1"),
#     gr.inputs.Audio(source="upload", type="filepath", optional=True, label="Speaker #2"),
# ]

description = (
    "Compare two speech samples and determine if they are from the same speaker."
)

microphone_interface = gr.Interface(
    fn=compare_samples,
    inputs=inputs,
    outputs=gr.outputs.HTML(label=""),
    title="Speaker Verification",
    description=description,
    layout="horizontal",
    theme="huggingface",
    allow_flagging=False,
    live=False,
)

# upload_interface = gr.Interface(
#     fn=compare_samples1,
#     inputs=upload_inputs,
#     outputs=gr.outputs.HTML(label=""),
#     title="Speaker Verification",
#     description=description,
#     layout="horizontal",
#     theme="huggingface",
#     allow_flagging=False,
#     live=False,
# )

demo = gr.TabbedInterface([microphone_interface, ], ["Microphone", ])
# demo = gr.TabbedInterface([microphone_interface, upload_interface], ["Microphone", "Upload File"])
demo.launch(enable_queue=True, share=True)