Spaces:
Sleeping
Sleeping
update handler
Browse files- app.py +23 -7
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import os
|
|
| 8 |
from moviepy.editor import VideoFileClip
|
| 9 |
from pydub import AudioSegment
|
| 10 |
import uuid
|
|
|
|
| 11 |
|
| 12 |
MODEL_ID = "dima806/english_accents_classification"
|
| 13 |
feature_extractor = AutoFeatureExtractor.from_pretrained(MODEL_ID)
|
|
@@ -31,12 +32,18 @@ def download_video(url):
|
|
| 31 |
'outtmpl': video_path,
|
| 32 |
'quiet': True,
|
| 33 |
'no_warnings': True,
|
| 34 |
-
'
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 37 |
ydl.download([url])
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def extract_audio(video_file):
|
| 42 |
audio_path = video_file.replace(".mp4", ".wav")
|
|
@@ -62,21 +69,26 @@ def classify_accent(audio_path):
|
|
| 62 |
return PRETTY.get(label, label), round(confidence * 100, 2)
|
| 63 |
|
| 64 |
def analyze_accent(url, file):
|
|
|
|
| 65 |
try:
|
| 66 |
if file is not None:
|
| 67 |
file_path = file.name
|
| 68 |
-
audio_path = file_path if file_path.endswith(".wav") else extract_audio(file_path)
|
| 69 |
-
elif url:
|
| 70 |
-
video_path = download_video(url)
|
| 71 |
audio_path = extract_audio(video_path)
|
| 72 |
os.remove(video_path)
|
| 73 |
else:
|
| 74 |
-
return "Error", 0.0
|
| 75 |
|
| 76 |
accent, confidence = classify_accent(audio_path)
|
| 77 |
os.remove(audio_path)
|
|
|
|
|
|
|
| 78 |
return accent, confidence
|
| 79 |
except Exception as e:
|
|
|
|
|
|
|
| 80 |
return f"Error: {str(e)}", 0.0
|
| 81 |
|
| 82 |
iface = gr.Interface(
|
|
@@ -89,7 +101,11 @@ iface = gr.Interface(
|
|
| 89 |
gr.Textbox(label="Predicted Accent"),
|
| 90 |
gr.Number(label="Confidence (%)")
|
| 91 |
],
|
| 92 |
-
title="English Accent Analyzer"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
)
|
| 94 |
|
| 95 |
iface.launch()
|
|
|
|
| 8 |
from moviepy.editor import VideoFileClip
|
| 9 |
from pydub import AudioSegment
|
| 10 |
import uuid
|
| 11 |
+
import shutil
|
| 12 |
|
| 13 |
MODEL_ID = "dima806/english_accents_classification"
|
| 14 |
feature_extractor = AutoFeatureExtractor.from_pretrained(MODEL_ID)
|
|
|
|
| 32 |
'outtmpl': video_path,
|
| 33 |
'quiet': True,
|
| 34 |
'no_warnings': True,
|
| 35 |
+
'http_headers': {
|
| 36 |
+
'User-Agent': 'Mozilla/5.0',
|
| 37 |
+
}
|
| 38 |
}
|
| 39 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 40 |
ydl.download([url])
|
| 41 |
|
| 42 |
+
if not os.path.exists(video_path):
|
| 43 |
+
shutil.rmtree(temp_dir)
|
| 44 |
+
raise Exception("Failed to download video")
|
| 45 |
+
|
| 46 |
+
return video_path, temp_dir
|
| 47 |
|
| 48 |
def extract_audio(video_file):
|
| 49 |
audio_path = video_file.replace(".mp4", ".wav")
|
|
|
|
| 69 |
return PRETTY.get(label, label), round(confidence * 100, 2)
|
| 70 |
|
| 71 |
def analyze_accent(url, file):
|
| 72 |
+
temp_dir = None
|
| 73 |
try:
|
| 74 |
if file is not None:
|
| 75 |
file_path = file.name
|
| 76 |
+
audio_path = file_path if file_path.lower().endswith(".wav") else extract_audio(file_path)
|
| 77 |
+
elif url and url.strip():
|
| 78 |
+
video_path, temp_dir = download_video(url)
|
| 79 |
audio_path = extract_audio(video_path)
|
| 80 |
os.remove(video_path)
|
| 81 |
else:
|
| 82 |
+
return "Error: Please provide a URL or upload a file.", 0.0
|
| 83 |
|
| 84 |
accent, confidence = classify_accent(audio_path)
|
| 85 |
os.remove(audio_path)
|
| 86 |
+
if temp_dir:
|
| 87 |
+
shutil.rmtree(temp_dir)
|
| 88 |
return accent, confidence
|
| 89 |
except Exception as e:
|
| 90 |
+
if temp_dir:
|
| 91 |
+
shutil.rmtree(temp_dir)
|
| 92 |
return f"Error: {str(e)}", 0.0
|
| 93 |
|
| 94 |
iface = gr.Interface(
|
|
|
|
| 101 |
gr.Textbox(label="Predicted Accent"),
|
| 102 |
gr.Number(label="Confidence (%)")
|
| 103 |
],
|
| 104 |
+
title="English Accent Analyzer",
|
| 105 |
+
description=(
|
| 106 |
+
"Enter a YouTube URL or upload a video/audio file to analyze the English accent.\n\n"
|
| 107 |
+
"💡 If your video URL doesn't work, try uploading a video file instead."
|
| 108 |
+
)
|
| 109 |
)
|
| 110 |
|
| 111 |
iface.launch()
|
requirements.txt
CHANGED
|
@@ -89,3 +89,5 @@ urllib3==2.5.0
|
|
| 89 |
Werkzeug==3.1.3
|
| 90 |
youtube-dl==2021.12.17
|
| 91 |
yt-dlp==2025.6.9
|
|
|
|
|
|
|
|
|
| 89 |
Werkzeug==3.1.3
|
| 90 |
youtube-dl==2021.12.17
|
| 91 |
yt-dlp==2025.6.9
|
| 92 |
+
pytube @ git+https://github.com/pytube/pytube.git@a32fff39058a6f7e5e59ecd06a7467b71197ce35
|
| 93 |
+
|