Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,6 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 'amt/src
|
|
6 |
import glob
|
7 |
import gradio as gr
|
8 |
|
9 |
-
from gradio_helper import *
|
10 |
from model_helper import *
|
11 |
|
12 |
# @title Load Checkpoint
|
@@ -43,9 +42,86 @@ else:
|
|
43 |
|
44 |
model = load_model_checkpoint(args=args)
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
|
48 |
-
AUDIO_EXAMPLES = glob.glob('
|
49 |
YOUTUBE_EXAMPLES = ["https://www.youtube.com/watch?v=vMboypSkj3c"]
|
50 |
|
51 |
theme = 'gradio/dracula_revamped' #'Insuz/Mocha' #gr.themes.Soft()
|
|
|
6 |
import glob
|
7 |
import gradio as gr
|
8 |
|
|
|
9 |
from model_helper import *
|
10 |
|
11 |
# @title Load Checkpoint
|
|
|
42 |
|
43 |
model = load_model_checkpoint(args=args)
|
44 |
|
45 |
+
# @title GradIO helper
|
46 |
+
import os
|
47 |
+
import subprocess
|
48 |
+
from typing import Tuple, Dict, Literal
|
49 |
+
from ctypes import ArgumentError
|
50 |
+
|
51 |
+
from html_helper import *
|
52 |
+
|
53 |
+
from pytube import YouTube
|
54 |
+
import torchaudio
|
55 |
+
|
56 |
+
def prepare_media(source_path_or_url: os.PathLike,
|
57 |
+
source_type: Literal['audio_filepath', 'youtube_url'],
|
58 |
+
delete_video: bool = True) -> Dict:
|
59 |
+
"""prepare media from source path or youtube, and return audio info"""
|
60 |
+
# Get audio_file
|
61 |
+
if source_type == 'audio_filepath':
|
62 |
+
audio_file = source_path_or_url
|
63 |
+
elif source_type == 'youtube_url':
|
64 |
+
# Download from youtube
|
65 |
+
try:
|
66 |
+
# Try PyTube first
|
67 |
+
yt = YouTube(source_path_or_url)
|
68 |
+
audio_stream = min(yt.streams.filter(only_audio=True), key=lambda s: s.bitrate)
|
69 |
+
mp4_file = audio_stream.download(output_path='downloaded') # ./downloaded
|
70 |
+
audio_file = mp4_file[:-3] + 'mp3'
|
71 |
+
subprocess.run(['ffmpeg', '-i', mp4_file, '-ac', '1', audio_file])
|
72 |
+
os.remove(mp4_file)
|
73 |
+
except Exception as e:
|
74 |
+
try:
|
75 |
+
# Try alternative
|
76 |
+
print(f"Failed with PyTube, error: {e}. Trying yt-dlp...")
|
77 |
+
audio_file = './downloaded/yt_audio'
|
78 |
+
subprocess.run(['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
|
79 |
+
'-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
|
80 |
+
'--force-overwrites'])
|
81 |
+
audio_file += '.mp3'
|
82 |
+
except Exception as e:
|
83 |
+
print(f"Alternative downloader failed, error: {e}. Please try again later!")
|
84 |
+
return None
|
85 |
+
else:
|
86 |
+
raise ValueError(source_type)
|
87 |
+
|
88 |
+
# Create info
|
89 |
+
info = torchaudio.info(audio_file)
|
90 |
+
return {
|
91 |
+
"filepath": audio_file,
|
92 |
+
"track_name": os.path.basename(audio_file).split('.')[0],
|
93 |
+
"sample_rate": int(info.sample_rate),
|
94 |
+
"bits_per_sample": int(info.bits_per_sample),
|
95 |
+
"num_channels": int(info.num_channels),
|
96 |
+
"num_frames": int(info.num_frames),
|
97 |
+
"duration": int(info.num_frames / info.sample_rate),
|
98 |
+
"encoding": str.lower(info.encoding),
|
99 |
+
}
|
100 |
+
|
101 |
+
def process_audio(audio_filepath):
|
102 |
+
if audio_filepath is None:
|
103 |
+
return None
|
104 |
+
audio_info = prepare_media(audio_filepath, source_type='audio_filepath')
|
105 |
+
midifile = transcribe(model, audio_info)
|
106 |
+
midifile = to_data_url(midifile)
|
107 |
+
return create_html_from_midi(midifile) # html midiplayer
|
108 |
+
|
109 |
+
def process_video(youtube_url):
|
110 |
+
if 'youtu' not in youtube_url:
|
111 |
+
return None
|
112 |
+
audio_info = prepare_media(youtube_url, source_type='youtube_url')
|
113 |
+
midifile = transcribe(model, audio_info)
|
114 |
+
midifile = to_data_url(midifile)
|
115 |
+
return create_html_from_midi(midifile) # html midiplayer
|
116 |
+
|
117 |
+
def play_video(youtube_url):
|
118 |
+
if 'youtu' not in youtube_url:
|
119 |
+
return None
|
120 |
+
return create_html_youtube_player(youtube_url)
|
121 |
+
|
122 |
|
123 |
|
124 |
+
AUDIO_EXAMPLES = glob.glob('examples/*.*', recursive=True)
|
125 |
YOUTUBE_EXAMPLES = ["https://www.youtube.com/watch?v=vMboypSkj3c"]
|
126 |
|
127 |
theme = 'gradio/dracula_revamped' #'Insuz/Mocha' #gr.themes.Soft()
|