PierreHanna commited on
Commit
3b04551
β€’
0 Parent(s):

Duplicate from PierreHanna/AudioSimilarity

Browse files
Files changed (7) hide show
  1. .gitattributes +36 -0
  2. README.md +13 -0
  3. app.py +106 -0
  4. dict_bmg.pickle +3 -0
  5. index.filenames +3 -0
  6. index.index +3 -0
  7. requirements.txt +4 -0
.gitattributes ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ index.filenames filter=lfs diff=lfs merge=lfs -text
36
+ index.index filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: AudioSimilarity
3
+ emoji: 🐒
4
+ colorFrom: green
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 3.28.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: PierreHanna/AudioSimilarity
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import json
3
+ import os
4
+ import openl3
5
+ import librosa
6
+ import pickle
7
+ import gradio as gr
8
+ import numpy as np
9
+ import faiss
10
+ from pytube import YouTube
11
+
12
+ embed_html1 = '<iframe width="560" height="315" src="https://www.youtube.com/embed/'
13
+ embed_html2 = '" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'
14
+
15
+
16
+ def download_audio(id_video):
17
+ video = YouTube(id_video,use_oauth=True, allow_oauth_cache=True)
18
+ id = id_video.split("?v=")[-1][:11]
19
+ print("ID youtube ", id)
20
+ audio_streams = video.streams.filter(only_audio=True)
21
+ audio_stream = audio_streams[0]
22
+ audio_file = audio_stream.download()
23
+ audio_parts = audio_file.split('/')[-1]
24
+ audio_title = '.'.join(audio_parts.split('.')[:-1])
25
+ embed_html_all = embed_html1 + id +embed_html2
26
+ return audio_file, audio_file, embed_html_all
27
+
28
+ def process_url(input_path):
29
+ # setup the client
30
+ audio_file, audio_file, embed_html_all = download_audio(input_path)
31
+ return process(audio_file, embed_html_all)
32
+
33
+ def process_file(input_path):
34
+ return process(input_path, '')
35
+
36
+ def process(audio_file, embed_html_all):
37
+ model = openl3.models.load_audio_embedding_model(input_repr="linear", content_type="music",embedding_size=512)
38
+ audio, sr = librosa.load(audio_file, mono=True)
39
+ emb, ts = openl3.get_audio_embedding(audio, sr, model=model)
40
+ emb = np.mean(emb, axis=0)
41
+ print("SHAPE ", emb.shape)
42
+
43
+ ind = faiss.read_index("index.index")
44
+ ind_filenames = pickle.load(open('index.filenames','rb'))
45
+ dict_bmg = pickle.load(open('dict_bmg.pickle','rb')) # filename to url
46
+ D, I = ind.search(emb.reshape((1,512)), 5)
47
+
48
+ top1 = dict_bmg[ind_filenames[I[0][0]]]
49
+ top2 = dict_bmg[ind_filenames[I[0][1]]]
50
+ top3 = dict_bmg[ind_filenames[I[0][2]]]
51
+ top4 = dict_bmg[ind_filenames[I[0][3]]]
52
+ top5 = dict_bmg[ind_filenames[I[0][4]]]
53
+
54
+ return top1, top2, top3, top4, top5
55
+
56
+
57
+ with gr.Blocks() as demo:
58
+
59
+ with gr.Row():
60
+
61
+ with gr.Column():
62
+
63
+ with gr.Row():
64
+ #gr.HTML(embed_html)
65
+ html = gr.HTML()
66
+
67
+ with gr.Row():
68
+ with gr.Column():
69
+ audio_url_input = gr.Textbox(placeholder='YouTube video URL', label='YouTube video URL')
70
+ analyze_url_btn = gr.Button('Search from URL')
71
+
72
+ with gr.Row():
73
+ with gr.Column():
74
+ audio_input_file = gr.Audio(type="filepath", label='Audio Input')
75
+ analyze_file_btn = gr.Button('Search from file')
76
+
77
+
78
+ with gr.Row():
79
+ with gr.Column():
80
+ '''
81
+ gr.HTML("<h3>Top 1</h3>")
82
+ top1 = gr.Textbox(label="top1", show_label=False)
83
+ gr.HTML("<h3>Top 2</h3>")
84
+ top2 = gr.Textbox(label="top2", show_label=False)
85
+ gr.HTML("<h3>Top 3</h3>")
86
+ top3 = gr.Textbox(label="top3", show_label=False)
87
+ gr.HTML("<h3>Top 4</h3>")
88
+ top4 = gr.Textbox(label="top4", show_label=False)
89
+ gr.HTML("<h3>Top 5</h3>")
90
+ top5 = gr.Textbox(label="top5", show_label=False)
91
+ '''
92
+ top1 = gr.Audio(label="top1", show_label=True)
93
+ top2 = gr.Audio(label="top2", show_label=True)
94
+ top3 = gr.Audio(label="top3", show_label=True)
95
+ top4 = gr.Audio(label="top4", show_label=True)
96
+ top5 = gr.Audio(label="top5", show_label=True)
97
+
98
+
99
+ analyze_url_btn.click(process_url, inputs=[audio_url_input],
100
+ outputs=[top1, top2, top3, top4, top5])
101
+
102
+ analyze_file_btn.click(process_file, inputs=[audio_input_file],
103
+ outputs=[top1, top2, top3, top4, top5])
104
+
105
+
106
+ demo.launch(debug=True)
dict_bmg.pickle ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3e67645329cc091ba56c358ba3dc61032a457878f3b879f0e2e720294d5337b1
3
+ size 15953609
index.filenames ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4d4b8ec64b35965b7ca3d202d767f4c9549934683bf1b854a2efc4aa12d1634d
3
+ size 1987986
index.index ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8db1af12dc9323081564f89052ea8cd331a826b5066ec6bbe8943b8b5c7f094b
3
+ size 117442605
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ pytube
2
+ faiss-cpu
3
+ librosa
4
+ openl3