File size: 8,540 Bytes
1912645
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb40f59
1912645
00aa15b
 
 
 
 
 
 
 
 
 
eb40f59
 
 
 
00aa15b
 
 
 
 
 
 
 
 
 
 
 
eb40f59
00aa15b
 
eb40f59
00aa15b
 
 
eb40f59
00aa15b
 
 
 
 
 
 
 
 
 
 
 
eb40f59
 
 
00aa15b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb40f59
 
ed2622c
 
 
 
f6e8343
ed2622c
1912645
 
ed2622c
eb40f59
 
 
 
 
 
 
 
 
 
00aa15b
eb40f59
 
ed2622c
 
 
f6e8343
1912645
ed2622c
f6e8343
ed2622c
 
eb40f59
ed2622c
 
00aa15b
ed2622c
 
eb40f59
ed2622c
eb40f59
1912645
 
 
 
 
 
 
 
 
 
945ab77
eb40f59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
945ab77
1912645
 
 
abc0eca
ed2622c
 
1912645
 
945ab77
1912645
eb40f59
ed2622c
945ab77
1912645
bcd91cb
afe64d9
eb40f59
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
# import os
# import gradio as gr
# from scipy.io.wavfile import write
# import subprocess
# import torch

# from audio_separator import Separator  # Ensure this is correctly implemented

# def inference(audio):
#     os.makedirs("out", exist_ok=True)
#     audio_path = 'test.wav'
#     write(audio_path, audio[0], audio[1])
#     device = 'cuda' if torch.cuda.is_available() else 'cpu'
#     if device=='cuda':
#         use_cuda=True
#         print(f"Using device: {device}")
#     else:
#         use_cuda=False
#         print(f"Using device: {device}")
#     try:
        
#         # Using subprocess.run for better control
#         command = f"python3 -m demucs.separate -n htdemucs_6s -d {device} {audio_path} -o out"
#         process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#         print("Demucs script output:", process.stdout.decode())
#     except subprocess.CalledProcessError as e:
#         print("Error in Demucs script:", e.stderr.decode())
#         return None

#     try:
#         # Separating the stems using your custom separator
#         separator = Separator("./out/htdemucs_6s/test/vocals.wav", model_name='UVR_MDXNET_KARA_2', use_cuda=use_cuda, output_format='mp3')
#         primary_stem_path, secondary_stem_path = separator.separate()
#     except Exception as e:
#         print("Error in custom separation:", str(e))
#         return None

#     # Collecting all file paths
#     files = [f"./out/htdemucs_6s/test/{stem}.wav" for stem in ["vocals", "bass", "drums", "other", "piano", "guitar"]]
#     files.extend([secondary_stem_path,primary_stem_path ])

#     # Check if files exist
#     existing_files = [file for file in files if os.path.isfile(file)]
#     if not existing_files:
#         print("No files were created.")
#         return None

#     return existing_files

# # Gradio Interface
# title = "Source Separation Demo"
# description = "Music Source Separation in the Waveform Domain. To use it, simply upload your audio."
# gr.Interface(
#     inference, 
#     gr.components.Audio(type="numpy", label="Input"), 
#     [gr.components.Audio(type="filepath", label=stem) for stem in ["Full Vocals","Bass", "Drums", "Other", "Piano", "Guitar", "Lead Vocals", "Backing Vocals" ]],
#     title=title,
#     description=description,
# ).launch()

 

# import os
# import gradio as gr
# from scipy.io.wavfile import write
# import subprocess
# import torch

# # Assuming audio_separator is available in your environment
# from audio_separator import Separator  

# def inference(audio, vocals, bass, drums, other, piano, guitar, lead_vocals, backing_vocals):
#     # Initially, show the loading GIF
#     loading_gif_path = "7RwF.gif"
#     gr.Image(loading_gif_path,visible=True) 

#     os.makedirs("out", exist_ok=True)
#     audio_path = 'test.wav'
#     write(audio_path, audio[0], audio[1])
#     device = 'cuda' if torch.cuda.is_available() else 'cpu'
#     print(f"Using device: {device}")

#     try:
#         command = f"python3 -m demucs.separate -n htdemucs_6s -d {device} {audio_path} -o out"
#         process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#         print("Demucs script output:", process.stdout.decode())
#     except subprocess.CalledProcessError as e:
#         print("Error in Demucs script:", e.stderr.decode())
#         return [gr.Audio(visible=False)] * 8 + [loading_gif_path]

#     try:
#         separator = Separator("./out/htdemucs_6s/test/vocals.wav", model_name='UVR_MDXNET_KARA_2', use_cuda=device=='cuda', output_format='wav')
#         primary_stem_path, secondary_stem_path = separator.separate()
#     except Exception as e:
#         print("Error in custom separation:", str(e))
#         return [gr.Audio(visible=False)] * 8 + [loading_gif_path]

