File size: 7,995 Bytes
3b04551
 
 
 
 
 
f95be78
7c1d20a
3513928
0423904
3b04551
 
 
7d21ed5
3fe2d17
7d21ed5
d7eccf5
7c5f808
 
ba774ae
03e603a
ee994fc
 
abca6ce
365609b
 
7d21ed5
70628eb
 
 
 
b51151a
70628eb
 
f95be78
774d921
 
f95be78
774d921
 
f95be78
72bacea
3b04551
 
 
f95be78
 
3b04551
 
 
 
 
4d17eaa
 
 
 
 
3b04551
 
341f056
3b04551
 
4d17eaa
95dea91
4d17eaa
7fc5b2a
b51151a
294ff95
7fc5b2a
294ff95
95dea91
774d921
f1634ee
7fc5b2a
4d17eaa
 
 
b51151a
 
 
3b04551
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f27218e
 
b51151a
 
ea76784
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
537317f
 
3b04551
 
f27218e
6468b0f
6f33556
c13afb3
128bd47
 
497efcc
 
8d5f163
6f33556
f27218e
6f33556
9056ead
6468b0f
3b04551
f27218e
3b04551
415e92e
497efcc
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
import time
import json
import os
import pickle
import gradio as gr
import numpy as np
import yt_dlp
import sys
import csv
from huggingface_hub import hf_hub_download

embed_html1 = '<iframe width="560" height="315" src="https://www.youtube.com/embed/'
embed_html2 = '" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'
# NO GPU
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow 
tensorflow.config.threading.set_intra_op_parallelism_threads(2)
tensorflow.config.threading.set_inter_op_parallelism_threads(2)

#
python_path = hf_hub_download(repo_id=os.environ['REPO_ID'], repo_type="space", filename=os.environ['MODEL_FILE'],
                                use_auth_token=os.environ['TOKEN'])
print("PATH : ", python_path)
sys.path.append(os.environ['PRIVATE_DIR'])
from models import *

ind = get_index()
ind_filenames = get_audio_names()
catalog = get_catalog()
url_dict = get_durl_myma()
###dict_catalog = get_dict_catalog() # dict containing track infos : key = track name (without ".mp3")


def download_audio_(link):
  with yt_dlp.YoutubeDL({'extract_audio': True, 'format': 'bestaudio', 'outtmpl': '%(title)s.mp3'}) as video:
    #with yt_dlp.YoutubeDL({'extract_audio': True, 'format': 'bestaudio', 'outtmpl': '%(id).mp3'}) as video:
    info_dict = video.extract_info(link, download = True)
    video_title = info_dict['title']
    #video_title = info_dict['id'] # KeyError !!!!!
    video.download(link)    
    return video_title #"tmp"

def download_audio(id_video):
    id = id_video.split("?v=")[-1][:11]
    audio_file = download_audio_(id_video)
    audio_file = audio_file+'.mp3'
    embed_html_all = embed_html1 + id +embed_html2
    return audio_file, audio_file, embed_html_all

def process_url(input_path):
    # setup the client
    #try :
    audio_file, audio_file, embed_html_all = download_audio(input_path)
    return process(audio_file, embed_html_all)
    #except:
    #    return "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", ""

def process_file(input_path):
    return process(input_path, '')[1:]

def process(audio_file, embed_html_all):
    #try : 
    timestart = time.time()
    emb, ts = get_embed(audio_file)
    print("Embed time :", time.time()-timestart)
     
    D, I = do_search(emb, ind)
    print("Search + embed time :", time.time()-timestart)
    prin("DEBUG DISTANCES : ", D)
    
    #top1, top2, top3, top4, top5, top6, top7, top8, top9, top10 = get_top(I, ind_filenames, url_dict, catalog)   
    top1, top2, top3, top4, top5, top6, top7, top8, top9, top10 = get_topN(I, ind_filenames, url_dict, catalog, 10)   
    print("Total time :", time.time()-timestart)
    return embed_html_all, ind_filenames[I[0][0]].split('.')[0], top1, ind_filenames[I[0][1]].split('.')[0], top2, ind_filenames[I[0][2]].split('.')[0], top3, ind_filenames[I[0][3]].split('.')[0], top4, ind_filenames[I[0][4]].split('.')[0], top5, ind_filenames[I[0][5]].split('.')[0], top6, ind_filenames[I[0][6]].split('.')[0], top7, ind_filenames[I[0][7]].split('.')[0], top8, ind_filenames[I[0][8]].split('.')[0], top9, ind_filenames[I[0][9]].split('.')[0], top10
    #except:
    #    return embed_html_all, "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", ""

