File size: 2,565 Bytes
9c06d0f
 
 
 
1d687f3
 
 
 
 
 
9c06d0f
1d687f3
 
9c06d0f
1d687f3
 
 
 
9c06d0f
1d687f3
 
 
 
 
 
 
 
 
 
9c06d0f
1d687f3
9c06d0f
 
 
 
1d687f3
 
 
 
 
 
 
 
 
 
 
 
 
9c06d0f
1d687f3
 
 
9c06d0f
 
 
 
1d687f3
 
 
9c06d0f
1d687f3
9c06d0f
1d687f3
 
 
9c06d0f
1d687f3
9c06d0f
1d687f3
 
 
 
9c06d0f
 
 
 
1d687f3
9c06d0f
1d687f3
 
9c06d0f
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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()