Spaces:
Running
Running
Delete gradio_helper.py
Browse files- gradio_helper.py +0 -80
gradio_helper.py
DELETED
|
@@ -1,80 +0,0 @@
|
|
| 1 |
-
# @title GradIO helper
|
| 2 |
-
import os
|
| 3 |
-
import subprocess
|
| 4 |
-
import glob
|
| 5 |
-
from typing import Tuple, Dict, Literal
|
| 6 |
-
from ctypes import ArgumentError
|
| 7 |
-
# from google.colab import output
|
| 8 |
-
|
| 9 |
-
from model_helper import *
|
| 10 |
-
from html_helper import *
|
| 11 |
-
|
| 12 |
-
from pytube import YouTube
|
| 13 |
-
import gradio as gr
|
| 14 |
-
import torchaudio
|
| 15 |
-
|
| 16 |
-
def prepare_media(source_path_or_url: os.PathLike,
|
| 17 |
-
source_type: Literal['audio_filepath', 'youtube_url'],
|
| 18 |
-
delete_video: bool = True) -> Dict:
|
| 19 |
-
"""prepare media from source path or youtube, and return audio info"""
|
| 20 |
-
# Get audio_file
|
| 21 |
-
if source_type == 'audio_filepath':
|
| 22 |
-
audio_file = source_path_or_url
|
| 23 |
-
elif source_type == 'youtube_url':
|
| 24 |
-
# Download from youtube
|
| 25 |
-
try:
|
| 26 |
-
# Try PyTube first
|
| 27 |
-
yt = YouTube(source_path_or_url)
|
| 28 |
-
audio_stream = min(yt.streams.filter(only_audio=True), key=lambda s: s.bitrate)
|
| 29 |
-
mp4_file = audio_stream.download(output_path='downloaded') # ./downloaded
|
| 30 |
-
audio_file = mp4_file[:-3] + 'mp3'
|
| 31 |
-
subprocess.run(['ffmpeg', '-i', mp4_file, '-ac', '1', audio_file])
|
| 32 |
-
os.remove(mp4_file)
|
| 33 |
-
except Exception as e:
|
| 34 |
-
try:
|
| 35 |
-
# Try alternative
|
| 36 |
-
print(f"Failed with PyTube, error: {e}. Trying yt-dlp...")
|
| 37 |
-
audio_file = './downloaded/yt_audio'
|
| 38 |
-
subprocess.run(['yt-dlp', '-x', source_path_or_url, '-f', 'bestaudio',
|
| 39 |
-
'-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
|
| 40 |
-
'--force-overwrites'])
|
| 41 |
-
audio_file += '.mp3'
|
| 42 |
-
except Exception as e:
|
| 43 |
-
print(f"Alternative downloader failed, error: {e}. Please try again later!")
|
| 44 |
-
return None
|
| 45 |
-
else:
|
| 46 |
-
raise ValueError(source_type)
|
| 47 |
-
|
| 48 |
-
# Create info
|
| 49 |
-
info = torchaudio.info(audio_file)
|
| 50 |
-
return {
|
| 51 |
-
"filepath": audio_file,
|
| 52 |
-
"track_name": os.path.basename(audio_file).split('.')[0],
|
| 53 |
-
"sample_rate": int(info.sample_rate),
|
| 54 |
-
"bits_per_sample": int(info.bits_per_sample),
|
| 55 |
-
"num_channels": int(info.num_channels),
|
| 56 |
-
"num_frames": int(info.num_frames),
|
| 57 |
-
"duration": int(info.num_frames / info.sample_rate),
|
| 58 |
-
"encoding": str.lower(info.encoding),
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
def process_audio(audio_filepath):
|
| 62 |
-
if audio_filepath is None:
|
| 63 |
-
return None
|
| 64 |
-
audio_info = prepare_media(audio_filepath, source_type='audio_filepath')
|
| 65 |
-
midifile = transcribe(model, audio_info)
|
| 66 |
-
midifile = to_data_url(midifile)
|
| 67 |
-
return create_html_from_midi(midifile) # html midiplayer
|
| 68 |
-
|
| 69 |
-
def process_video(youtube_url):
|
| 70 |
-
if 'youtu' not in youtube_url:
|
| 71 |
-
return None
|
| 72 |
-
audio_info = prepare_media(youtube_url, source_type='youtube_url')
|
| 73 |
-
midifile = transcribe(model, audio_info)
|
| 74 |
-
midifile = to_data_url(midifile)
|
| 75 |
-
return create_html_from_midi(midifile) # html midiplayer
|
| 76 |
-
|
| 77 |
-
def play_video(youtube_url):
|
| 78 |
-
if 'youtu' not in youtube_url:
|
| 79 |
-
return None
|
| 80 |
-
return create_html_youtube_player(youtube_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|