firagne commited on
Commit
0baca95
1 Parent(s): ae79574

factorize and generalize

Browse files
Files changed (1) hide show
  1. app.py +100 -99
app.py CHANGED
@@ -1,153 +1,154 @@
1
- import time
2
- import json
3
  import os
4
- import pickle
5
  import gradio as gr
6
- import numpy as np
7
  import yt_dlp
8
  import sys
9
- import csv
10
- from huggingface_hub import hf_hub_download
11
  import uuid
12
  import traceback
13
-
14
 
15
  embed_html1 = '<iframe width="560" height="315" src="https://www.youtube.com/embed/'
16
  embed_html2 = '" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'
17
  # NO GPU
18
- #os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
19
- os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
20
- import tensorflow
21
- #tensorflow.config.threading.set_intra_op_parallelism_threads(8)
22
- #tensorflow.config.threading.set_inter_op_parallelism_threads(8)
23
- print(tensorflow.config.list_physical_devices('GPU'))
24
  #
25
- python_path = hf_hub_download(repo_id=os.environ['REPO_ID'], repo_type="space", filename=os.environ['MODEL_FILE'],
26
- use_auth_token=os.environ['TOKEN'])
27
- #print("PATH : ", python_path)
28
- sys.path.append(os.environ['PRIVATE_DIR'])
 
 
 
 
29
  from models import *
30
 
 
 
 
31
  def download_audio_(link):
32
- file_name = f"{uuid.uuid4()}.mp3"
33
- with yt_dlp.YoutubeDL({'extract_audio': True, 'format': 'bestaudio', 'outtmpl': file_name}) as video:
34
- info_dict = video.extract_info(link, download = True)
35
- video_title = info_dict['title']
36
- video.download(link)
37
- return video_title, file_name
 
38
 
39
  def download_audio(id_video):
40
  id = id_video.split("?v=")[-1][:11]
41
- audio_title, file_name = download_audio_(id_video)
42
- embed_html_all = embed_html1 + id +embed_html2
43
- return audio_title, file_name, embed_html_all
 
44
 
45
  def process_url(input_path):
46
  # setup the client
47
- #try :
48
- audio_title, file_name, embed_html_all = download_audio(input_path)
49
- return process(file_name, audio_title, embed_html_all)
50
- #except:
51
  # return "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", ""
52
 
 
53
  def process_file(input_path):
54
- return process(input_path, '', '')[1:]
55
 
56
- def process(file_name, audio_title, embed_html_all):
57
- '''try : '''
58
- emb, ts = get_embed(file_name)
 
59
  try:
60
  os.remove(file_name)
61
  except:
62
  print(traceback.format_exc())
 
63
  ind = get_index()
64
  ind_filenames = get_audio_names()
65
  catalog = get_catalog()
66
  url_dict = get_durl_myma()
 
67
 
68
  timestart = time.time()
69
  _, I = do_search(emb, ind)
70
- print("search time :", time.time()-timestart)
71
- top1, top2, top3, top4, top5, top6, top7, top8, top9, top10 = get_top(I, ind_filenames, url_dict, catalog)
72
- 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
73
- '''except:
 
 
 
 
 
 
74
  return embed_html_all, "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", ""
75
- '''
 
 
76
  with gr.Blocks() as demo:
77
-
78
  with gr.Row():
79
-
80
  with gr.Column():
81
-
82
  with gr.Row():
83
- #gr.HTML(embed_html)
84
  html = gr.HTML()
85
-
86
  with gr.Row():
87
  with gr.Column():
88
- audio_url_input = gr.Textbox(placeholder='YouTube video URL', label='YouTube video URL')
89
- analyze_url_btn = gr.Button('Search from URL')
 
 
90
 
91
  with gr.Row():
92
  with gr.Column():
93
- audio_input_file = gr.Audio(type="filepath", label='Audio Input')
94
- analyze_file_btn = gr.Button('Search from file')
95
-
96
 
97
  with gr.Row():
98
  with gr.Column():