topname=[]

with gr.Blocks() as demo:
    
    with gr.Row():

        with gr.Column():

            with gr.Row():
                #gr.HTML(embed_html)
                html = gr.HTML()
            
            with gr.Row():
                with gr.Column():
                    audio_url_input = gr.Textbox(placeholder='YouTube video URL', label='YouTube video URL')
                    analyze_url_btn = gr.Button('Search from URL')

            with gr.Row():
                with gr.Column():
                    audio_input_file = gr.Audio(type="filepath", label='Audio Input')
                    analyze_file_btn = gr.Button('Search from file')
                

            with gr.Row():
                with gr.Column():
                    '''
                    gr.HTML("<h3>Top 1</h3>")
                    top1 = gr.Textbox(label="top1", show_label=False)
                    gr.HTML("<h3>Top 2</h3>")
                    top2 = gr.Textbox(label="top2", show_label=False)
                    gr.HTML("<h3>Top 3</h3>")
                    top3 = gr.Textbox(label="top3", show_label=False)
                    gr.HTML("<h3>Top 4</h3>")
                    top4 = gr.Textbox(label="top4", show_label=False)
                    gr.HTML("<h3>Top 5</h3>")
                    top5 = gr.Textbox(label="top5", show_label=False)
                    '''
                    topname.append(gr.Textbox(label="top"+str(1)+"track name", show_label=True))
                    topname.append(gr.Audio(label="top"+str(1), show_label=False))
                    #top1_name = gr.Textbox(label="top1 track name", show_label=True)
                    #top1 = gr.Audio(label="top1", show_label=False)
                    top2_name = gr.Textbox(label="top2 track name", show_label=True)
                    top2 = gr.Audio(label="top2", show_label=False)
                    top3_name = gr.Textbox(label="top3 track name", show_label=True)
                    top3 = gr.Audio(label="top3", show_label=False)
                    top4_name = gr.Textbox(label="top4 track name", show_label=True)
                    top4 = gr.Audio(label="top4", show_label=False)
                    top5_name = gr.Textbox(label="top5 track name", show_label=True)
                    top5 = gr.Audio(label="top5", show_label=False)
                    top6_name = gr.Textbox(label="top6 track name", show_label=True)
                    top6 = gr.Audio(label="top6", show_label=False)
                    top7_name = gr.Textbox(label="top7 track name", show_label=True)
                    top7 = gr.Audio(label="top7", show_label=False)
                    top8_name = gr.Textbox(label="top8 track name", show_label=True)
                    top8 = gr.Audio(label="top8", show_label=False)
                    top9_name = gr.Textbox(label="top9 track name", show_label=True)
                    top9 = gr.Audio(label="top9", show_label=False)
                    top10_name = gr.Textbox(label="top10 track name", show_label=True)
                    top10 = gr.Audio(label="top10", show_label=False)
                    
                
    
    analyze_url_btn.click(process_url, inputs=[audio_url_input], 
                      outputs=[html]+topname+[top2_name, top2, top3_name, top3, top4_name, top4, top5_name, top5, top6_name, top6, top7_name, top7, top8_name, top8, top9_name, top9, top10_name, top10])
    gr.Examples(
        examples = [
         "https://www.youtube.com/watch?v=aNzCDt2eidg", 
         "https://www.youtube.com/watch?v=NBE-uBgtINg", 
         "https://www.youtube.com/watch?v=5NV6Rdv1a3I",
         "https://www.youtube.com/watch?v=OiC1rgCPmUQ",#
         "https://www.youtube.com/watch?v=dRX0wDNK6S4",#
         "https://www.youtube.com/watch?v=Guzu9aAeDIc"],
        inputs = [audio_url_input],
        outputs = [html]+topname+[top2_name, top2, top3_name, top3, top4_name, top4, top5_name, top5, top6_name, top6, top7_name, top7, top8_name, top8, top9_name, top9, top10_name, top10],
        fn = process_url,
        cache_examples=False,
    )
    analyze_file_btn.click(process_file, inputs=[audio_input_file], 
                      outputs=topname+[top2_name, top2, top3_name, top3, top4_name, top4, top5_name, top5, top6_name, top6, top7_name, top7, top8_name, top8, top9_name, top9, top10_name, top10])


demo.launch(debug=False)