Spaces:
Running
on
Zero
Running
on
Zero
feat!: Add WAV file upload functionality and remove YouTube download option
Browse files
app.py
CHANGED
@@ -23,25 +23,20 @@ def delete_input_files(input_dir):
|
|
23 |
wav_file.unlink()
|
24 |
print(f"Deleted {wav_file}")
|
25 |
|
26 |
-
def
|
27 |
-
if
|
28 |
-
|
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 |
-
# Check if the file object has a `read` method, otherwise handle it differently.
|
35 |
-
if hasattr(file, 'read'):
|
36 |
-
file_content = file.read()
|
37 |
-
else:
|
38 |
-
# Assuming `file` is a NamedString or similar, with the content directly as a string
|
39 |
-
file_content = file.value # or file['value'] if it's a dictionary-like object
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
def run_inference(model_type, config_path, start_check_point, input_dir, output_dir, device_ids="0"):
|
47 |
command = [
|
@@ -97,6 +92,8 @@ def move_stems_to_parent(input_dir):
|
|
97 |
new_instrumental_path = os.path.join(parent_dir, "instrumental.wav")
|
98 |
print(f"Moving {instrumental_path} to {new_instrumental_path}")
|
99 |
shutil.move(instrumental_path, new_instrumental_path)
|
|
|
|
|
100 |
|
101 |
def combine_stems_for_all(input_dir):
|
102 |
for subdir, _, _ in os.walk(input_dir):
|
@@ -155,12 +152,14 @@ def delete_folders_and_files(input_dir):
|
|
155 |
|
156 |
def process_audio(uploaded_file):
|
157 |
try:
|
158 |
-
yield "Processing audio
|
159 |
|
160 |
-
if uploaded_file
|
|
|
|
|
|
|
|
|
161 |
raise ValueError("Please upload a WAV file.")
|
162 |
-
|
163 |
-
file_path = process_uploaded_audio(uploaded_file, False)
|
164 |
|
165 |
yield "Starting SCNet inference...", None
|
166 |
proc_folder_direct("scnet", "configs/config_scnet_other.yaml", "results/model_scnet_other.ckpt", f"{INPUT_FOLDER}/wav", OUTPUT_FOLDER)
|
@@ -171,13 +170,13 @@ def process_audio(uploaded_file):
|
|
171 |
yield "Starting HTDemucs inference...", None
|
172 |
proc_folder_direct("htdemucs", "configs/config_htdemucs_bass.yaml", "results/model_htdemucs_bass.th", f"{INPUT_FOLDER}/wav", OUTPUT_FOLDER)
|
173 |
|
174 |
-
source_path = f'{OUTPUT_FOLDER}
|
175 |
-
destination_path = f'{OUTPUT_FOLDER}
|
176 |
-
|
177 |
os.rename(source_path, destination_path)
|
178 |
-
|
179 |
yield "Starting BS Roformer inference...", None
|
180 |
-
proc_folder_direct("bs_roformer", "configs/config_bs_roformer_instrumental.yaml", "results/model_bs_roformer_instrumental.ckpt", f'{OUTPUT_FOLDER}
|
181 |
|
182 |
yield "Moving input files...", None
|
183 |
delete_input_files(INPUT_FOLDER)
|
@@ -191,27 +190,35 @@ def process_audio(uploaded_file):
|
|
191 |
yield "Cleaning up...", None
|
192 |
delete_folders_and_files(OUTPUT_FOLDER)
|
193 |
|
194 |
-
yield f"Audio processing completed successfully.", f'{OUTPUT_FOLDER}
|
195 |
except Exception as e:
|
196 |
error_msg = f"An error occurred: {str(e)}\n{traceback.format_exc()}"
|
197 |
logging.error(error_msg)
|
198 |
yield error_msg, None
|
199 |
|
200 |
with gr.Blocks() as demo:
|
201 |
-
gr.Markdown("# Music
|
|
|
|
|
|
|
|
|
202 |
|
203 |
-
|
204 |
-
file_input = gr.File(label="Upload WAV File", file_types=['wav'])
|
205 |
-
process_button = gr.Button("Process Audio")
|
206 |
|
207 |
log_output = gr.Textbox(label="Processing Log", interactive=False)
|
208 |
processed_audio_output = gr.Audio(label="Processed Audio")
|
209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
process_button.click(
|
211 |
fn=process_audio,
|
212 |
-
inputs=
|
213 |
outputs=[log_output, processed_audio_output],
|
214 |
show_progress=True
|
215 |
)
|
216 |
-
|
217 |
demo.launch()
|
|
|
23 |
wav_file.unlink()
|
24 |
print(f"Deleted {wav_file}")
|
25 |
|
26 |
+
def handle_file_upload(file):
|
27 |
+
if file is None:
|
28 |
+
return None, "No file uploaded"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
filename = Path(file.name).stem
|
31 |
+
formatted_title = sanitize_filename(filename)
|
32 |
+
|
33 |
+
input_path = os.path.join(INPUT_FOLDER, "wav", f"{formatted_title}.wav")
|
34 |
+
os.makedirs(os.path.dirname(input_path), exist_ok=True)
|
35 |
+
|
36 |
+
with open(input_path, "wb") as f:
|
37 |
+
f.write(file.read())
|
38 |
+
|
39 |
+
return input_path, formatted_title
|
40 |
|
41 |
def run_inference(model_type, config_path, start_check_point, input_dir, output_dir, device_ids="0"):
|
42 |
command = [
|
|
|
92 |
new_instrumental_path = os.path.join(parent_dir, "instrumental.wav")
|
93 |
print(f"Moving {instrumental_path} to {new_instrumental_path}")
|
94 |
shutil.move(instrumental_path, new_instrumental_path)
|
95 |
+
else:
|
96 |
+
print(f"Instrumental file not found: {instrumental_path}")
|
97 |
|
98 |
def combine_stems_for_all(input_dir):
|
99 |
for subdir, _, _ in os.walk(input_dir):
|
|
|
152 |
|
153 |
def process_audio(uploaded_file):
|
154 |
try:
|
155 |
+
yield "Processing audio...", None
|
156 |
|
157 |
+
if uploaded_file:
|
158 |
+
input_path, formatted_title = handle_file_upload(uploaded_file)
|
159 |
+
if input_path is None:
|
160 |
+
raise ValueError("File upload failed.")
|
161 |
+
else:
|
162 |
raise ValueError("Please upload a WAV file.")
|
|
|
|
|
163 |
|
164 |
yield "Starting SCNet inference...", None
|
165 |
proc_folder_direct("scnet", "configs/config_scnet_other.yaml", "results/model_scnet_other.ckpt", f"{INPUT_FOLDER}/wav", OUTPUT_FOLDER)
|
|
|
170 |
yield "Starting HTDemucs inference...", None
|
171 |
proc_folder_direct("htdemucs", "configs/config_htdemucs_bass.yaml", "results/model_htdemucs_bass.th", f"{INPUT_FOLDER}/wav", OUTPUT_FOLDER)
|
172 |
|
173 |
+
source_path = f'{OUTPUT_FOLDER}{formatted_title}/mel_band_roformer/{formatted_title}_instrumental.wav'
|
174 |
+
destination_path = f'{OUTPUT_FOLDER}{formatted_title}/mel_band_roformer/{formatted_title}.wav'
|
175 |
+
|
176 |
os.rename(source_path, destination_path)
|
177 |
+
|
178 |
yield "Starting BS Roformer inference...", None
|
179 |
+
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)
|
180 |
|
181 |
yield "Moving input files...", None
|
182 |
delete_input_files(INPUT_FOLDER)
|
|
|
190 |
yield "Cleaning up...", None
|
191 |
delete_folders_and_files(OUTPUT_FOLDER)
|
192 |
|
193 |
+
yield f"Audio processing completed successfully.", f'{OUTPUT_FOLDER}{formatted_title}/{formatted_title}.MDS.wav'
|
194 |
except Exception as e:
|
195 |
error_msg = f"An error occurred: {str(e)}\n{traceback.format_exc()}"
|
196 |
logging.error(error_msg)
|
197 |
yield error_msg, None
|
198 |
|
199 |
with gr.Blocks() as demo:
|
200 |
+
gr.Markdown("# Music Player and Processor")
|
201 |
+
|
202 |
+
file_upload = gr.File(label="Upload WAV file", file_types=[".wav"])
|
203 |
+
|
204 |
+
audio_output = gr.Audio(label="Audio Player")
|
205 |
|
206 |
+
process_button = gr.Button("Process Audio")
|
|
|
|
|
207 |
|
208 |
log_output = gr.Textbox(label="Processing Log", interactive=False)
|
209 |
processed_audio_output = gr.Audio(label="Processed Audio")
|
210 |
|
211 |
+
file_upload.change(
|
212 |
+
fn=lambda file: file.name if file else None,
|
213 |
+
inputs=file_upload,
|
214 |
+
outputs=audio_output
|
215 |
+
)
|
216 |
+
|
217 |
process_button.click(
|
218 |
fn=process_audio,
|
219 |
+
inputs=file_upload,
|
220 |
outputs=[log_output, processed_audio_output],
|
221 |
show_progress=True
|
222 |
)
|
223 |
+
|
224 |
demo.launch()
|