Spaces:
Runtime error
Runtime error
Create utils.py
Browse files- src/utils.py +63 -0
src/utils.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import yt_dlp
|
| 3 |
+
from pydub import AudioSegment
|
| 4 |
+
import uuid
|
| 5 |
+
from speechbrain.pretrained.interfaces import foreign_class
|
| 6 |
+
|
| 7 |
+
accent_map = {
|
| 8 |
+
'england': 'British',
|
| 9 |
+
'us': 'American',
|
| 10 |
+
'australia': 'Australian',
|
| 11 |
+
'canada': 'Canadian',
|
| 12 |
+
'indian': 'Indian',
|
| 13 |
+
'ireland': 'Irish',
|
| 14 |
+
'scotland': 'Scottish',
|
| 15 |
+
'wales': 'Welsh',
|
| 16 |
+
'african': 'African',
|
| 17 |
+
'bermuda': 'Bermudian',
|
| 18 |
+
'hong_kong': 'Hong Kong',
|
| 19 |
+
'malaysia': 'Malaysian',
|
| 20 |
+
'new_zealand': 'New Zealander',
|
| 21 |
+
'philippines': 'Filipino',
|
| 22 |
+
'singapore': 'Singaporean',
|
| 23 |
+
'south_atlantic': 'South Atlantic'
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
def download_video_audio( url ):
|
| 27 |
+
output_dir = "/tmp"
|
| 28 |
+
|
| 29 |
+
os.makedirs( output_dir, exist_ok=True )
|
| 30 |
+
|
| 31 |
+
unique_id = str( uuid.uuid4() )
|
| 32 |
+
mp4_path = os.path.join( output_dir, f"{unique_id}.mp4" )
|
| 33 |
+
wav_path = os.path.join( output_dir, f"{unique_id}.wav" )
|
| 34 |
+
|
| 35 |
+
ydl_opts = {
|
| 36 |
+
'format' : 'bestaudio/best',
|
| 37 |
+
'outtmpl' : mp4_path,
|
| 38 |
+
'quiet' : True,
|
| 39 |
+
'noplaylist': True,
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
with yt_dlp.YoutubeDL( ydl_opts ) as ydl:
|
| 43 |
+
ydl.download( [ url ] )
|
| 44 |
+
|
| 45 |
+
audio = AudioSegment.from_file( mp4_path )
|
| 46 |
+
audio.export( wav_path, format = "wav" )
|
| 47 |
+
|
| 48 |
+
os.remove( mp4_path )
|
| 49 |
+
|
| 50 |
+
return wav_path
|
| 51 |
+
|
| 52 |
+
def classify_accent( audio_path ):
|
| 53 |
+
classifier = foreign_class(
|
| 54 |
+
source="Jzuluaga/accent-id-commonaccent_xlsr-en-english",
|
| 55 |
+
pymodule_file="custom_interface.py",
|
| 56 |
+
classname="CustomEncoderWav2vec2Classifier"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
out_prob, score, index, text_lab = classifier.classify_file( audio_path )
|
| 60 |
+
accent = text_lab[ 0 ].lower()
|
| 61 |
+
polished_accent = accent_map.get( accent, accent.capitalize() )
|
| 62 |
+
|
| 63 |
+
return polished_accent, score.item() * 100
|