Awell00 commited on
Commit
e1ba51e
·
verified ·
1 Parent(s): 7b2f3ca

feat: add support for processing uploaded WAV files instead of downloading YouTube audio .

Browse files

- Replaced song title input with a file upload interface to accept WAV files.
- Implemented function to process uploaded audio files.
- Updated the `process_audio` function to handle uploaded WAV files.
- Removed YouTube download functionality

Files changed (1) hide show
  1. app.py +23 -57
app.py CHANGED
@@ -23,42 +23,18 @@ def delete_input_files(input_dir):
23
  wav_file.unlink()
24
  print(f"Deleted {wav_file}")
25
 
26
- def download_youtube_audio_by_title(query, state=True):
27
  if state:
28
  delete_input_files(INPUT_FOLDER)
29
 
30
- ydl_opts = {
31
- 'quiet': True,
32
- 'default_search': 'ytsearch',
33
- 'noplaylist': True,
34
- 'format': 'bestaudio/best',
35
- 'outtmpl': './input/wav/%(title)s.%(ext)s',
36
- 'postprocessors': [{
37
- 'key': 'FFmpegExtractAudio',
38
- 'preferredcodec': 'wav',
39
- }],
40
- 'cookiefile': './cookies.txt',
41
- }
42
 
43
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
44
- search_results = ydl.extract_info(query, download=False)
45
- video_info = search_results['entries'][0]
46
- video_url = video_info['webpage_url']
47
- video_title = video_info['title']
48
-
49
- match = re.match(r'^(.*? - .*?)(?: \[.*\]|\(.*\))?$', video_title)
50
- formatted_title = match.group(1) if match else video_title
51
-
52
- formatted_title = sanitize_filename(formatted_title.strip())
53
-
54
- ydl_opts['outtmpl'] = f'./input/wav/{formatted_title}.%(ext)s'
55
-
56
- if state:
57
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
58
- ydl.download([video_url])
59
- return f'./input/wav/{formatted_title}.wav'
60
-
61
- return formatted_title
62
 
63
  def run_inference(model_type, config_path, start_check_point, input_dir, output_dir, device_ids="0"):
