-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module for finding context similarity between audio files
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import deepspeech | ||
import time | ||
import wave | ||
import numpy as np | ||
import pyaudio | ||
from audio_gen import user_audio | ||
import glob | ||
|
||
import soundfile as sf | ||
import torch | ||
from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer | ||
|
||
import deep_speech | ||
import wav2vec | ||
|
||
from text_gen import text_gen | ||
|
||
import tensorflow as tf | ||
import tensorflow_hub as hub | ||
|
||
# Module for calculating similarity between the audios | ||
|
||
module_url = "https://tfhub.dev/google/universal-sentence-encoder/4" | ||
embed_model = hub.load(module_url) | ||
|
||
def cosine(u, v): | ||
return np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v)) | ||
|
||
# Function to find similarity between 2 audios from the file | ||
# path to the files | ||
def similarity(audio1, audio2, model='deepspeech'): | ||
gen = text_gen(model=model) | ||
|
||
text1 = gen.text_from_file(audio1) | ||
text2 = gen.text_from_file(audio2) | ||
|
||
query = embed_model([text1])[0] | ||
target =embed_model([text2])[0] | ||
|
||
sim = cosine(query, target) | ||
|
||
return sim | ||
|
||
|
||
|