import os import subprocess import sys # Ensure required libraries are installed try: import yt_dlp as youtube_dl except ImportError: subprocess.check_call([sys.executable, "-m", "pip", "install", "yt-dlp"]) import yt_dlp as youtube_dl import gradio as gr from transformers import pipeline def download_video(video_url, filename="downloaded_video.mp4"): """ Download the video file using yt_dlp with cookies.txt for authentication. """ ydl_opts = { 'format': 'best', 'outtmpl': filename, 'noplaylist': True, 'quiet': True, 'cookiefile': 'cookies.txt', 'user_agent': ( 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/115.0.0.0 Safari/537.36' ), } with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([video_url]) return filename def extract_audio(video_filename, audio_filename="extracted_audio.wav"): command = [ 'ffmpeg', '-y', '-i', video_filename, '-vn', '-acodec', 'pcm_s16le', '-ar', '44100', '-ac', '2', audio_filename ] subprocess.run(command, check=True) return audio_filename def classify_accent(audio_file, model_name="your-hf-accent-model"): if model_name == "your-hf-accent-model": return "Accent: Demo Accent\nConfidence: 100.00%" classifier = pipeline("audio-classification", model=model_name) results = classifier(audio_file) if results: top = results[0] return f"Accent: {top['label']}\nConfidence: {top['score'] * 100:.2f}%" else: return "No classification result." def accent_classifier(video_url): try: video_filename = download_video(video_url) audio_filename = extract_audio(video_filename) result = classify_accent(audio_filename) except Exception as e: result = f"Error occurred: {e}" finally: if os.path.exists("downloaded_video.mp4"): os.remove("downloaded_video.mp4") if os.path.exists("extracted_audio.wav"): os.remove("extracted_audio.wav") return result iface = gr.Interface( fn=accent_classifier, inputs=gr.Textbox(label="Video URL", placeholder="Enter a public YouTube, Vimeo, or Dailymotion link"), outputs="text", title="Accent Classifier", description="Downloads a video, extracts audio, and classifies the accent using a Hugging Face model." ) if __name__ == "__main__": iface.launch()