ernestchu
commited on
Commit
·
96edf76
1
Parent(s):
f6c8d4d
add yt functionality
Browse files- .gitignore +1 -0
- app.py +35 -5
.gitignore
CHANGED
|
@@ -4,4 +4,5 @@ __pycache__
|
|
| 4 |
*.swp
|
| 5 |
*.egg-info
|
| 6 |
build
|
|
|
|
| 7 |
|
|
|
|
| 4 |
*.swp
|
| 5 |
*.egg-info
|
| 6 |
build
|
| 7 |
+
yt-audio
|
| 8 |
|
app.py
CHANGED
|
@@ -2,10 +2,12 @@ import os
|
|
| 2 |
from tsmnet import Stretcher
|
| 3 |
import gradio as gr
|
| 4 |
from gradio import processing_utils
|
| 5 |
-
import torch
|
| 6 |
import torchaudio
|
|
|
|
| 7 |
|
| 8 |
model_root = './weights'
|
|
|
|
| 9 |
available_models = ['speech', 'pop-music', 'classical-music']
|
| 10 |
working_sr = 22050
|
| 11 |
|
|
@@ -15,13 +17,40 @@ def prepare_models():
|
|
| 15 |
for weight in available_models
|
| 16 |
}
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def prepare_audio_file(rec, audio_file, yt_url):
|
| 19 |
if rec is not None:
|
| 20 |
return rec
|
| 21 |
if audio_file is not None:
|
| 22 |
return audio_file
|
| 23 |
if yt_url != '':
|
| 24 |
-
|
| 25 |
else:
|
| 26 |
raise gr.Error('No audio found!')
|
| 27 |
|
|
@@ -45,6 +74,7 @@ def run(rec, audio_file, yt_url, speed, model, start_time, end_time):
|
|
| 45 |
# @@@@@@@ Start of the program @@@@@@@@
|
| 46 |
|
| 47 |
models = prepare_models()
|
|
|
|
| 48 |
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
gr.Markdown('# TSM-Net')
|
|
@@ -56,7 +86,7 @@ with gr.Blocks() as demo:
|
|
| 56 |
with gr.Tab('From file'):
|
| 57 |
audio_file_box = gr.Audio(label='Audio sample', type='filepath')
|
| 58 |
with gr.Tab('From YouTube'):
|
| 59 |
-
yt_url_box = gr.Textbox(label='YouTube URL', placeholder='
|
| 60 |
|
| 61 |
rec_box.change(lambda: [None] * 2, outputs=[audio_file_box, yt_url_box])
|
| 62 |
audio_file_box.change(lambda: [None] * 2, outputs=[rec_box, yt_url_box])
|
|
@@ -68,7 +98,7 @@ with gr.Blocks() as demo:
|
|
| 68 |
# gr.Markdown('### Trim audio sample (sec)')
|
| 69 |
with gr.Row():
|
| 70 |
start_time_box = gr.Number(label='Start', value=0)
|
| 71 |
-
end_time_box = gr.Number(label='End', value=
|
| 72 |
model_box = gr.Dropdown(label='Model weight', choices=available_models, value=available_models[0])
|
| 73 |
|
| 74 |
submit_btn = gr.Button('Submit')
|
|
@@ -76,7 +106,7 @@ with gr.Blocks() as demo:
|
|
| 76 |
with gr.Column():
|
| 77 |
with gr.Accordion('Hint', open=False):
|
| 78 |
gr.Markdown('You can find more settings under the **Fine-grained settings**')
|
| 79 |
-
gr.Markdown('-
|
| 80 |
gr.Markdown('- Low audio quality? Try to switch to a proper model weight')
|
| 81 |
outputs=gr.Audio(label='Output')
|
| 82 |
|
|
|
|
| 2 |
from tsmnet import Stretcher
|
| 3 |
import gradio as gr
|
| 4 |
from gradio import processing_utils
|
| 5 |
+
# import torch
|
| 6 |
import torchaudio
|
| 7 |
+
import yt_dlp
|
| 8 |
|
| 9 |
model_root = './weights'
|
| 10 |
+
yt_dl_dir = 'yt-audio'
|
| 11 |
available_models = ['speech', 'pop-music', 'classical-music']
|
| 12 |
working_sr = 22050
|
| 13 |
|
|
|
|
| 17 |
for weight in available_models
|
| 18 |
}
|
| 19 |
|
| 20 |
+
def download_yt_audio(url):
|
| 21 |
+
# purge outdated audio files (older than 1 days)
|
| 22 |
+
os.system(f'find {yt_dl_dir} -audio -mtime +1 -delete')
|
| 23 |
+
|
| 24 |
+
ydl_opts = {
|
| 25 |
+
'format': 'm4a/bestaudio/best',
|
| 26 |
+
'postprocessors': [{ # Extract audio using ffmpeg
|
| 27 |
+
'key': 'FFmpegExtractAudio',
|
| 28 |
+
'preferredcodec': 'wav',
|
| 29 |
+
}],
|
| 30 |
+
'outtmpl': f"{yt_dl_dir}/%(id)s.%(ext)s"
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 34 |
+
try:
|
| 35 |
+
ydl.cache.remove()
|
| 36 |
+
meta = ydl.extract_info(url, download=False)
|
| 37 |
+
audio_file = os.path.join(yt_dl_dir, meta['id'] + '.wav')
|
| 38 |
+
if not os.path.isfile(audio_file):
|
| 39 |
+
ydl.download(url)
|
| 40 |
+
|
| 41 |
+
except yt_dlp.DownloadError as error:
|
| 42 |
+
raise gr.Error(f'Failed to download from YouTube: {error}')
|
| 43 |
+
|
| 44 |
+
return audio_file
|
| 45 |
+
|
| 46 |
+
|
| 47 |
def prepare_audio_file(rec, audio_file, yt_url):
|
| 48 |
if rec is not None:
|
| 49 |
return rec
|
| 50 |
if audio_file is not None:
|
| 51 |
return audio_file
|
| 52 |
if yt_url != '':
|
| 53 |
+
return download_yt_audio(yt_url)
|
| 54 |
else:
|
| 55 |
raise gr.Error('No audio found!')
|
| 56 |
|
|
|
|
| 74 |
# @@@@@@@ Start of the program @@@@@@@@
|
| 75 |
|
| 76 |
models = prepare_models()
|
| 77 |
+
os.makedirs(yt_dl_dir, exist_ok=True)
|
| 78 |
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
gr.Markdown('# TSM-Net')
|
|
|
|
| 86 |
with gr.Tab('From file'):
|
| 87 |
audio_file_box = gr.Audio(label='Audio sample', type='filepath')
|
| 88 |
with gr.Tab('From YouTube'):
|
| 89 |
+
yt_url_box = gr.Textbox(label='YouTube URL', placeholder='https://youtu.be/q6EoRBvdVPQ')
|
| 90 |
|
| 91 |
rec_box.change(lambda: [None] * 2, outputs=[audio_file_box, yt_url_box])
|
| 92 |
audio_file_box.change(lambda: [None] * 2, outputs=[rec_box, yt_url_box])
|
|
|
|
| 98 |
# gr.Markdown('### Trim audio sample (sec)')
|
| 99 |
with gr.Row():
|
| 100 |
start_time_box = gr.Number(label='Start', value=0)
|
| 101 |
+
end_time_box = gr.Number(label='End', value=60)
|
| 102 |
model_box = gr.Dropdown(label='Model weight', choices=available_models, value=available_models[0])
|
| 103 |
|
| 104 |
submit_btn = gr.Button('Submit')
|
|
|
|
| 106 |
with gr.Column():
|
| 107 |
with gr.Accordion('Hint', open=False):
|
| 108 |
gr.Markdown('You can find more settings under the **Fine-grained settings**')
|
| 109 |
+
gr.Markdown('- Waiting too long? Try to adjust the start/end timestamp')
|
| 110 |
gr.Markdown('- Low audio quality? Try to switch to a proper model weight')
|
| 111 |
outputs=gr.Audio(label='Output')
|
| 112 |
|