Spaces:
Running
on
Zero
Running
on
Zero
feat: add youtbue url download
Browse files- __pycache__/inference.cpython-310.pyc +0 -0
- __pycache__/utils.cpython-310.pyc +0 -0
- app.py +60 -2
__pycache__/inference.cpython-310.pyc
ADDED
Binary file (4.35 kB). View file
|
|
__pycache__/utils.cpython-310.pyc
ADDED
Binary file (5.11 kB). View file
|
|
app.py
CHANGED
@@ -12,6 +12,8 @@ from pathlib import Path
|
|
12 |
import spaces
|
13 |
from pydub.exceptions import CouldntEncodeError
|
14 |
from transformers import pipeline
|
|
|
|
|
15 |
|
16 |
# Initialize text generation model
|
17 |
model = pipeline('text-generation', model='EleutherAI/gpt-neo-125M')
|
@@ -90,6 +92,58 @@ def standardize_title(input_title):
|
|
90 |
|
91 |
return standardized_output.strip()
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
def handle_file_upload(file):
|
94 |
"""
|
95 |
Handle file upload, standardize the filename, change extension to .wav, and copy it to the input folder.
|
@@ -302,7 +356,7 @@ def delete_folders_and_files(input_dir):
|
|
302 |
print("Cleanup completed.")
|
303 |
|
304 |
@spaces.GPU(duration=160)
|
305 |
-
def process_audio(uploaded_file):
|
306 |
"""
|
307 |
Main function to process the uploaded audio file.
|
308 |
|
@@ -319,6 +373,9 @@ def process_audio(uploaded_file):
|
|
319 |
input_path, formatted_title = handle_file_upload(uploaded_file)
|
320 |
if input_path is None:
|
321 |
raise ValueError("File upload failed.")
|
|
|
|
|
|
|
322 |
else:
|
323 |
raise ValueError("Please upload a WAV file.")
|
324 |
|
@@ -363,6 +420,7 @@ def process_audio(uploaded_file):
|
|
363 |
with gr.Blocks() as demo:
|
364 |
gr.Markdown("# Music Player and Processor")
|
365 |
|
|
|
366 |
file_upload = gr.File(label="Upload MP3 file", file_types=[".mp3"])
|
367 |
process_button = gr.Button("Process Audio")
|
368 |
log_output = gr.Textbox(label="Processing Log", interactive=False)
|
@@ -370,7 +428,7 @@ with gr.Blocks() as demo:
|
|
370 |
|
371 |
process_button.click(
|
372 |
fn=process_audio,
|
373 |
-
inputs=file_upload,
|
374 |
outputs=[log_output, processed_audio_output],
|
375 |
show_progress=True
|
376 |
)
|
|
|
12 |
import spaces
|
13 |
from pydub.exceptions import CouldntEncodeError
|
14 |
from transformers import pipeline
|
15 |
+
import requests
|
16 |
+
from bs4 import BeautifulSoup
|
17 |
|
18 |
# Initialize text generation model
|
19 |
model = pipeline('text-generation', model='EleutherAI/gpt-neo-125M')
|
|
|
92 |
|
93 |
return standardized_output.strip()
|
94 |
|
95 |
+
def download_youtube_audio(youtube_url: str, output_dir: str = './download', delete_existing: bool = True, simulate: bool = False) -> str:
|
96 |
+
"""
|
97 |
+
Downloads audio from a YouTube URL and saves it as an MP3 file with specified yt-dlp options.
|
98 |
+
|
99 |
+
Args:
|
100 |
+
youtube_url (str): URL of the YouTube video.
|
101 |
+
output_dir (str): Directory to save the downloaded audio file.
|
102 |
+
delete_existing (bool): If True, deletes any existing file with the same name.
|
103 |
+
simulate (bool): If True, simulates the download without actually downloading.
|
104 |
+
|
105 |
+
Returns:
|
106 |
+
str: Path to the downloaded audio file.
|
107 |
+
"""
|
108 |
+
if not os.path.exists(output_dir):
|
109 |
+
os.makedirs(output_dir)
|
110 |
+
|
111 |
+
r = requests.get(youtube_url)
|
112 |
+
soup = BeautifulSoup(r.text)
|
113 |
+
|
114 |
+
link = soup.find_all(name="title")[0]
|
115 |
+
title = str(link)
|
116 |
+
title = title.replace("<title>","")
|
117 |
+
title = title.replace("</title>","")
|
118 |
+
|
119 |
+
audio_file = os.path.join(output_dir, title)
|
120 |
+
|
121 |
+
# Remove existing file if requested
|
122 |
+
if delete_existing and os.path.exists(audio_file + '.mp3'):
|
123 |
+
os.remove(audio_file + '.mp3')
|
124 |
+
|
125 |
+
# Prepare yt-dlp command
|
126 |
+
command = [
|
127 |
+
'yt-dlp', '-x', youtube_url, '-f', 'bestaudio',
|
128 |
+
'-o', audio_file, '--audio-format', 'mp3', '--restrict-filenames',
|
129 |
+
'--extractor-retries', '10', '--force-overwrites', '--username', 'oauth2', '--password', '', '-v'
|
130 |
+
]
|
131 |
+
|
132 |
+
if simulate:
|
133 |
+
command.append('-s')
|
134 |
+
|
135 |
+
# Run the download command
|
136 |
+
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
137 |
+
|
138 |
+
for line in iter(process.stdout.readline, ''):
|
139 |
+
print(line, end='') # Print download progress
|
140 |
+
|
141 |
+
process.stdout.close()
|
142 |
+
process.wait()
|
143 |
+
|
144 |
+
return audio_file + '.mp3'
|
145 |
+
|
146 |
+
|
147 |
def handle_file_upload(file):
|
148 |
"""
|
149 |
Handle file upload, standardize the filename, change extension to .wav, and copy it to the input folder.
|
|
|
356 |
print("Cleanup completed.")
|
357 |
|
358 |
@spaces.GPU(duration=160)
|
359 |
+
def process_audio(uploaded_file, link):
|
360 |
"""
|
361 |
Main function to process the uploaded audio file.
|
362 |
|
|
|
373 |
input_path, formatted_title = handle_file_upload(uploaded_file)
|
374 |
if input_path is None:
|
375 |
raise ValueError("File upload failed.")
|
376 |
+
elif link:
|
377 |
+
new_file = download_youtube_audio(link)
|
378 |
+
input_path, formatted_title = handle_file_upload(new_file)
|
379 |
else:
|
380 |
raise ValueError("Please upload a WAV file.")
|
381 |
|
|
|
420 |
with gr.Blocks() as demo:
|
421 |
gr.Markdown("# Music Player and Processor")
|
422 |
|
423 |
+
youtube_url = gr.Textbox(label="URL Youtube Song")
|
424 |
file_upload = gr.File(label="Upload MP3 file", file_types=[".mp3"])
|
425 |
process_button = gr.Button("Process Audio")
|
426 |
log_output = gr.Textbox(label="Processing Log", interactive=False)
|
|
|
428 |
|
429 |
process_button.click(
|
430 |
fn=process_audio,
|
431 |
+
inputs=[file_upload, youtube_url],
|
432 |
outputs=[log_output, processed_audio_output],
|
433 |
show_progress=True
|
434 |
)
|