99
- '''
100
- gr.HTML("<h3>Top 1</h3>")
101
- top1 = gr.Textbox(label="top1", show_label=False)
102
- gr.HTML("<h3>Top 2</h3>")
103
- top2 = gr.Textbox(label="top2", show_label=False)
104
- gr.HTML("<h3>Top 3</h3>")
105
- top3 = gr.Textbox(label="top3", show_label=False)
106
- gr.HTML("<h3>Top 4</h3>")
107
- top4 = gr.Textbox(label="top4", show_label=False)
108
- gr.HTML("<h3>Top 5</h3>")
109
- top5 = gr.Textbox(label="top5", show_label=False)
110
- '''
111
- top1_name = gr.Textbox(label="top1 track name", show_label=True)
112
- top1 = gr.Audio(label="top1", show_label=False)
113
- top2_name = gr.Textbox(label="top2 track name", show_label=True)
114
- top2 = gr.Audio(label="top2", show_label=False)
115
- top3_name = gr.Textbox(label="top3 track name", show_label=True)
116
- top3 = gr.Audio(label="top3", show_label=False)
117
- top4_name = gr.Textbox(label="top4 track name", show_label=True)
118
- top4 = gr.Audio(label="top4", show_label=False)
119
- top5_name = gr.Textbox(label="top5 track name", show_label=True)
120
- top5 = gr.Audio(label="top5", show_label=False)
121
- top6_name = gr.Textbox(label="top6 track name", show_label=True)
122
- top6 = gr.Audio(label="top6", show_label=False)
123
- top7_name = gr.Textbox(label="top7 track name", show_label=True)
124
- top7 = gr.Audio(label="top7", show_label=False)
125
- top8_name = gr.Textbox(label="top8 track name", show_label=True)
126
- top8 = gr.Audio(label="top8", show_label=False)
127
- top9_name = gr.Textbox(label="top9 track name", show_label=True)
128
- top9 = gr.Audio(label="top9", show_label=False)
129
- top10_name = gr.Textbox(label="top10 track name", show_label=True)
130
- top10 = gr.Audio(label="top10", show_label=False)
131
-
132
-
133
-
134
- analyze_url_btn.click(process_url, inputs=[audio_url_input],
135
- outputs=[html,top1_name, top1, 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])
136
  gr.Examples(
137
- examples = [
138
- "https://www.youtube.com/watch?v=aNzCDt2eidg",
139
- "https://www.youtube.com/watch?v=NBE-uBgtINg",
140
- "https://www.youtube.com/watch?v=5NV6Rdv1a3I",
141
- "https://www.youtube.com/watch?v=OiC1rgCPmUQ",#
142
- "https://www.youtube.com/watch?v=dRX0wDNK6S4",#
143
- "https://www.youtube.com/watch?v=Guzu9aAeDIc"],
144
- inputs = [audio_url_input],
145
- outputs = [html,top1_name, top1, 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],
146
- fn = process_url,
 
 
 
 
147
  cache_examples=False,
148
  )
149
- analyze_file_btn.click(process_file, inputs=[audio_input_file],
150
- outputs=[top1_name, top1, 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])
 
 
 
151
 
152
 
153
- demo.launch(debug=False)
 
1
+ from huggingface_hub import hf_hub_download
2
+
3
  import os
4
+ import time
5
  import gradio as gr
 
6
  import yt_dlp
7
  import sys
 
 
8
  import uuid
9
  import traceback
10
+ import tensorflow
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
  # NO GPU
15
+ # os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
16
+ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
17
+
18
+ # tensorflow.config.threading.set_intra_op_parallelism_threads(8)
19
+ # tensorflow.config.threading.set_inter_op_parallelism_threads(8)
20
+ print(tensorflow.config.list_physical_devices("GPU"))
21
  #
22
+ python_path = hf_hub_download(
23
+ repo_id=os.environ["REPO_ID"],
24
+ repo_type="space",
25
+ filename=os.environ["MODEL_FILE"],
26
+ use_auth_token=os.environ["TOKEN"],
27
+ )
28
+ # print("PATH : ", python_path)
29
+ sys.path.append(os.environ["PRIVATE_DIR"])
30
  from models import *
31
 
32
+ max_results = 50
33
+
34
+
35
  def download_audio_(link):
