File size: 10,268 Bytes
0baca95
 
3b04551
0baca95
3b04551
f95be78
7c1d20a
c6bdfa8
 
0baca95
607f1ae
1cc7035
a5f5c9f
3b04551
 
92791f8
 
 
 
0baca95
 
 
 
92791f8
03e603a
0baca95
 
 
 
 
 
 
 
365609b
7d21ed5
29d8a43
90f97b8
0baca95
0c1bb10
 
 
 
 
 
 
3b5ec49
 
 
1cc7035
3b5ec49
1cc7035
3b5ec49
 
1cc7035
3b5ec49
1cc7035
 
 
 
 
 
 
 
 
 
 
 
 
3b5ec49
 
 
 
1cc7035
bf32acd
3b5ec49
 
 
 
20c4b17
 
1cc7035
 
 
 
 
 
 
3b5ec49
1cc7035
3b5ec49
0baca95
f1ff944
 
 
0baca95
 
 
f1ff944
0baca95
 
 
3b04551
 
 
0baca95
 
 
 
fdcc758
3b04551
0baca95
3b04551
0baca95
3b04551
0baca95
 
 
 
c6bdfa8
 
 
 
0baca95
0c1bb10
043c163
fdcc758
4176bf3
0baca95
4176bf3
0baca95
2672b0d
607f1ae
 
 
58f8d16
3b5ec49
cfa894e
725e29e
90f97b8
 
 
3b5ec49
 
 
 
 
 
 
 
a0772af
eabc593
d555084
 
 
eabc593
a0772af
1cc7035
 
 
 
 
 
 
 
eabc593
 
90f97b8
a0772af
607f1ae
dec2550
0baca95
2936031
0baca95
 
 
3b04551
 
 
 
0baca95
3b04551
0baca95
3b04551
 
0baca95
 
 
 
3b04551
 
 
0baca95
 
3b04551
607f1ae
 
cfa894e
607f1ae
3b04551
 
2672b0d
cfa894e
 
dec2550
cfa894e
0fb5151
cfa894e
 
 
0baca95
cfa894e
 
 
0fb5151
2672b0d
cfa894e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5622cb6
cfa894e
 
 
 
 
 
 
 
 
 
 
 
f1ff944
cfa894e
 
 
46e0397
 
 
 
 
 
5622cb6
46e0397
cfa894e
 
 
 
 
 
 
 
 
f1ff944
cfa894e
ff28df8
0baca95
0fb5151
 
2672b0d
0baca95
6468b0f
0baca95
 
 
 
 
 
 
 
 
2672b0d
0baca95
d4349ec
f1ff944
 
6468b0f
0baca95
 
 
2672b0d
0baca95
3b04551
415e92e
0baca95
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from huggingface_hub import hf_hub_download

import os
import time
import gradio as gr
import yt_dlp
import sys
import uuid
import traceback
import tensorflow
import csv
import json

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"

# tensorflow.config.threading.set_intra_op_parallelism_threads(8)
# tensorflow.config.threading.set_inter_op_parallelism_threads(8)
#print(f"Available GPUs {tensorflow.config.list_physical_devices('GPU')}")
#
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 *

max_results = 100
max_output = 50

# global (faster)
ind = get_index()
ind_filenames = get_audio_names()
catalog = get_catalog()
url_dict = get_durl_myma()
dict_catalog = get_dict_catalog()

fixation_id_to_file_name = {}
for file_name, infos in dict_catalog.items():
    # we want only main versions
    if infos["Parent fixation id"].strip():
        continue
    fixation_id_to_file_name[infos["Fixation id"].strip()] = file_name

child_to_parent_filename = {}
count = count_failed = 0
for file_name, infos in dict_catalog.items():
    if not infos["Parent fixation id"].strip():
        continue

    count += 1
    try:
        child_to_parent_filename[file_name] = fixation_id_to_file_name[
            infos["Parent fixation id"].strip()
        ]
    except Exception as e:
        print(f"No parent for {file_name} : {e}")
        count_failed += 1

print(f"{count_failed} tracks have no parent / {count} tracks")

parent_file_names = set(list(fixation_id_to_file_name.values()))

file_name_to_url = {}
for file_url in url_dict.values():
    file_name = os.path.splitext(os.path.basename(file_url))[0]
    if file_name not in parent_file_names:
        continue
    file_name_to_url[file_name] = file_url

parent_file_names = []
fixation_id_to_file_name = []
with open("fixation_id_to_file_name.json", "w") as w, open(
    "child_to_parent_filename.json", "w"
) as w2, open("file_name_to_url.json", "w") as w3:
    json.dump(fixation_id_to_file_name, w)
    json.dump(child_to_parent_filename, w2)
    json.dump(file_name_to_url, w3)