64
  command = [
@@ -114,8 +90,6 @@ def move_stems_to_parent(input_dir):
114
  new_instrumental_path = os.path.join(parent_dir, "instrumental.wav")
115
  print(f"Moving {instrumental_path} to {new_instrumental_path}")
116
  shutil.move(instrumental_path, new_instrumental_path)
117
- else:
118
- print(f"Instrumental file not found: {instrumental_path}")
119
 
120
  def combine_stems_for_all(input_dir):
121
  for subdir, _, _ in os.walk(input_dir):
@@ -172,29 +146,31 @@ def delete_folders_and_files(input_dir):
172
 
173
  print("Cleanup completed.")
174
 
175
- def process_audio(song_title):
176
  try:
177
- yield "Finding audio...", None
178
- if title_input == "":
179
- raise ValueError("Please enter a song title.")
180
 
181
- formatted_title = download_youtube_audio_by_title(song_title, False)
 
 
 
182
 
183
  yield "Starting SCNet inference...", None
184
  proc_folder_direct("scnet", "configs/config_scnet_other.yaml", "results/model_scnet_other.ckpt", f"{INPUT_FOLDER}/wav", OUTPUT_FOLDER)
185
 
186
  yield "Starting Mel Band Roformer inference...", None
187
  proc_folder_direct("mel_band_roformer", "configs/config_mel_band_roformer_vocals.yaml", "results/model_mel_band_roformer_vocals.ckpt", f"{INPUT_FOLDER}/wav", OUTPUT_FOLDER, extract_instrumental=True)
 
188
  yield "Starting HTDemucs inference...", None
189
  proc_folder_direct("htdemucs", "configs/config_htdemucs_bass.yaml", "results/model_htdemucs_bass.th", f"{INPUT_FOLDER}/wav", OUTPUT_FOLDER)
190
 
191
- source_path = f'{OUTPUT_FOLDER}{formatted_title}/mel_band_roformer/{formatted_title}_instrumental.wav'
192
- destination_path = f'{OUTPUT_FOLDER}{formatted_title}/mel_band_roformer/{formatted_title}.wav'
193
 
194
  os.rename(source_path, destination_path)
195
 
196
  yield "Starting BS Roformer inference...", None
197
- proc_folder_direct("bs_roformer", "configs/config_bs_roformer_instrumental.yaml", "results/model_bs_roformer_instrumental.ckpt", f'{OUTPUT_FOLDER}{formatted_title}/mel_band_roformer', OUTPUT_FOLDER)
198
 
199
  yield "Moving input files...", None
200
  delete_input_files(INPUT_FOLDER)
@@ -208,8 +184,7 @@ def process_audio(song_title):
208
  yield "Cleaning up...", None
209
  delete_folders_and_files(OUTPUT_FOLDER)
210
 
211
-
212
- yield f"Audio processing completed successfully.", f'{OUTPUT_FOLDER}{formatted_title}/{formatted_title}.MDS.wav'
213
  except Exception as e:
214
  error_msg = f"An error occurred: {str(e)}\n{traceback.format_exc()}"
215
  logging.error(error_msg)
@@ -219,26 +194,17 @@ with gr.Blocks() as demo:
219
  gr.Markdown("# Music Player and Processor")
220
 
221
  with gr.Row():
222
- title_input = gr.Textbox(label="Enter Song Title")
223
- play_button = gr.Button("Play")
224
-
225
- audio_output = gr.Audio(label="Audio Player")
226
-
227
- process_button = gr.Button("Process Audio")
228
 
229
  log_output = gr.Textbox(label="Processing Log", interactive=False)
230
  processed_audio_output = gr.Audio(label="Processed Audio")
231
 
232
- play_button.click(
233
- fn=download_youtube_audio_by_title,
234
- inputs=title_input,
235
- outputs=audio_output
236
- )
237
-
238
  process_button.click(
239
  fn=process_audio,
240
- inputs=title_input,
241
  outputs=[log_output, processed_audio_output],
242
  show_progress=True
243
  )
 
244
  demo.launch()
 
23
  wav_file.unlink()
24
  print(f"Deleted {wav_file}")
25
 
26
+ def process_uploaded_audio(file, state=True):
27
  if state:
28
  delete_input_files(INPUT_FOLDER)
29
 
30
+ sanitized_filename = sanitize_filename(file.name)
31
+ input_path = Path(INPUT_FOLDER) / "wav" / sanitized_filename
32
+ input_path.parent.mkdir(parents=True, exist_ok=True)
 
 
 
 
 
 
 
 
 
33
 
34
+ with open(input_path, 'wb') as f:
35
+ f.write(file.read())
36
+
37
+ return str(input_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  def run_inference(model_type, config_path, start_check_point, input_dir, output_dir, device_ids="0"):
40
  command = [
 
90
  new_instrumental_path = os.path.join(parent_dir, "instrumental.wav")
91
  print(f"Moving {instrumental_path} to {new_instrumental_path}")
92
  shutil.move(instrumental_path, new_instrumental_path)
 
 
93
 
94
  def combine_stems_for_all(input_dir):
95
  for subdir, _, _ in os.walk(input_dir):
 
146
 
147
  print("Cleanup completed.")
148
 
149
+ def process_audio(uploaded_file):
150
  try:
151
+ yield "Processing audio file...", None
 
 
152
 
153
+ if uploaded_file is None:
154
+ raise ValueError("Please upload a WAV file.")
155
+
156
+ file_path = process_uploaded_audio(uploaded_file, False)
157
 
158
  yield "Starting SCNet inference...", None
159
  proc_folder_direct("scnet", "configs/config_scnet_other.yaml", "results/model_scnet_other.ckpt", f"{INPUT_FOLDER}/wav", OUTPUT_FOLDER)
160
 
161
  yield "Starting Mel Band Roformer inference...", None
162
  proc_folder_direct("mel_band_roformer", "configs/config_mel_band_roformer_vocals.yaml", "results/model_mel_band_roformer_vocals.ckpt", f"{INPUT_FOLDER}/wav", OUTPUT_FOLDER, extract_instrumental=True)
163
+
164
  yield "Starting HTDemucs inference...", None
165
  proc_folder_direct("htdemucs", "configs/config_htdemucs_bass.yaml", "results/model_htdemucs_bass.th", f"{INPUT_FOLDER}/wav", OUTPUT_FOLDER)
166
 
167
+ source_path = f'{OUTPUT_FOLDER}/{file_path.stem}/mel_band_roformer/{file_path.stem}_instrumental.wav'
168
+ destination_path = f'{OUTPUT_FOLDER}/{file_path.stem}/mel_band_roformer/{file_path.stem}.wav'
169
 
170
  os.rename(source_path, destination_path)
171
 
172
  yield "Starting BS Roformer inference...", None
173
+ proc_folder_direct("bs_roformer", "configs/config_bs_roformer_instrumental.yaml", "results/model_bs_roformer_instrumental.ckpt", f'{OUTPUT_FOLDER}/{file_path.stem}/mel_band_roformer', OUTPUT_FOLDER)
174
 
175
  yield "Moving input files...", None
176
  delete_input_files(INPUT_FOLDER)
 
184
  yield "Cleaning up...", None
185
  delete_folders_and_files(OUTPUT_FOLDER)
186
 
187
+ yield f"Audio processing completed successfully.", f'{OUTPUT_FOLDER}/{file_path.stem}/{file_path.stem}.MDS.wav'
 
188
  except Exception as e:
189
  error_msg = f"An error occurred: {str(e)}\n{traceback.format_exc()}"
190
  logging.error(error_msg)
 
194
  gr.Markdown("# Music Player and Processor")
195
 
196
  with gr.Row():
197
+ file_input = gr.File(label="Upload WAV File", file_types=['wav'])
198
+ process_button = gr.Button("Process Audio")
 
 
 
 
199
 
200
  log_output = gr.Textbox(label="Processing Log", interactive=False)
201
  processed_audio_output = gr.Audio(label="Processed Audio")
202
 
 
 
 
 
 
 
203
  process_button.click(
204
  fn=process_audio,
205
+ inputs=file_input,
206
  outputs=[log_output, processed_audio_output],
207
  show_progress=True
208
  )
209
+
210
  demo.launch()