Spaces:
Runtime error
Runtime error
File size: 8,839 Bytes
5f10f3c 2735a87 12538cc c43fc50 15020c4 9964dc6 28b1346 650a7e0 1f67700 e38c032 08c05e2 a155e5a 08c05e2 28b1346 1f67700 eb392be 1f67700 b687ee3 1f67700 b687ee3 958c44f 6d0bde9 1f67700 e4fb848 a66b073 1f67700 c85050b eb392be 2735a87 e38c032 1f67700 f65a9bb 1f67700 f65a9bb 2735a87 1f67700 f65a9bb 1f67700 5c465f9 1f67700 708834d 9341d39 a8c9a19 708834d 1f67700 9341d39 08c05e2 1f67700 5c465f9 9341d39 2fe620b c85050b 1f67700 9341d39 1f67700 c85050b 1f67700 2fe620b c85050b f06cc84 1f67700 1b5b297 08c05e2 9964dc6 c85050b 9964dc6 35414ca 708834d 1f67700 | 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 | import gradio as gr
import os
import sys
import numpy as np
import csv
import time
import datetime
from huggingface_hub import hf_hub_download
import traceback
# NO GPU
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
max_results = 100
max_output = 50
# Cacher le nom du repo
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(python_path)
sys.path.append(os.environ["PRIVATE_DIR"])
from models import *
preprocess_model, model = get_models()
url_dict = get_durl_myma()
dict_catalog = get_dict_catalog()
# audio_names = get_audio_names()
audio_names = get_audio_names_pickle()
index = get_index()
# encoder_text = get_encoder_text() #Error ??
encoder_text = tf.keras.models.load_model(
"encoder_text_retrievaltext_bmg_221022_54_clean"
)
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 = []
def process(prompt, lang):
now = datetime.datetime.now()
print()
print("*************")
print("Current Time: ", str(now))
print("Text input : ", prompt)
print("*************")
print()
a = time.time()
embed_query = get_predict(encoder_text, prompt, preprocess_model, model)
print("Embed time : ", time.time() - a)
do_normalize(embed_query)
D, I = get_distance(index, embed_query, max_output)
print("Search + Embed time : ", time.time() - a)
# print(I)
# print(D)
# print("----")
# for i in range(len(I[0])):
# print(audio_names[I[0][i]], " with distance ", D[0][i])
# print(" url : ", get_url_myma(I[0][i], audio_names, url_dict))
formated = [{"f": "Choose a result to play", "t": ""}]
output_csv = f"prompt_{prompt}_results.csv"
with open(output_csv, "w") as w:
writer = csv.writer(w)
header = False
already = set()
for position, top in enumerate(I[0]):
if len(formated) / 2 >= max_output:
break
file = os.path.splitext(os.path.basename(audio_names[top]))[0]
top = get_url_myma(top, audio_names, url_dict)
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
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
writer.writerow(dict_catalog[file].values())
else:
writer.writerow([file, "no metadata provided"])
try:
formated.append(
{
"f": f"{position+1} - {file_name}",
"t": top,
}
)
except:
print(f"Error with {file}")
print(traceback.format_exc())
print("Total time : ", time.time() - a)
return output_csv, formated
"""return [output_csv,
audio_names[I[0][0]].split('.')[0], get_url_myma(I[0][0], audio_names, url_dict),
"""
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
with gr.Row():
with gr.Column():
input_search = gr.Textbox(
label="Input", value="type your description", max_lines=2
)
input_search_lang = gr.Radio(
label="Language", choices=["en"], value="en"
)
analyze_btn = gr.Button("Search")
with gr.Column():
csv_results = gr.File(
label="Results CSV file : ready for download", show_label=True
)
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:
return gr.Dropdown(
label="Results",
choices=[(k["f"], k["t"]) for k in json_results],
value=None,
)
except:
return gr.Dropdown(
choices=[],
label="Results",
)
@input_search.change(
outputs=[results, select_results, csv_results, audio_player]
)
def cleanup_on_url():
print("cleanup on url change")
return (
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.Examples(
examples=[
["Mysterious filmscore with Arabic influenced instruments", "en"],
[
"Let's go on a magical adventure with wizzards, dragons and castles",
"en",
],
[
"Creepy piano opening evolves and speeds up into a cinematic orchestral piece",
"en",
],
["Chilled electronic", "en"],
# ["","en"],
["Relax piano", "en"],
["Halloween rock with creepy organ", "en"],
[
"Rhythmic electro dance track for sport, motivation and sweating",
"en",
],
[
"soundtrack for an action movie from the eighties in a retro synth wave style",
"en",
],
[
"Choral female singing is rhythmically accompanied in a church with medieval instruments",
"en",
],
["Christmas", "en"],
["love romantic with piano, strings and vocals", "en"],
["Electronic soundscapes for chilling and relaxing", "en"],
["Minimal, emotional, melancholic piano", "en"],
["A calm and romantic acoustic guitar melody", "en"],
["horror suspense piano", "en"],
["Big Band", "en"],
["90 eurodance beat", "en"],
],
inputs=[input_search, input_search_lang],
outputs=[csv_results, results],
cache_examples=False,
fn=process,
examples_per_page=20,
run_on_click=True,
)
analyze_btn.click(
process,
inputs=[input_search, input_search_lang],
outputs=[csv_results, results],
)
demo.launch(debug=False)
|