parent_file_names = []
fixation_id_to_file_name = {}


def download_audio(id_video):
    id = id_video.split("?v=")[-1][:11]
    file_name = f"youtube_video_{id}.mp3"
    with yt_dlp.YoutubeDL(
        {"extract_audio": True, "format": "bestaudio", "outtmpl": file_name}
    ) as video:
        video.download(id_video)
    embed_html_all = embed_html1 + id + embed_html2
    return file_name, embed_html_all


def process_url(input_path):
    # setup the client
    # try :
    file_name, embed_html_all = download_audio(input_path)
    return process(file_name, 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(file_name, embed_html_all):
    """try :"""
    emb, _ = get_embed(file_name)
    try:
        os.remove(file_name)
    except:
        print(traceback.format_exc())

    # => global

    timestart = time.time()
    D, I = do_search(emb, ind)
    print("search time :", time.time() - timestart)
    print("DEBUG DISTANCES : ", D)
    tops = get_topN(I, ind_filenames, url_dict, catalog, max_results)
    formated = [{"f": "Choose a result to play", "t": ""}]
    output_csv = f"{file_name}_results.csv"
    with open(output_csv, "w") as w:
        writer = csv.writer(w)
        header = False
        already = set()
        for position, top in enumerate(tops):
            if len(formated) / 2 >= max_output:
                break

            file = os.path.splitext(os.path.basename(top))[0]
            try:
                file = child_to_parent_filename[file]
                top = file_name_to_url[file]
            except KeyError:
                pass
            if file in already:
                continue
            already.add(file)
            file_name = file
            if file in dict_catalog:
                if not header:
                    writer.writerow(list(dict_catalog[file].keys()))
                    header = True
                writer.writerow(dict_catalog[file].values())
                file_name = dict_catalog[file]["Track name"]
                try:
                    file_name += " - " + dict_catalog[file]["Composer1 full name"]
                except:
                    pass
                try:
                    file_name += " - " + dict_catalog[file]["Album name"]
                except:
                    pass
            else:
                writer.writerow([file, "no metadata provided"])

            formated.append({"f": f"{position+1} - {file_name}", "t": top})

    return (embed_html_all, output_csv, formated)
    """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", ""
    """


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():
                    csv_results = gr.File(label="Results as CSV")

            with gr.Row():
                with gr.Column():
                    results = gr.JSON(visible=False)
                    select_results = gr.Dropdown(label="Results", choices=[])
                    audio_player = gr.Audio(None, label="Results player")

                    @select_results.select(inputs=select_results, outputs=audio_player)
                    def change_audio(value):
                        if value:
                            return gr.Audio(value, label="Results player")
                        return gr.Audio(None, label="Results player")

                    @results.change(
                        inputs=results,
                        outputs=select_results,
                    )
                    def update_select(json_results):
                        try:
                            print("change dropdown")
                            return gr.Dropdown(
                                label="Results",
                                choices=[(k["f"], k["t"]) for k in json_results],
                                value=None,
                            )
                        except:
                            return gr.Dropdown(
                                choices=[],
                                label="Results",
                            )

            @audio_input_file.change(
                outputs=[
                    audio_url_input,
                    results,
                    select_results,
                    csv_results,
                    audio_player,
                    html,
                ]
            )
            def cleanup_on_file():
                print("cleanup on file change")
                return (
                    gr.Textbox(
                        placeholder="YouTube video URL", label="YouTube video URL"
                    ),
                    gr.JSON([{"f": "Choose a result to play", "t": ""}], visible=False),
                    gr.Dropdown(label="Results", choices=[]),
                    gr.File(None, label="Results as CSV"),
                    gr.Audio(None, label="Results player"),
                    gr.HTML(""),
                )

            @audio_url_input.change(
                outputs=[
                    audio_input_file,
                    results,
                    select_results,
                    csv_results,
                    audio_player,
                    html,
                ]
            )
            def cleanup_on_url():
                print("cleanup on url change")
                return (
                    gr.Audio(None, type="filepath", label="Audio Input"),
                    gr.JSON([{"f": "Choose a result to play", "t": ""}], visible=False),
                    gr.Dropdown(choices=[], label="Results"),
                    gr.File(None, label="Results as CSV"),
                    gr.Audio(None, label="Results player"),
                    gr.HTML(""),
                )

    analyze_url_btn.click(
        process_url,
        inputs=[audio_url_input],
        outputs=[html, csv_results, results],
    )
    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, csv_results, results],
        fn=process_url,
        cache_examples=False,
        examples_per_page=20,
        run_on_click=True,
    )
    analyze_file_btn.click(
        process_file,
        inputs=[audio_input_file],
        outputs=[csv_results, results],
    )


demo.launch(debug=False)