Pecorized commited on
Commit
bcd91cb
·
1 Parent(s): 4dd16b8

simple back

Browse files
Files changed (1) hide show
  1. app.py +18 -85
app.py CHANGED
@@ -1,64 +1,3 @@
1
- # import os
2
- # import gradio as gr
3
- # from scipy.io.wavfile import write
4
- # import subprocess
5
- # import torch
6
-
7
- # from audio_separator import Separator # Ensure this is correctly implemented
8
-
9
- # def inference(audio):
10
- # os.makedirs("out", exist_ok=True)
11
- # audio_path = 'test.wav'
12
- # write(audio_path, audio[0], audio[1])
13
- # device = 'cuda' if torch.cuda.is_available() else 'cpu'
14
- # if device=='cuda':
15
- # use_cuda=True
16
- # print(f"Using device: {device}")
17
- # else:
18
- # use_cuda=False
19
- # print(f"Using device: {device}")
20
- # try:
21
-
22
- # # Using subprocess.run for better control
23
- # command = f"python3 -m demucs.separate -n htdemucs_6s -d {device} {audio_path} -o out"
24
- # process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
25
- # print("Demucs script output:", process.stdout.decode())
26
- # except subprocess.CalledProcessError as e:
27
- # print("Error in Demucs script:", e.stderr.decode())
28
- # return None
29
-
30
- # try:
31
- # # Separating the stems using your custom separator
32
- # separator = Separator("./out/htdemucs_6s/test/vocals.wav", model_name='UVR_MDXNET_KARA_2', use_cuda=use_cuda, output_format='mp3')
33
- # primary_stem_path, secondary_stem_path = separator.separate()
34
- # except Exception as e:
35
- # print("Error in custom separation:", str(e))
36
- # return None
37
-
38
- # # Collecting all file paths
39
- # files = [f"./out/htdemucs_6s/test/{stem}.wav" for stem in ["vocals", "bass", "drums", "other", "piano", "guitar"]]
40
- # files.extend([secondary_stem_path,primary_stem_path ])
41
-
42
- # # Check if files exist
43
- # existing_files = [file for file in files if os.path.isfile(file)]
44
- # if not existing_files:
45
- # print("No files were created.")
46
- # return None
47
-
48
- # return existing_files
49
-
50
- # # Gradio Interface
51
- # title = "Source Separation Demo"
52
- # description = "Music Source Separation in the Waveform Domain. To use it, simply upload your audio."
53
- # gr.Interface(
54
- # inference,
55
- # gr.components.Audio(type="numpy", label="Input"),
56
- # [gr.components.Audio(type="filepath", label=stem) for stem in ["Full Vocals","Bass", "Drums", "Other", "Piano", "Guitar", "Lead Vocals", "Backing Vocals" ]],
57
- # title=title,
58
- # description=description,
59
- # ).launch()
60
-
61
-
62
  import os
63
  import gradio as gr
64
  from scipy.io.wavfile import write
@@ -67,7 +6,7 @@ import torch
67
 
68
  from audio_separator import Separator # Ensure this is correctly implemented
69
 
70
- def inference(audio, vocals, bass, drums, other, piano, guitar, lead_vocals, backing_vocals):
71
  os.makedirs("out", exist_ok=True)
72
  audio_path = 'test.wav'
73
  write(audio_path, audio[0], audio[1])
