File size: 2,207 Bytes
40a86aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from nemo.collections.asr.models import EncDecSpeakerLabelModel
import json

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

THRESHOLD = 0.60

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

def create_voice_print(audio):
    if not audio:
        return json.dumps({ "error": "no se proporciono un audio"})
    
    embs1 = model.get_embedding(audio).squeeze()

    X = embs1 / torch.linalg.norm(embs1)
    
    # return X.tolist()
    return X

def compare_voice_print(X, Y):
    # Score
    similarity_score = torch.dot(X, Y) / ((torch.dot(X, X) * torch.dot(Y, Y)) ** 0.5)
    similarity_score = (similarity_score + 1) / 2
    return similarity_score.item()

# encontrar como ejecutar la huella de voz
def find_matches(file, voice_print):
    matches = []
    if not file:
        return json.dumps({"error": "No se proporcionó un archivo JSON"})
    
    try:

        json_content = json.load(open(file))
    except json.JSONDecodeError:
        return json.dumps({"error": "El archivo JSON no es válido"})

    data = json_content.get("data", [])

    # Convertir a tensor
    voice_print = torch.tensor(json.loads(voice_print))

    for speaker in data:
        speaker_voice_print = torch.tensor(json.loads(speaker['voice_print']))
        # speaker_voice_print = eval(speaker['voice_print'])
        similarity_score = compare_voice_print(voice_print, speaker_voice_print)
        print(similarity_score)
        if similarity_score >= THRESHOLD:
            matches.append({ "speaker": speaker, "similarity_score": similarity_score })
    
    matches.sort(key=lambda match: match['similarity_score'], reverse=True)
    return matches[:3]


voice_print_maker = gr.Interface(
    fn=create_voice_print,
    inputs=[gr.Audio(type="filepath")],
    outputs=gr.JSON(),
)

voice_prints_loader = gr.Interface(
    fn=find_matches,
    inputs=[
        gr.File(type="filepath", label="Upload JSON file"),
        gr.TextArea()
    ],
    outputs=gr.JSON(),
)

demo = gr.TabbedInterface([voice_print_maker, voice_prints_loader], ["app", "loader"])

demo.launch()