Spaces:
Runtime error
Runtime error
Epsilon617
commited on
Commit
·
c2c7513
1
Parent(s):
8c952bb
add live
Browse files- __pycache__/app.cpython-310.pyc +0 -0
- app.py +60 -11
__pycache__/app.cpython-310.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-310.pyc and b/__pycache__/app.cpython-310.pyc differ
|
|
|
app.py
CHANGED
|
@@ -25,10 +25,14 @@ logger.addHandler(ch)
|
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
-
inputs = [
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# outputs = [gr.components.Textbox(), transcription_df]
|
| 33 |
title = "Output the tags of a (music) audio"
|
| 34 |
description = "An example of using MERT-95M-public to conduct music tagging."
|
|
@@ -74,16 +78,41 @@ def convert_audio(inputs, microphone):
|
|
| 74 |
# print(all_layer_hidden_states.shape) # [13 layer, Time steps, 768 feature_dim]
|
| 75 |
# logger.warning(all_layer_hidden_states.shape)
|
| 76 |
|
| 77 |
-
return device
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
# iface = gr.Interface(fn=convert_audio, inputs="audio", outputs="text")
|
| 81 |
-
# iface.launch()
|
| 82 |
|
| 83 |
audio_chunked = gr.Interface(
|
| 84 |
fn=convert_audio,
|
| 85 |
inputs=inputs,
|
| 86 |
-
outputs=
|
| 87 |
allow_flagging="never",
|
| 88 |
title=title,
|
| 89 |
description=description,
|
|
@@ -91,10 +120,30 @@ audio_chunked = gr.Interface(
|
|
| 91 |
examples=audio_examples,
|
| 92 |
)
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
demo = gr.Blocks()
|
| 96 |
with demo:
|
| 97 |
-
gr.TabbedInterface(
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
demo.launch(show_api=False)
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
+
inputs = [
|
| 29 |
+
gr.components.Audio(type="filepath", label="Add music audio file"),
|
| 30 |
+
gr.components.Audio(source="microphone", type="filepath"),
|
| 31 |
+
]
|
| 32 |
+
live_inputs = [
|
| 33 |
+
gr.components.Audio(source="microphone",streaming=True, type="filepath"),
|
| 34 |
+
]
|
| 35 |
+
# outputs = [gr.components.Textbox()]
|
| 36 |
# outputs = [gr.components.Textbox(), transcription_df]
|
| 37 |
title = "Output the tags of a (music) audio"
|
| 38 |
description = "An example of using MERT-95M-public to conduct music tagging."
|
|
|
|
| 78 |
# print(all_layer_hidden_states.shape) # [13 layer, Time steps, 768 feature_dim]
|
| 79 |
# logger.warning(all_layer_hidden_states.shape)
|
| 80 |
|
| 81 |
+
return f"device {device}\n sample reprensentation: {str(all_layer_hidden_states[12, 0, :10])}"
|
| 82 |
|
| 83 |
+
def live_convert_audio(microphone):
|
| 84 |
+
if (microphone is not None):
|
| 85 |
+
inputs = microphone
|
| 86 |
+
|
| 87 |
+
waveform, sample_rate = torchaudio.load(inputs)
|
| 88 |
+
|
| 89 |
+
resample_rate = processor.sampling_rate
|
| 90 |
+
|
| 91 |
+
# make sure the sample_rate aligned
|
| 92 |
+
if resample_rate != sample_rate:
|
| 93 |
+
print(f'setting rate from {sample_rate} to {resample_rate}')
|
| 94 |
+
resampler = T.Resample(sample_rate, resample_rate)
|
| 95 |
+
waveform = resampler(waveform)
|
| 96 |
+
|
| 97 |
+
waveform = waveform.view(-1,) # make it (n_sample, )
|
| 98 |
+
model_inputs = processor(waveform, sampling_rate=resample_rate, return_tensors="pt")
|
| 99 |
+
model_inputs.to(device)
|
| 100 |
+
with torch.no_grad():
|
| 101 |
+
model_outputs = model(**model_inputs, output_hidden_states=True)
|
| 102 |
+
|
| 103 |
+
# take a look at the output shape, there are 13 layers of representation
|
| 104 |
+
# each layer performs differently in different downstream tasks, you should choose empirically
|
| 105 |
+
all_layer_hidden_states = torch.stack(model_outputs.hidden_states).squeeze()
|
| 106 |
+
# print(all_layer_hidden_states.shape) # [13 layer, Time steps, 768 feature_dim]
|
| 107 |
+
# logger.warning(all_layer_hidden_states.shape)
|
| 108 |
+
|
| 109 |
+
return f"device {device}, sample reprensentation: {str(all_layer_hidden_states[12, 0, :10])}"
|
| 110 |
|
|
|
|
|
|
|
| 111 |
|
| 112 |
audio_chunked = gr.Interface(
|
| 113 |
fn=convert_audio,
|
| 114 |
inputs=inputs,
|
| 115 |
+
outputs=[gr.components.Textbox()],
|
| 116 |
allow_flagging="never",
|
| 117 |
title=title,
|
| 118 |
description=description,
|
|
|
|
| 120 |
examples=audio_examples,
|
| 121 |
)
|
| 122 |
|
| 123 |
+
live_audio_chunked = gr.Interface(
|
| 124 |
+
fn=live_convert_audio,
|
| 125 |
+
inputs=live_inputs,
|
| 126 |
+
outputs=[gr.components.Textbox()],
|
| 127 |
+
allow_flagging="never",
|
| 128 |
+
title=title,
|
| 129 |
+
description=description,
|
| 130 |
+
article=article,
|
| 131 |
+
# examples=audio_examples,
|
| 132 |
+
live=True,
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
|
| 136 |
demo = gr.Blocks()
|
| 137 |
with demo:
|
| 138 |
+
gr.TabbedInterface(
|
| 139 |
+
[
|
| 140 |
+
audio_chunked,
|
| 141 |
+
live_audio_chunked,
|
| 142 |
+
],
|
| 143 |
+
[
|
| 144 |
+
"Audio File or Recording",
|
| 145 |
+
"Live Streaming Music"
|
| 146 |
+
]
|
| 147 |
+
)
|
| 148 |
+
demo.queue(concurrency_count=1, max_size=5)
|
| 149 |
demo.launch(show_api=False)
|