Spaces:
Runtime error
Runtime error
File size: 7,904 Bytes
0baca95 3b04551 0baca95 3b04551 f95be78 7c1d20a c6bdfa8 0baca95 607f1ae a5f5c9f 3b04551 7d21ed5 0baca95 03e603a 0baca95 365609b 7d21ed5 90f97b8 0baca95 0c1bb10 0baca95 f95be78 0baca95 3b04551 0baca95 3b04551 0baca95 fdcc758 3b04551 0baca95 3b04551 0baca95 3b04551 0baca95 c6bdfa8 0baca95 0c1bb10 043c163 fdcc758 0baca95 2672b0d 607f1ae 58f8d16 cfa894e 725e29e 90f97b8 eabc593 d555084 eabc593 90f97b8 cfa894e 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 ff28df8 0baca95 0fb5151 2672b0d 0baca95 6468b0f 0baca95 2672b0d 0baca95 d4349ec 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 |
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
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(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()
def download_audio_(link):
file_name = f"{uuid.uuid4()}.mp3"
with yt_dlp.YoutubeDL(
{"extract_audio": True, "format": "bestaudio", "outtmpl": file_name}
) as video:
video.download(link)
return file_name
def download_audio(id_video):
id = id_video.split("?v=")[-1][:11]
file_name = download_audio_(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()
_, I = do_search(emb, ind)
print("search time :", time.time() - timestart)
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
for position, top in enumerate(tops):
if len(formated) / 2 >= max_output:
break
file = os.path.splitext(os.path.basename(top))[0]
if file in dict_catalog:
if not header:
writer.writerow(list(dict_catalog[file].keys()))
header = True
writer.writerow(dict_catalog[file].values())
else:
writer.writerow([file, "no metadata provided"])
formated.append({"f": f"{position+1} - {file}", "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,
]
)
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"),
)
@audio_url_input.change(
outputs=[audio_input_file, results, select_results, csv_results]
)
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"),
)
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,
)
analyze_file_btn.click(
process_file,
inputs=[audio_input_file],
outputs=[csv_results, results],
)
demo.launch(debug=False)
|