import gradio as gr import re import unidecode import yt_dlp import os # Function to clean the string for file naming def cleanString(string): clean_string = unidecode.unidecode(string) clean_string = re.sub(r'[^\w\s]', '', clean_string) clean_string = clean_string.replace(" ", "_") return clean_string.lower() # Function to download audio from YouTube def download_audio(url): path_to_folder_audio_mp3 = "./" ydl_opts = { 'format': 'bestaudio/best', 'outtmpl': f'{path_to_folder_audio_mp3}%(title)s.%(ext)s', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192', }], 'noplaylist': True, # Avoid downloading playlists } with yt_dlp.YoutubeDL(ydl_opts) as ydl: info_dict = ydl.extract_info(url, download=True) video_title = info_dict['title'] # Clean the title for the filename new_local_link = cleanString(video_title) + ".mp3" original_file_path = os.path.join(path_to_folder_audio_mp3, f"{video_title}.mp3") # Rename the audio file if it exists if os.path.exists(original_file_path): os.rename(original_file_path, os.path.join(path_to_folder_audio_mp3, new_local_link)) # Get the final file path file_path = os.path.join(path_to_folder_audio_mp3, new_local_link) return file_path, file_path # Gradio interface with gr.Blocks() as demo: gr.Markdown("