#     stem_paths = {
#         "vocals": "./out/htdemucs_6s/test/vocals.wav" if vocals else None,
#         "bass": "./out/htdemucs_6s/test/bass.wav" if bass else None,
#         "drums": "./out/htdemucs_6s/test/drums.wav" if drums else None,
#         "other": "./out/htdemucs_6s/test/other.wav" if other else None,
#         "piano": "./out/htdemucs_6s/test/piano.wav" if piano else None,
#         "guitar": "./out/htdemucs_6s/test/guitar.wav" if guitar else None,
#         "lead_vocals": primary_stem_path if lead_vocals else None,
#         "backing_vocals": secondary_stem_path if backing_vocals else None
#     }

#     # Once processing is done, hide the GIF by returning a transparent image
#     gr.Image(visible=False)
#     return [gr.Audio(stem_paths[stem], visible=bool(stem_paths[stem])) for stem in stem_paths]  

# # Define checkboxes for each stem
# checkbox_labels = ["Full Vocals", "Bass", "Drums", "Other", "Piano", "Guitar", "Lead Vocals", "Backing Vocals"]
# checkboxes = [gr.components.Checkbox(label=label) for label in checkbox_labels]

# # Gradio Interface
# title = "Source Separation Demo"
# description = "Music Source Separation in the Waveform Domain. Upload your audio to begin."
# iface = gr.Interface(
#     inference, 
#     [gr.components.Audio(type="numpy", label="Input")] + checkboxes,
#     [gr.Audio(label=label, visible=False) for label in checkbox_labels],
#     title=title,
#     description=description,
# )

# iface.launch()







import os
import gradio as gr
from scipy.io.wavfile import write
import subprocess
import torch

# Assuming audio_separator is available in your environment
from audio_separator import Separator  

# Global variable to track the display state of the GIF
show_loading_gif = True

def start_loading():
    global show_loading_gif
    show_loading_gif = True

def stop_loading():
    global show_loading_gif
    show_loading_gif = False

def process_audio(audio, vocals, bass, drums, other, piano, guitar, lead_vocals, backing_vocals):
    # Audio processing logic
    os.makedirs("out", exist_ok=True)
    audio_path = 'test.wav'
    write(audio_path, audio[0], audio[1])
    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    try:
        command = f"python3 -m demucs.separate -n htdemucs_6s -d {device} {audio_path} -o out"
        process = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    except subprocess.CalledProcessError as e:
        return None  # Handle the error appropriately

    try:
        separator = Separator("./out/htdemucs_6s/test/vocals.wav", model_name='UVR_MDXNET_KARA_2', use_cuda=device=='cuda', output_format='wav')
        primary_stem_path, secondary_stem_path = separator.separate()
    except Exception as e:
        return None  # Handle the error appropriately

    # Generate paths for the stems
    stem_paths = {
        "vocals": "./out/htdemucs_6s/test/vocals.wav" if vocals else None,
        "bass": "./out/htdemucs_6s/test/bass.wav" if bass else None,
        "drums": "./out/htdemucs_6s/test/drums.wav" if drums else None,
        "other": "./out/htdemucs_6s/test/other.wav" if other else None,
        "piano": "./out/htdemucs_6s/test/piano.wav" if piano else None,
        "guitar": "./out/htdemucs_6s/test/guitar.wav" if guitar else None,
        "lead_vocals": primary_stem_path if lead_vocals else None,
        "backing_vocals": secondary_stem_path if backing_vocals else None
    }

    return [stem_paths[stem] for stem in stem_paths]

def inference(audio, vocals, bass, drums, other, piano, guitar, lead_vocals, backing_vocals):
    global show_loading_gif

    # Start loading
    start_loading()

    # Call the main processing function
    audio_outputs = process_audio(audio, vocals, bass, drums, other, piano, guitar, lead_vocals, backing_vocals)

    # Stop loading
    stop_loading()

    # Return the outputs along with the loading GIF state
    loading_gif_path = "7RwF.gif" if show_loading_gif else ""
    return loading_gif_path, audio_outputs

# Define checkboxes for each stem
checkbox_labels = ["Full Vocals", "Bass", "Drums", "Other", "Piano", "Guitar", "Lead Vocals", "Backing Vocals"]
checkboxes = [gr.components.Checkbox(label=label) for label in checkbox_labels]

# Gradio Interface
title = "Source Separation Demo"
description = "Music Source Separation in the Waveform Domain. Upload your audio to begin."
iface = gr.Interface(
    inference, 
    [gr.components.Audio(type="numpy", label="Input")] + checkboxes,
    [gr.Image(), [gr.Audio(label=label, visible=False) for label in checkbox_labels]],
    title=title,
    description=description,
)

iface.launch()