Spaces:
Runtime error
Runtime error
update app
Browse files- app.py +59 -16
- inference.py +16 -19
app.py
CHANGED
|
@@ -9,17 +9,12 @@ from config import args
|
|
| 9 |
|
| 10 |
mastering_transfer = MasteringStyleTransfer(args)
|
| 11 |
|
| 12 |
-
def process_audio(input_audio, reference_audio, perform_ito):
|
| 13 |
# Process the audio files
|
| 14 |
output_audio, predicted_params, ito_output_audio, ito_predicted_params, _, sr, _ = mastering_transfer.process_audio(
|
| 15 |
-
input_audio, reference_audio, reference_audio, {}, perform_ito
|
| 16 |
)
|
| 17 |
|
| 18 |
-
# Save the output audio
|
| 19 |
-
sf.write("output_mastered.wav", output_audio.T, sr)
|
| 20 |
-
if ito_output_audio is not None:
|
| 21 |
-
sf.write("ito_output_mastered.wav", ito_output_audio.T, sr)
|
| 22 |
-
|
| 23 |
# Generate parameter output strings
|
| 24 |
param_output = mastering_transfer.get_param_output_string(predicted_params)
|
| 25 |
ito_param_output = mastering_transfer.get_param_output_string(ito_predicted_params) if ito_predicted_params is not None else "ITO not performed"
|
|
@@ -29,10 +24,25 @@ def process_audio(input_audio, reference_audio, perform_ito):
|
|
| 29 |
|
| 30 |
return "output_mastered.wav", "ito_output_mastered.wav" if ito_output_audio is not None else None, param_output, ito_param_output, top_10_diff
|
| 31 |
|
| 32 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
input_audio = download_youtube_audio(input_url)
|
| 34 |
reference_audio = download_youtube_audio(reference_url)
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
gr.Markdown("# Mastering Style Transfer Demo")
|
|
@@ -40,28 +50,61 @@ with gr.Blocks() as demo:
|
|
| 40 |
input_audio = gr.Audio(label="Input Audio")
|
| 41 |
reference_audio = gr.Audio(label="Reference Audio")
|
| 42 |
perform_ito = gr.Checkbox(label="Perform ITO")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
submit_button = gr.Button("Process")
|
| 44 |
output_audio = gr.Audio(label="Output Audio")
|
| 45 |
ito_output_audio = gr.Audio(label="ITO Output Audio")
|
| 46 |
param_output = gr.Textbox(label="Predicted Parameters", lines=10)
|
| 47 |
ito_param_output = gr.Textbox(label="ITO Predicted Parameters", lines=10)
|
| 48 |
top_10_diff = gr.Textbox(label="Top 10 Parameter Differences", lines=10)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
with gr.Tab("YouTube URLs"):
|
| 54 |
input_url = gr.Textbox(label="Input YouTube URL")
|
| 55 |
reference_url = gr.Textbox(label="Reference YouTube URL")
|
| 56 |
perform_ito_yt = gr.Checkbox(label="Perform ITO")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
submit_button_yt = gr.Button("Process")
|
| 58 |
output_audio_yt = gr.Audio(label="Output Audio")
|
| 59 |
ito_output_audio_yt = gr.Audio(label="ITO Output Audio")
|
| 60 |
param_output_yt = gr.Textbox(label="Predicted Parameters", lines=10)
|
| 61 |
ito_param_output_yt = gr.Textbox(label="ITO Predicted Parameters", lines=10)
|
| 62 |
top_10_diff_yt = gr.Textbox(label="Top 10 Parameter Differences", lines=10)
|
| 63 |
-
|
| 64 |
-
inputs=[input_url, reference_url, perform_ito_yt],
|
| 65 |
-
outputs=[output_audio_yt, ito_output_audio_yt, param_output_yt, ito_param_output_yt, top_10_diff_yt])
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
demo.launch()
|
|
|
|
| 9 |
|
| 10 |
mastering_transfer = MasteringStyleTransfer(args)
|
| 11 |
|
| 12 |
+
def process_audio(input_audio, reference_audio, perform_ito, ito_reference_audio=None):
|
| 13 |
# Process the audio files
|
| 14 |
output_audio, predicted_params, ito_output_audio, ito_predicted_params, _, sr, _ = mastering_transfer.process_audio(
|
| 15 |
+
input_audio, reference_audio, ito_reference_audio if ito_reference_audio else reference_audio, {}, perform_ito
|
| 16 |
)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Generate parameter output strings
|
| 19 |
param_output = mastering_transfer.get_param_output_string(predicted_params)
|
| 20 |
ito_param_output = mastering_transfer.get_param_output_string(ito_predicted_params) if ito_predicted_params is not None else "ITO not performed"
|
|
|
|
| 24 |
|
| 25 |
return "output_mastered.wav", "ito_output_mastered.wav" if ito_output_audio is not None else None, param_output, ito_param_output, top_10_diff
|
| 26 |
|
| 27 |
+
def process_with_ito(input_audio, reference_audio, perform_ito, use_same_reference, ito_reference_audio):
|
| 28 |
+
ito_ref = reference_audio if use_same_reference else ito_reference_audio
|
| 29 |
+
return process_audio(input_audio, reference_audio, perform_ito, ito_ref)
|
| 30 |
+
|
| 31 |
+
def process_youtube_with_ito(input_url, reference_url, perform_ito, use_same_reference, ito_reference_url):
|
| 32 |
input_audio = download_youtube_audio(input_url)
|
| 33 |
reference_audio = download_youtube_audio(reference_url)
|
| 34 |
+
ito_ref = reference_audio if use_same_reference else download_youtube_audio(ito_reference_url)
|
| 35 |
+
|
| 36 |
+
output_audio, predicted_params, ito_output_audio, ito_predicted_params, ito_log, sr, _ = mastering_transfer.process_audio(
|
| 37 |
+
input_audio, reference_audio, ito_ref, {}, perform_ito, log_ito=True
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
param_output = mastering_transfer.get_param_output_string(predicted_params)
|
| 41 |
+
ito_param_output = mastering_transfer.get_param_output_string(ito_predicted_params) if ito_predicted_params is not None else "ITO not performed"
|
| 42 |
+
top_10_diff = mastering_transfer.get_top_10_diff_string(predicted_params, ito_predicted_params) if ito_predicted_params is not None else "ITO not performed"
|
| 43 |
+
|
| 44 |
+
return "output_mastered_yt.wav", "ito_output_mastered_yt.wav" if ito_output_audio is not None else None, param_output, ito_param_output, top_10_diff, ito_log
|
| 45 |
+
|
| 46 |
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
gr.Markdown("# Mastering Style Transfer Demo")
|
|
|
|
| 50 |
input_audio = gr.Audio(label="Input Audio")
|
| 51 |
reference_audio = gr.Audio(label="Reference Audio")
|
| 52 |
perform_ito = gr.Checkbox(label="Perform ITO")
|
| 53 |
+
with gr.Column(visible=False) as ito_options:
|
| 54 |
+
use_same_reference = gr.Checkbox(label="Use same reference audio for ITO", value=True)
|
| 55 |
+
ito_reference_audio = gr.Audio(label="ITO Reference Audio", visible=False)
|
| 56 |
+
|
| 57 |
+
def update_ito_options(perform_ito):
|
| 58 |
+
return gr.Column.update(visible=perform_ito)
|
| 59 |
+
|
| 60 |
+
def update_ito_reference(use_same):
|
| 61 |
+
return gr.Audio.update(visible=not use_same)
|
| 62 |
+
|
| 63 |
+
perform_ito.change(fn=update_ito_options, inputs=perform_ito, outputs=ito_options)
|
| 64 |
+
use_same_reference.change(fn=update_ito_reference, inputs=use_same_reference, outputs=ito_reference_audio)
|
| 65 |
+
|
| 66 |
submit_button = gr.Button("Process")
|
| 67 |
output_audio = gr.Audio(label="Output Audio")
|
| 68 |
ito_output_audio = gr.Audio(label="ITO Output Audio")
|
| 69 |
param_output = gr.Textbox(label="Predicted Parameters", lines=10)
|
| 70 |
ito_param_output = gr.Textbox(label="ITO Predicted Parameters", lines=10)
|
| 71 |
top_10_diff = gr.Textbox(label="Top 10 Parameter Differences", lines=10)
|
| 72 |
+
|
| 73 |
+
submit_button.click(
|
| 74 |
+
process_with_ito,
|
| 75 |
+
inputs=[input_audio, reference_audio, perform_ito, use_same_reference, ito_reference_audio],
|
| 76 |
+
outputs=[output_audio, ito_output_audio, param_output, ito_param_output, top_10_diff]
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
with gr.Tab("YouTube URLs"):
|
| 80 |
input_url = gr.Textbox(label="Input YouTube URL")
|
| 81 |
reference_url = gr.Textbox(label="Reference YouTube URL")
|
| 82 |
perform_ito_yt = gr.Checkbox(label="Perform ITO")
|
| 83 |
+
with gr.Column(visible=False) as ito_options_yt:
|
| 84 |
+
use_same_reference_yt = gr.Checkbox(label="Use same reference audio for ITO", value=True)
|
| 85 |
+
ito_reference_url = gr.Textbox(label="ITO Reference YouTube URL", visible=False)
|
| 86 |
+
|
| 87 |
+
def update_ito_options_yt(perform_ito):
|
| 88 |
+
return gr.Column.update(visible=perform_ito)
|
| 89 |
+
|
| 90 |
+
def update_ito_reference_yt(use_same):
|
| 91 |
+
return gr.Textbox.update(visible=not use_same)
|
| 92 |
+
|
| 93 |
+
perform_ito_yt.change(fn=update_ito_options_yt, inputs=perform_ito_yt, outputs=ito_options_yt)
|
| 94 |
+
use_same_reference_yt.change(fn=update_ito_reference_yt, inputs=use_same_reference_yt, outputs=ito_reference_url)
|
| 95 |
+
|
| 96 |
submit_button_yt = gr.Button("Process")
|
| 97 |
output_audio_yt = gr.Audio(label="Output Audio")
|
| 98 |
ito_output_audio_yt = gr.Audio(label="ITO Output Audio")
|
| 99 |
param_output_yt = gr.Textbox(label="Predicted Parameters", lines=10)
|
| 100 |
ito_param_output_yt = gr.Textbox(label="ITO Predicted Parameters", lines=10)
|
| 101 |
top_10_diff_yt = gr.Textbox(label="Top 10 Parameter Differences", lines=10)
|
| 102 |
+
ito_log_yt = gr.Textbox(label="ITO Log", lines=20)
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
submit_button_yt.click(
|
| 105 |
+
process_youtube_with_ito,
|
| 106 |
+
inputs=[input_url, reference_url, perform_ito_yt, use_same_reference_yt, ito_reference_url],
|
| 107 |
+
outputs=[output_audio_yt, ito_output_audio_yt, param_output_yt, ito_param_output_yt, top_10_diff_yt, ito_log_yt]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
demo.launch()
|
inference.py
CHANGED
|
@@ -104,13 +104,7 @@ class MasteringStyleTransfer:
|
|
| 104 |
|
| 105 |
return min_loss_output, min_loss_params, min_loss_embedding, min_loss_step + 1
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
def process_audio(self, input_path, reference_path, ito_reference_path, ito_config, perform_ito):
|
| 110 |
-
input_audio, sr = sf.read(input_path)
|
| 111 |
-
reference_audio, _ = sf.read(reference_path)
|
| 112 |
-
ito_reference_audio, _ = sf.read(ito_reference_path)
|
| 113 |
-
|
| 114 |
input_audio, reference_audio, ito_reference_audio = [
|
| 115 |
np.stack([audio, audio]) if audio.ndim == 1 else audio.transpose(1,0)
|
| 116 |
for audio in [input_audio, reference_audio, ito_reference_audio]
|
|
@@ -123,21 +117,24 @@ class MasteringStyleTransfer:
|
|
| 123 |
reference_feature = self.get_reference_embedding(reference_tensor)
|
| 124 |
|
| 125 |
output_audio, predicted_params = self.mastering_style_transfer(input_tensor, reference_feature)
|
| 126 |
-
|
| 127 |
if perform_ito:
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
else:
|
| 136 |
-
ito_output_audio
|
| 137 |
-
|
| 138 |
-
|
| 139 |
|
| 140 |
-
return output_audio, predicted_params, ito_output_audio, ito_predicted_params,
|
| 141 |
|
| 142 |
def print_param_difference(self, initial_params, ito_params):
|
| 143 |
all_diffs = []
|
|
|
|
| 104 |
|
| 105 |
return min_loss_output, min_loss_params, min_loss_embedding, min_loss_step + 1
|
| 106 |
|
| 107 |
+
def process_audio(self, input_audio, reference_audio, ito_reference_audio, params, perform_ito, log_ito=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
input_audio, reference_audio, ito_reference_audio = [
|
| 109 |
np.stack([audio, audio]) if audio.ndim == 1 else audio.transpose(1,0)
|
| 110 |
for audio in [input_audio, reference_audio, ito_reference_audio]
|
|
|
|
| 117 |
reference_feature = self.get_reference_embedding(reference_tensor)
|
| 118 |
|
| 119 |
output_audio, predicted_params = self.mastering_style_transfer(input_tensor, reference_feature)
|
|
|
|
| 120 |
if perform_ito:
|
| 121 |
+
ito_log = []
|
| 122 |
+
for i in range(self.args.max_iter_ito):
|
| 123 |
+
loss, ito_predicted_params = self.ito_step(input_audio, ito_reference_audio, predicted_params)
|
| 124 |
+
if log_ito:
|
| 125 |
+
top_10_diff = self.get_top_10_diff(predicted_params, ito_predicted_params)
|
| 126 |
+
log_entry = f"Iteration {i+1}, Loss: {loss:.4f}\nTop 10 parameter differences:\n{top_10_diff}\n"
|
| 127 |
+
ito_log.append(log_entry)
|
| 128 |
+
predicted_params = ito_predicted_params
|
| 129 |
+
|
| 130 |
+
ito_output_audio = self.converter.convert(input_audio, predicted_params)
|
| 131 |
+
ito_log = "\n".join(ito_log) if log_ito else None
|
| 132 |
else:
|
| 133 |
+
ito_output_audio = None
|
| 134 |
+
ito_predicted_params = None
|
| 135 |
+
ito_log = None
|
| 136 |
|
| 137 |
+
return output_audio, predicted_params, ito_output_audio, ito_predicted_params, ito_log, sr, duration
|
| 138 |
|
| 139 |
def print_param_difference(self, initial_params, ito_params):
|
| 140 |
all_diffs = []
|