Sutirtha commited on
Commit
8e60c3b
·
1 Parent(s): 232d8ce

Updated full code

Browse files
Files changed (1) hide show
  1. app.py +39 -55
app.py CHANGED
@@ -1,67 +1,51 @@
1
  import os
2
  import gradio as gr
3
  from scipy.io.wavfile import write
4
- import subprocess
5
-
6
-
7
- from audio_separator import Separator
8
-
9
 
 
10
 
11
  def inference(audio):
12
  os.makedirs("out", exist_ok=True)
13
- write('test.wav', audio[0], audio[1])
14
-
15
- command = "python3 -m demucs.separate -n htdemucs_6s -d cpu test.wav -o out"
16
- process = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
17
- print("Demucs script output:", process.stdout.decode())
18
-
19
- os.makedirs("out", exist_ok=True)
20
- write('test.wav', audio[0], audio[1])
21
- result = os.system("python3 -m demucs.separate -n htdemucs_6s -d cpu test.wav -o out")
22
- print(f"Demucs script result: {result}")
23
-
24
- # Initialize the Separator with the audio file and model name
25
- separator = Separator("./out/mdx_extra_q/test/vocals.wav", model_name='UVR_MDXNET_KARA_2',use_cuda=False,output_format='mp3')
26
- primary_stem_path, secondary_stem_path = separator.separate()
27
-
28
- print(f'Primary stem saved at {primary_stem_path}')
29
- print(f'Secondary stem saved at {secondary_stem_path}')
30
-
31
- # Check if files exist before returning
32
- files = ["./out/htdemucs_6s/test/vocals.wav",
33
- secondary_stem_path,
34
- primary_stem_path,
35
- "./out/htdemucs_6s/test/bass.wav",
36
- "./out/htdemucs_6s/test/drums.wav",
37
- "./out/htdemucs_6s/test/other.wav",
38
- "./out/htdemucs_6s/test/piano.wav",
39
- "./out/htdemucs_6s/test/guitar.wav",
40
- ]
41
-
42
-
43
- for file in files:
44
- if not os.path.isfile(file):
45
- print(f"File not found: {file}")
46
- else:
47
- print(f"File exists: {file}")
48
-
49
- return files
50
-
51
- title = "Source Sepration 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="Full Vocals"),
57
- gr.components.Audio(type="filepath", label="Lead Vocals"),
58
- gr.components.Audio(type="filepath", label="Chorus"),
59
- gr.components.Audio(type="filepath", label="Bass"),
60
- gr.components.Audio(type="filepath", label="Drums"),
61
- gr.components.Audio(type="filepath", label="Other"),
62
- gr.components.Audio(type="filepath", label="Piano"),
63
- gr.components.Audio(type="filepath", label="Guitar"),
64
- ],
65
  title=title,
66
  description=description,
67
- ).launch(share=True)
 
1
  import os
2
  import gradio as gr
3
  from scipy.io.wavfile import write
4
+ import subprocess
 
 
 
 
5
 
6
+ from audio_separator import Separator # Ensure this is correctly implemented
7
 
8
  def inference(audio):
9
  os.makedirs("out", exist_ok=True)
10
+ audio_path = 'test.wav'
11
+ write(audio_path, audio[0], audio[1])
12
+
13
+ try:
14
+ # Using subprocess.run for better control
15
+ command = f"python3 -m demucs.separate -n htdemucs_6s -d cpu {audio_path} -o out"
16
+ process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
17
+ print("Demucs script output:", process.stdout.decode())
18
+ except subprocess.CalledProcessError as e:
19
+ print("Error in Demucs script:", e.stderr.decode())
20
+ return None
21
+
22
+ try:
23
+ # Separating the stems using your custom separator
24
+ separator = Separator("./out/htdemucs_6s/test/vocals.wav", model_name='UVR_MDXNET_KARA_2', use_cuda=False, output_format='mp3')
25
+ primary_stem_path, secondary_stem_path = separator.separate()
26
+ except Exception as e:
27
+ print("Error in custom separation:", str(e))
28
+ return None
29
+
30
+ # Collecting all file paths
31
+ files = [f"./out/htdemucs_6s/test/{stem}.wav" for stem in ["vocals", "bass", "drums", "other", "piano", "guitar"]]
32
+ files.extend([primary_stem_path, secondary_stem_path])
33
+
34
+ # Check if files exist
35
+ existing_files = [file for file in files if os.path.isfile(file)]
36
+ if not existing_files:
37
+ print("No files were created.")
38
+ return None
39
+
40
+ return existing_files
41
+
42
+ # Gradio Interface
43
+ title = "Source Separation Demo"
44
+ description = "Music Source Separation in the Waveform Domain. To use it, simply upload your audio."
 
 
 
 
 
45
  gr.Interface(
46
  inference,
47
  gr.components.Audio(type="numpy", label="Input"),
48
+ [gr.components.Audio(type="filepath", label=stem) for stem in ["Full Vocals", "Lead Vocals", "Chorus", "Bass", "Drums", "Other", "Piano", "Guitar"]],
 
 
 
 
 
 
 
 
49
  title=title,
50
  description=description,
51
+ ).launch()