@@ -78,8 +17,9 @@ def inference(audio, vocals, bass, drums, other, piano, guitar, lead_vocals, bac
78
  else:
79
  use_cuda=False
80
  print(f"Using device: {device}")
81
-
82
  try:
 
 
83
  command = f"python3 -m demucs.separate -n htdemucs_6s -d {device} {audio_path} -o out"
84
  process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
85
  print("Demucs script output:", process.stdout.decode())
@@ -88,40 +28,33 @@ def inference(audio, vocals, bass, drums, other, piano, guitar, lead_vocals, bac
88
  return None
89
 
90
  try:
 
91
  separator = Separator("./out/htdemucs_6s/test/vocals.wav", model_name='UVR_MDXNET_KARA_2', use_cuda=use_cuda, output_format='mp3')
92
  primary_stem_path, secondary_stem_path = separator.separate()
93
  except Exception as e:
94
  print("Error in custom separation:", str(e))
95
  return None
96
 
97
- stem_files = {
98
- "vocals": "./out/htdemucs_6s/test/vocals.wav",
99
- "bass": "./out/htdemucs_6s/test/bass.wav",
100
- "drums": "./out/htdemucs_6s/test/drums.wav",
101
- "other": "./out/htdemucs_6s/test/other.wav",
102
- "piano": "./out/htdemucs_6s/test/piano.wav",
103
- "guitar": "./out/htdemucs_6s/test/guitar.wav",
104
- "lead_vocals": primary_stem_path,
105
- "backing_vocals": secondary_stem_path
106
- }
107
 
108
- # Filter out unchecked stems
109
- selected_stems = [vocals, bass, drums, other, piano, guitar, lead_vocals, backing_vocals]
110
- output_files = [stem_files[stem] if selected_stems[i] and os.path.isfile(stem_files[stem]) else None for i, stem in enumerate(stem_files)]
111
-
112
- return output_files
113
 
114
- # Checkbox for each stem
115
- checkboxes = [gr.components.Checkbox(label=stem) for stem in ["Vocals", "Bass", "Drums", "Other", "Piano", "Guitar", "Lead Vocals", "Backing Vocals"]]
116
 
117
  # Gradio Interface
118
  title = "Source Separation Demo"
119
- description = "Music Source Separation in the Waveform Domain. To use it, simply upload your audio and select the stems you want to display."
120
  gr.Interface(
121
  inference,
122
- [gr.components.Audio(type="numpy", label="Input")] + checkboxes,
123
- [gr.components.Audio(type="filepath", label=stem) for stem in ["Vocals", "Bass", "Drums", "Other", "Piano", "Guitar", "Lead Vocals", "Backing Vocals"]],
124
  title=title,
125
  description=description,
126
- live=True # Enable live updates
127
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import gradio as gr
3
  from scipy.io.wavfile import write
 
6
 
7
  from audio_separator import Separator # Ensure this is correctly implemented
8
 
9
+ def inference(audio):
10
  os.makedirs("out", exist_ok=True)
11
  audio_path = 'test.wav'
12
  write(audio_path, audio[0], audio[1])
 
17
  else:
18
  use_cuda=False
19
  print(f"Using device: {device}")
 
20
  try:
21
+
22
+ # Using subprocess.run for better control
23
  command = f"python3 -m demucs.separate -n htdemucs_6s -d {device} {audio_path} -o out"
24
  process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
25
  print("Demucs script output:", process.stdout.decode())
 
28
  return None
29
 
30
  try:
31
+ # Separating the stems using your custom separator
32
  separator = Separator("./out/htdemucs_6s/test/vocals.wav", model_name='UVR_MDXNET_KARA_2', use_cuda=use_cuda, output_format='mp3')
33
  primary_stem_path, secondary_stem_path = separator.separate()
34
  except Exception as e:
35
  print("Error in custom separation:", str(e))
36
  return None
37
 
38
+ # Collecting all file paths
39
+ files = [f"./out/htdemucs_6s/test/{stem}.wav" for stem in ["vocals", "bass", "drums", "other", "piano", "guitar"]]
40
+ files.extend([secondary_stem_path,primary_stem_path ])
 
 
 
 
 
 
 
41
 
42
+ # Check if files exist
43
+ existing_files = [file for file in files if os.path.isfile(file)]
44
+ if not existing_files:
45
+ print("No files were created.")
46
+ return None
47
 
48
+ return existing_files
 
49
 
50
  # Gradio Interface
51
  title = "Source Separation Demo"
52
+ description = "Music Source Separation in the Waveform Domain. To use it, simply upload your audio."
53
  gr.Interface(
54
  inference,
55
+ gr.components.Audio(type="numpy", label="Input"),
56
+ [gr.components.Audio(type="filepath", label=stem) for stem in ["Full Vocals","Bass", "Drums", "Other", "Piano", "Guitar", "Lead Vocals", "Backing Vocals" ]],
57
  title=title,
58
  description=description,
59
+ ).launch()
60
+