36
+ file_name = f"{uuid.uuid4()}.mp3"
37
+ with yt_dlp.YoutubeDL(
38
+ {"extract_audio": True, "format": "bestaudio", "outtmpl": file_name}
39
+ ) as video:
40
+ video.download(link)
41
+ return file_name
42
+
43
 
44
  def download_audio(id_video):
45
  id = id_video.split("?v=")[-1][:11]
46
+ file_name = download_audio_(id_video)
47
+ embed_html_all = embed_html1 + id + embed_html2
48
+ return file_name, embed_html_all
49
+
50
 
51
  def process_url(input_path):
52
  # setup the client
53
+ # try :
54
+ file_name, embed_html_all = download_audio(input_path)
55
+ return process(file_name, embed_html_all)
56
+ # except:
57
  # return "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", ""
58
 
59
+
60
  def process_file(input_path):
61
+ return process(input_path, "")[1:]
62
 
63
+
64
+ def process(file_name, embed_html_all):
65
+ """try :"""
66
+ emb, _ = get_embed(file_name)
67
  try:
68
  os.remove(file_name)
69
  except:
70
  print(traceback.format_exc())
71
+
72
  ind = get_index()
73
  ind_filenames = get_audio_names()
74
  catalog = get_catalog()
75
  url_dict = get_durl_myma()
76
+ dict_catalog = get_dict_catalog()
77
 
78
  timestart = time.time()
79
  _, I = do_search(emb, ind)
80
+ print("search time :", time.time() - timestart)
81
+
82
+ tops = get_topN(I, ind_filenames, url_dict, catalog, max_results)
83
+ formated = []
84
+ for i in range(max_results):
85
+ file = os.path.splitext(os.path.basename(ind_filenames[I[0][0]]))[0]
86
+ formated.append(file)
87
+ formated.append(tops[i])
88
+ return (embed_html_all, *formated)
89
+ """except:
90
  return embed_html_all, "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", "", "Erreur Input", ""
91
+ """
92
+
93
+
94
  with gr.Blocks() as demo:
95
+ tops = []
96
  with gr.Row():
 
97
  with gr.Column():
 
98
  with gr.Row():
99
+ # gr.HTML(embed_html)
100
  html = gr.HTML()
101
+
102
  with gr.Row():
103
  with gr.Column():
104
+ audio_url_input = gr.Textbox(
105
+ placeholder="YouTube video URL", label="YouTube video URL"
106
+ )
107
+ analyze_url_btn = gr.Button("Search from URL")
108
 
109
  with gr.Row():
110
  with gr.Column():
111
+ audio_input_file = gr.Audio(type="filepath", label="Audio Input")
112
+ analyze_file_btn = gr.Button("Search from file")
 
113
 
114
  with gr.Row():
115
  with gr.Column():
116
+ for i in range(max_results):
117
+ tops.append(
118
+ gr.Textbox(label=f"top{i} track name", show_label=True)
119
+ )
120
+ tops.append(gr.Audio(label=f"top{i}", show_label=False))
121
+
122
+ analyze_url_btn.click(
123
+ process_url,
124
+ inputs=[audio_url_input],
125
+ outputs=[
126
+ html,
127
+ *tops,
128
+ ],
129
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  gr.Examples(
131
+ examples=[
132
+ "https://www.youtube.com/watch?v=aNzCDt2eidg",
133
+ "https://www.youtube.com/watch?v=NBE-uBgtINg",
134
+ "https://www.youtube.com/watch?v=5NV6Rdv1a3I",
135
+ "https://www.youtube.com/watch?v=OiC1rgCPmUQ", #
136
+ "https://www.youtube.com/watch?v=dRX0wDNK6S4", #
137
+ "https://www.youtube.com/watch?v=Guzu9aAeDIc",
138
+ ],
139
+ inputs=[audio_url_input],
140
+ outputs=[
141
+ html,
142
+ *tops,
143
+ ],
144
+ fn=process_url,
145
  cache_examples=False,
146
  )
147
+ analyze_file_btn.click(
148
+ process_file,
149
+ inputs=[audio_input_file],
150
+ outputs=tops,
151
+ )
152
 
153
 
154
+ demo.launch(debug=False)