File size: 1,702 Bytes
9e030a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import yt_dlp
from pydub import AudioSegment
import uuid
from speechbrain.pretrained.interfaces import foreign_class

accent_map = {
    'england': 'British',
    'us': 'American',
    'australia': 'Australian',
    'canada': 'Canadian',
    'indian': 'Indian',
    'ireland': 'Irish',
    'scotland': 'Scottish',
    'wales': 'Welsh',
    'african': 'African',
    'bermuda': 'Bermudian',
    'hong_kong': 'Hong Kong',
    'malaysia': 'Malaysian',
    'new_zealand': 'New Zealander',
    'philippines': 'Filipino',
    'singapore': 'Singaporean',
    'south_atlantic': 'South Atlantic'
}

def download_video_audio( url ):
    output_dir = "/tmp"

    os.makedirs( output_dir, exist_ok=True )

    unique_id = str( uuid.uuid4() )
    mp4_path = os.path.join( output_dir, f"{unique_id}.mp4" )
    wav_path = os.path.join( output_dir, f"{unique_id}.wav" )

    ydl_opts = {
        'format'    : 'bestaudio/best',
        'outtmpl'   : mp4_path,
        'quiet'     : True,
        'noplaylist': True,
    }

    with yt_dlp.YoutubeDL( ydl_opts ) as ydl:
        ydl.download( [ url ] )

    audio = AudioSegment.from_file( mp4_path )
    audio.export( wav_path, format = "wav" )

    os.remove( mp4_path )

    return wav_path

def classify_accent( audio_path ):
    classifier = foreign_class(
        source="Jzuluaga/accent-id-commonaccent_xlsr-en-english",
        pymodule_file="custom_interface.py",
        classname="CustomEncoderWav2vec2Classifier"
    )

    out_prob, score, index, text_lab = classifier.classify_file( audio_path )
    accent = text_lab[ 0 ].lower()
    polished_accent = accent_map.get( accent, accent.capitalize() )

    return polished_accent, score.item() * 100