Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +255 -273
src/streamlit_app.py
CHANGED
|
@@ -1,311 +1,293 @@
|
|
| 1 |
import os
|
| 2 |
-
import io
|
| 3 |
-
import csv
|
| 4 |
-
import subprocess
|
| 5 |
import streamlit as st
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
import
|
| 11 |
-
from tensorflow import keras
|
| 12 |
-
from huggingface_hub import from_pretrained_keras
|
| 13 |
-
from audio_recorder_streamlit import audio_recorder
|
| 14 |
-
import yt_dlp
|
| 15 |
import torch
|
| 16 |
import torchaudio
|
|
|
|
|
|
|
| 17 |
torchaudio.set_audio_backend("soundfile")
|
| 18 |
-
import speechbrain
|
| 19 |
|
| 20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
-
from speechbrain.pretrained import EncoderClassifier
|
| 23 |
from speechbrain.pretrained.interfaces import foreign_class
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
# Configuration
|
| 35 |
-
xlsr_accent_classes = [
|
| 36 |
-
"US",
|
| 37 |
-
"England",
|
| 38 |
-
"Australia",
|
| 39 |
-
"Indian",
|
| 40 |
-
"Canada",
|
| 41 |
-
"Bermuda",
|
| 42 |
-
"Scotland",
|
| 43 |
-
"African",
|
| 44 |
-
"Ireland",
|
| 45 |
-
"NewZealand",
|
| 46 |
-
"Wales",
|
| 47 |
-
"Malaysia",
|
| 48 |
-
"Philippines",
|
| 49 |
-
"Singapore",
|
| 50 |
-
"HongKong",
|
| 51 |
-
"SouthAtlantic"
|
| 52 |
-
]
|
| 53 |
-
|
| 54 |
-
@st.cache_resource
|
| 55 |
-
def load_models():
|
| 56 |
-
xlsr_model = None
|
| 57 |
-
|
| 58 |
-
try:
|
| 59 |
-
# Show loading message for XLSR
|
| 60 |
-
with st.spinner("Loading XLSR-based accent classifier..."):
|
| 61 |
-
xlsr_model = foreign_class(
|
| 62 |
source="Jzuluaga/accent-id-commonaccent_xlsr-en-english",
|
| 63 |
pymodule_file="custom_interface.py",
|
| 64 |
-
classname="
|
| 65 |
-
savedir=
|
| 66 |
)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
try:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
ydl_opts = {
|
| 94 |
-
'format': 'bestaudio/best',
|
| 95 |
-
'postprocessors': [{
|
| 96 |
-
'key': 'FFmpegExtractAudio',
|
| 97 |
-
'preferredcodec': 'wav',
|
| 98 |
-
}],
|
| 99 |
-
'outtmpl': 'temp_video.%(ext)s',
|
| 100 |
-
}
|
| 101 |
-
try:
|
| 102 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 103 |
-
info_dict = ydl.extract_info(video_url, download=True)
|
| 104 |
-
video_filepath = ydl.prepare_filename(info_dict)
|
| 105 |
-
# yt-dlp with FFmpegExtractAudio should directly output the audio file
|
| 106 |
-
# The output file will have the same name as the video but with .wav extension
|
| 107 |
-
base, _ = os.path.splitext(video_filepath)
|
| 108 |
-
audio_filepath = base + '.wav'
|
| 109 |
-
|
| 110 |
-
# Rename the output file to the desired output_audio_path
|
| 111 |
-
if os.path.exists(audio_filepath):
|
| 112 |
-
# Use copy instead of rename to avoid issues if files are on different file systems
|
| 113 |
-
import shutil
|
| 114 |
-
shutil.copy2(audio_filepath, output_audio_path)
|
| 115 |
-
os.remove(audio_filepath) # Remove the original after copying
|
| 116 |
-
st.success(f"Audio extracted successfully to {output_audio_path}")
|
| 117 |
else:
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
except Exception as e:
|
| 129 |
-
st.error(f"
|
| 130 |
return False
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
def load_16k_audio_wav(filename):
|
| 134 |
-
"""Read and resample audio file to 16kHz without using tensorflow-io."""
|
| 135 |
-
# Use ffmpeg to resample the audio file to 16kHz
|
| 136 |
-
output_filename = "resampled_16k.wav"
|
| 137 |
-
|
| 138 |
try:
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
], check=True, capture_output=True)
|
| 142 |
-
|
| 143 |
-
# Read the resampled file
|
| 144 |
-
audio, sample_rate = tf.audio.decode_wav(tf.io.read_file(output_filename))
|
| 145 |
-
audio = tf.squeeze(audio, axis=-1)
|
| 146 |
|
| 147 |
-
#
|
| 148 |
-
if os.path.exists(output_filename):
|
| 149 |
-
os.remove(output_filename)
|
| 150 |
-
|
| 151 |
-
return audio
|
| 152 |
-
except Exception as e:
|
| 153 |
-
st.error(f"Error resampling audio: {e}")
|
| 154 |
-
# Fallback to just decoding without resampling
|
| 155 |
-
audio, _ = tf.audio.decode_wav(tf.io.read_file(filename))
|
| 156 |
-
audio = tf.squeeze(audio, axis=-1)
|
| 157 |
-
return audio
|
| 158 |
-
|
| 159 |
-
# Function that takes a recorded audio array and returns a tensor
|
| 160 |
-
def recorded_audio_to_tensor(audio_bytes):
|
| 161 |
-
# Save the audio bytes to a temporary file
|
| 162 |
-
temp_path = "temp_recorded_audio.wav"
|
| 163 |
-
with open(temp_path, "wb") as f:
|
| 164 |
-
f.write(audio_bytes)
|
| 165 |
-
|
| 166 |
-
# Load the audio file as a tensor
|
| 167 |
-
audio_tensor = load_16k_audio_wav(temp_path)
|
| 168 |
-
|
| 169 |
-
# Clean up
|
| 170 |
-
if os.path.exists(temp_path):
|
| 171 |
-
os.remove(temp_path)
|
| 172 |
-
|
| 173 |
-
return audio_tensor
|
| 174 |
-
|
| 175 |
-
# Function to use XLSR model for accent classification
|
| 176 |
-
def predict_accent_with_xlsr(audio_file_path, xlsr_model):
|
| 177 |
-
try:
|
| 178 |
-
# Classify the audio file
|
| 179 |
-
out_prob, score, index, text_lab = xlsr_model.classify_file(audio_file_path)
|
| 180 |
-
|
| 181 |
-
# Convert the prediction tensor to numpy for easier handling
|
| 182 |
probs = out_prob.squeeze().numpy()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
#
|
| 185 |
-
|
|
|
|
| 186 |
|
| 187 |
-
|
| 188 |
-
predicted_accent = text_lab
|
| 189 |
-
confidence = float(score)
|
| 190 |
|
| 191 |
-
return predicted_accent, confidence, accent_probs
|
| 192 |
except Exception as e:
|
| 193 |
-
st.error(f"
|
| 194 |
return None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
|
|
|
| 196 |
def main():
|
| 197 |
-
st.
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
""")
|
| 203 |
-
|
| 204 |
-
# Load models
|
| 205 |
-
xlsr_model = load_models()
|
| 206 |
-
|
| 207 |
-
# Check if ffmpeg is installed
|
| 208 |
-
if not is_ffmpeg_installed():
|
| 209 |
-
st.warning("FFmpeg is not installed. You won't be able to use YouTube URLs or process some audio files correctly.")
|
| 210 |
-
st.info("Please install FFmpeg. You can download it from [FFmpeg](https://ffmpeg.org/download.html)")
|
| 211 |
-
|
| 212 |
-
# Create tabs for different input methods
|
| 213 |
-
tab3 = st.tabs(["YouTube URL"])[0]
|
| 214 |
-
|
| 215 |
-
with tab3:
|
| 216 |
-
youtube_url = st.text_input("Enter YouTube URL", placeholder="https://www.youtube.com/watch?v=...")
|
| 217 |
-
|
| 218 |
-
if youtube_url:
|
| 219 |
-
if st.button("Extract Audio from YouTube", key="extract_btn"):
|
| 220 |
-
with st.spinner("Extracting audio from YouTube..."):
|
| 221 |
-
output_path = "youtube_audio.wav"
|
| 222 |
-
if extract_audio(youtube_url, output_path):
|
| 223 |
-
st.success("Audio extracted successfully!")
|
| 224 |
-
st.audio(output_path, format="audio/wav")
|
| 225 |
-
st.session_state.youtube_audio_path = output_path
|
| 226 |
-
else:
|
| 227 |
-
st.error("Failed to extract audio from YouTube URL.")
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
audio_file_path = None
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
audio_file_path = st.session_state.youtube_audio_path
|
| 236 |
-
else:
|
| 237 |
-
st.warning("Please provide a YouTube URL.")
|
| 238 |
-
st.stop()
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
if xlsr_predicted_accent:
|
| 248 |
-
st.success(f"π― **Predicted Accent: {xlsr_predicted_accent}** (Confidence: {xlsr_confidence:.2f})")
|
| 249 |
-
|
| 250 |
-
# Create visualization for XLSR results
|
| 251 |
-
sorted_probs = {k: v for k, v in sorted(xlsr_accent_probs.items(), key=lambda item: item[1], reverse=True)}
|
| 252 |
-
|
| 253 |
-
# Create a bar chart
|
| 254 |
-
fig, ax = plt.subplots(figsize=(10, 6))
|
| 255 |
-
accents = list(sorted_probs.keys())
|
| 256 |
-
probabilities = list(sorted_probs.values())
|
| 257 |
-
|
| 258 |
-
ax.bar(accents, probabilities, color='lightcoral')
|
| 259 |
-
ax.set_ylabel('Probability')
|
| 260 |
-
ax.set_title('XLSR Wav2Vec 2.0 Accent Probabilities (16 English Accents)')
|
| 261 |
-
plt.xticks(rotation=45)
|
| 262 |
-
plt.tight_layout()
|
| 263 |
-
|
| 264 |
-
st.pyplot(fig)
|
| 265 |
-
|
| 266 |
-
# Also display as a table
|
| 267 |
-
df = pd.DataFrame({
|
| 268 |
-
'Accent': accents,
|
| 269 |
-
'Probability': [f"{p:.2%}" for p in probabilities]
|
| 270 |
-
})
|
| 271 |
-
st.dataframe(df, hide_index=True)
|
| 272 |
-
|
| 273 |
-
# Add information about XLSR model
|
| 274 |
-
st.info("""
|
| 275 |
-
π **XLSR Wav2Vec 2.0 Model**: This state-of-the-art model achieves up to 95% accuracy
|
| 276 |
-
and can distinguish between 16 different English accent regions including specialized
|
| 277 |
-
accents like Bermuda, Hong Kong, and South Atlantic varieties.
|
| 278 |
-
""")
|
| 279 |
-
else:
|
| 280 |
-
st.error("XLSR model failed to classify the accent.")
|
| 281 |
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
-
#
|
| 287 |
st.markdown("---")
|
| 288 |
-
st.subheader("
|
| 289 |
-
|
| 290 |
-
st.
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
st.markdown("---")
|
| 305 |
st.markdown("""
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
|
|
|
| 309 |
|
|
|
|
|
|
|
| 310 |
if __name__ == "__main__":
|
| 311 |
main()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
import tempfile
|
| 4 |
+
import subprocess
|
| 5 |
+
import requests
|
| 6 |
+
from urllib.parse import urlparse
|
| 7 |
+
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
import torch
|
| 9 |
import torchaudio
|
| 10 |
+
|
| 11 |
+
# Set audio backend like in your working code
|
| 12 |
torchaudio.set_audio_backend("soundfile")
|
|
|
|
| 13 |
|
| 14 |
+
# Set cache directories for HuggingFace models
|
| 15 |
+
# Ensure this directory exists and is writable
|
| 16 |
+
cache_dir = "/tmp/hf_cache" # This is a common writable location on Linux/Docker
|
| 17 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 18 |
+
os.environ["HF_HOME"] = cache_dir
|
| 19 |
+
os.environ["HUGGINGFACE_HUB_CACHE"] = cache_dir
|
| 20 |
+
|
| 21 |
+
# --- Accent Model Cache Directory ---
|
| 22 |
+
# Create a dedicated directory for SpeechBrain models within the accessible cache
|
| 23 |
+
# Ensure this path is fully prepared and writable
|
| 24 |
+
speechbrain_model_cache_base = os.path.join(cache_dir, "speechbrain_models_accent_id")
|
| 25 |
+
os.makedirs(speechbrain_model_cache_base, exist_ok=True)
|
| 26 |
+
|
| 27 |
+
# The specific model's subdirectory within the cache
|
| 28 |
+
# This is the full path that 'savedir' should point to
|
| 29 |
+
model_save_path = os.path.join(speechbrain_model_cache_base, "accent-id-commonaccent_xlsr-en-english")
|
| 30 |
+
os.makedirs(model_save_path, exist_ok=True) # Ensure this specific model directory exists and is writable
|
| 31 |
+
# --- End Accent Model Cache Directory ---
|
| 32 |
+
|
| 33 |
+
# Try importing the accent detection model
|
| 34 |
try:
|
|
|
|
| 35 |
from speechbrain.pretrained.interfaces import foreign_class
|
| 36 |
+
MODEL_AVAILABLE = True
|
| 37 |
+
|
| 38 |
+
@st.cache_resource
|
| 39 |
+
def load_accent_model():
|
| 40 |
+
"""Load the XLSR Wav2Vec 2.0 accent classification model"""
|
| 41 |
+
try:
|
| 42 |
+
st.info(f"Attempting to load model from: {model_save_path}")
|
| 43 |
+
model = foreign_class(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
source="Jzuluaga/accent-id-commonaccent_xlsr-en-english",
|
| 45 |
pymodule_file="custom_interface.py",
|
| 46 |
+
classname="CustomEncoderWav2vec22Classifier", # Note: Double check if this is the correct classname. It was CustomEncoderWav2vec2Classifier in the original.
|
| 47 |
+
savedir=model_save_path # Use the pre-prepared full path
|
| 48 |
)
|
| 49 |
+
return model
|
| 50 |
+
except Exception as e:
|
| 51 |
+
st.error(f"Failed to load accent model: [Errno 13] Permission denied: '{e}' - Please ensure '{model_save_path}' is writable.")
|
| 52 |
+
st.error(f"Detailed Error: {e}")
|
| 53 |
+
return None
|
| 54 |
+
except ImportError:
|
| 55 |
+
MODEL_AVAILABLE = False
|
| 56 |
+
st.error("SpeechBrain not available. Install with: pip install speechbrain")
|
| 57 |
+
|
| 58 |
+
# Accent categories with confidence thresholds
|
| 59 |
+
ACCENT_CATEGORIES = [
|
| 60 |
+
"US", "England", "Australia", "Indian", "Canada",
|
| 61 |
+
"Scotland", "Ireland", "Wales", "African", "NewZealand",
|
| 62 |
+
"Bermuda", "Malaysia", "Philippines", "Singapore",
|
| 63 |
+
"HongKong", "SouthAtlantic"]
|
| 64 |
+
|
| 65 |
+
def download_video_audio(url, output_path):
|
| 66 |
+
"""Download and extract audio from video URL"""
|
| 67 |
try:
|
| 68 |
+
# Check if it's a direct video file
|
| 69 |
+
if url.endswith(('.mp4', '.avi', '.mov', '.mkv')):
|
| 70 |
+
# Download direct video file
|
| 71 |
+
response = requests.get(url, stream=True, timeout=30)
|
| 72 |
+
response.raise_for_status()
|
| 73 |
+
|
| 74 |
+
temp_video = output_path.replace('.wav', '.mp4')
|
| 75 |
+
with open(temp_video, 'wb') as f:
|
| 76 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 77 |
+
f.write(chunk)
|
| 78 |
+
|
| 79 |
+
# Extract audio using ffmpeg
|
| 80 |
+
cmd = [
|
| 81 |
+
'ffmpeg', '-i', temp_video, '-ar', '16000',
|
| 82 |
+
'-ac', '1', '-y', output_path
|
| 83 |
+
]
|
| 84 |
+
subprocess.run(cmd, check=True, capture_output=True)
|
| 85 |
+
os.remove(temp_video)
|
| 86 |
+
return True
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
else:
|
| 89 |
+
# Use yt-dlp for other video platforms (Loom, YouTube, etc.)
|
| 90 |
+
cmd = [
|
| 91 |
+
'yt-dlp', '--extract-audio', '--audio-format', 'wav',
|
| 92 |
+
'--audio-quality', '0', '--output', output_path.replace('.wav', '.%(ext)s'),
|
| 93 |
+
url
|
| 94 |
+
]
|
| 95 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 96 |
+
|
| 97 |
+
if result.returncode == 0:
|
| 98 |
+
return True
|
| 99 |
+
else:
|
| 100 |
+
st.error(f"yt-dlp error: {result.stderr}")
|
| 101 |
+
return False
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
+
st.error(f"Download failed: {e}")
|
| 105 |
return False
|
| 106 |
+
def analyze_accent(audio_file_path, model):
|
| 107 |
+
"""Analyze accent using XLSR Wav2Vec 2.0 model"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
try:
|
| 109 |
+
# Get predictions from the model - same as your working code
|
| 110 |
+
out_prob, score, index, text_lab = model.classify_file(audio_file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
# Convert probabilities to dictionary - same approach as your working code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
probs = out_prob.squeeze().numpy()
|
| 114 |
+
accent_scores = {
|
| 115 |
+
ACCENT_CATEGORIES[i]: float(probs[i]) * 100
|
| 116 |
+
for i in range(len(ACCENT_CATEGORIES))
|
| 117 |
+
}
|
| 118 |
|
| 119 |
+
# Get top prediction - same as your working code
|
| 120 |
+
predicted_accent = text_lab # Use text_lab like your working code
|
| 121 |
+
confidence = float(score) * 100
|
| 122 |
|
| 123 |
+
return predicted_accent, confidence, accent_scores
|
|
|
|
|
|
|
| 124 |
|
|
|
|
| 125 |
except Exception as e:
|
| 126 |
+
st.error(f"Accent analysis failed: {e}")
|
| 127 |
return None, None, None
|
| 128 |
+
def generate_summary(accent, confidence, top_scores):
|
| 129 |
+
"""Generate a summary of the accent analysis"""
|
| 130 |
+
if confidence > 80:
|
| 131 |
+
confidence_level = "Very High"
|
| 132 |
+
elif confidence > 60:
|
| 133 |
+
confidence_level = "High"
|
| 134 |
+
elif confidence > 40:
|
| 135 |
+
confidence_level = "Moderate"
|
| 136 |
+
else:
|
| 137 |
+
confidence_level = "Low"
|
| 138 |
+
|
| 139 |
+
# Get top 3 accents
|
| 140 |
+
top_3 = sorted(top_scores.items(), key=lambda x: x[1], reverse=True)[:3]
|
| 141 |
+
|
| 142 |
+
summary = f"""
|
| 143 |
+
**Primary Accent:** {accent} ({confidence:.1f}% confidence)
|
| 144 |
+
**Confidence Level:** {confidence_level}
|
| 145 |
+
|
| 146 |
+
**Top 3 Detected Accents:**
|
| 147 |
+
1. {top_3[0][0]}: {top_3[0][1]:.1f}%
|
| 148 |
+
2. {top_3[1][0]}: {top_3[1][1]:.1f}%
|
| 149 |
+
3. {top_3[2][0]}: {top_3[2][1]:.1f}%
|
| 150 |
+
|
| 151 |
+
**Hiring Recommendation:**
|
| 152 |
+
"""
|
| 153 |
+
|
| 154 |
+
if confidence > 70:
|
| 155 |
+
summary += "β
Strong English accent detected - Suitable for English-speaking roles"
|
| 156 |
+
elif confidence > 50:
|
| 157 |
+
summary += "β οΈ Moderate English accent detected - May require accent assessment"
|
| 158 |
+
else:
|
| 159 |
+
summary += "β Weak English accent signal - Further evaluation recommended"
|
| 160 |
|
| 161 |
+
return summary
|
| 162 |
def main():
|
| 163 |
+
st.set_page_config(
|
| 164 |
+
page_title="English Accent Detector",
|
| 165 |
+
page_icon="π£οΈ",
|
| 166 |
+
layout="wide"
|
| 167 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
st.title("π£οΈ English Accent Detection Tool")
|
| 170 |
+
st.subheader("For Hiring & Language Assessment")
|
|
|
|
| 171 |
|
| 172 |
+
st.markdown("""
|
| 173 |
+
**Purpose:** Analyze spoken English accents from video URLs to assist in hiring decisions.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
+
**Supported:** Loom videos, direct MP4 links, YouTube, and other video platforms.
|
| 176 |
+
""")
|
| 177 |
+
|
| 178 |
+
# Load model
|
| 179 |
+
if not MODEL_AVAILABLE:
|
| 180 |
+
st.stop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
+
with st.spinner("Loading XLSR Wav2Vec 2.0 model..."):
|
| 183 |
+
model = load_accent_model()
|
| 184 |
+
if not model:
|
| 185 |
+
st.error("β Could not load accent detection model")
|
| 186 |
+
st.stop()
|
| 187 |
+
st.success("β
Accent detection model loaded successfully!")
|
| 188 |
|
| 189 |
+
# Input section
|
| 190 |
st.markdown("---")
|
| 191 |
+
st.subheader("π₯ Video Input")
|
| 192 |
+
|
| 193 |
+
video_url = st.text_input(
|
| 194 |
+
"Enter Video URL",
|
| 195 |
+
placeholder="https://www.loom.com/share/... or direct MP4 link",
|
| 196 |
+
help="Supports Loom, YouTube, direct video files, and most video platforms"
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
if video_url:
|
| 200 |
+
st.info(f"π **URL:** {video_url}")
|
| 201 |
+
|
| 202 |
+
if st.button("π― Analyze Accent", type="primary"):
|
| 203 |
+
# Create temporary file for audio
|
| 204 |
+
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp_file:
|
| 205 |
+
audio_path = tmp_file.name
|
| 206 |
+
|
| 207 |
+
try:
|
| 208 |
+
# Step 1: Download and extract audio
|
| 209 |
+
with st.spinner("π₯ Downloading video and extracting audio..."):
|
| 210 |
+
if download_video_audio(video_url, audio_path):
|
| 211 |
+
st.success("β
Audio extracted successfully")
|
| 212 |
+
|
| 213 |
+
# Play the extracted audio
|
| 214 |
+
with open(audio_path, 'rb') as audio_file:
|
| 215 |
+
st.audio(audio_file.read(), format="audio/wav")
|
| 216 |
+
else:
|
| 217 |
+
st.error("β Failed to extract audio")
|
| 218 |
+
st.stop()
|
| 219 |
+
|
| 220 |
+
# Step 2: Analyze accent
|
| 221 |
+
with st.spinner("π§ Analyzing accent with XLSR Wav2Vec 2.0..."):
|
| 222 |
+
accent, confidence, accent_scores = analyze_accent(audio_path, model)
|
| 223 |
+
|
| 224 |
+
if accent:
|
| 225 |
+
# Display results
|
| 226 |
+
st.markdown("---")
|
| 227 |
+
st.subheader("π Analysis Results")
|
| 228 |
+
|
| 229 |
+
# Main result
|
| 230 |
+
col1, col2 = st.columns(2)
|
| 231 |
+
|
| 232 |
+
with col1:
|
| 233 |
+
st.metric(
|
| 234 |
+
label="π― Detected Accent",
|
| 235 |
+
value=accent,
|
| 236 |
+
help="Primary English accent classification"
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
with col2:
|
| 240 |
+
st.metric(
|
| 241 |
+
label="πͺ Confidence Score",
|
| 242 |
+
value=f"{confidence:.1f}%",
|
| 243 |
+
help="Model confidence in the prediction"
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
# Detailed breakdown
|
| 247 |
+
st.subheader("π Accent Probability Breakdown")
|
| 248 |
+
|
| 249 |
+
# Sort and display top 8 accents
|
| 250 |
+
sorted_accents = sorted(accent_scores.items(), key=lambda x: x[1], reverse=True)[:8]
|
| 251 |
+
|
| 252 |
+
for accent_name, score in sorted_accents:
|
| 253 |
+
st.progress(score/100, text=f"{accent_name}: {score:.1f}%")
|
| 254 |
+
|
| 255 |
+
# Summary
|
| 256 |
+
st.subheader("π Assessment Summary")
|
| 257 |
+
summary = generate_summary(accent, confidence, accent_scores)
|
| 258 |
+
st.markdown(summary)
|
| 259 |
+
|
| 260 |
+
# JSON output for API integration
|
| 261 |
+
with st.expander("π§ JSON Output (for API integration)"):
|
| 262 |
+
result_json = {
|
| 263 |
+
"primary_accent": accent,
|
| 264 |
+
"confidence_score": round(confidence, 1),
|
| 265 |
+
"accent_probabilities": {k: round(v, 1) for k, v in accent_scores.items()},
|
| 266 |
+
"top_3_accents": [
|
| 267 |
+
{"accent": k, "probability": round(v, 1)}
|
| 268 |
+
for k, v in sorted(accent_scores.items(), key=lambda x: x[1], reverse=True)[:3]
|
| 269 |
+
],
|
| 270 |
+
"recommendation": "suitable" if confidence > 70 else "assessment_needed" if confidence > 50 else "further_evaluation"
|
| 271 |
+
}
|
| 272 |
+
st.json(result_json)
|
| 273 |
+
|
| 274 |
+
else:
|
| 275 |
+
st.error("β Accent analysis failed")
|
| 276 |
+
|
| 277 |
+
finally:
|
| 278 |
+
# Cleanup
|
| 279 |
+
if os.path.exists(audio_path):
|
| 280 |
+
os.remove(audio_path)
|
| 281 |
+
|
| 282 |
+
# Footer
|
| 283 |
st.markdown("---")
|
| 284 |
st.markdown("""
|
| 285 |
+
**Technical Details:**
|
| 286 |
+
- Model: XLSR Wav2Vec 2.0 (95% accuracy on English accents)
|
| 287 |
+
- Supports: 16 English accent varieties
|
| 288 |
+
- Processing: Automatic audio extraction and resampling to 16kHz
|
| 289 |
|
| 290 |
+
**Built for hiring teams to assess English language proficiency**
|
| 291 |
+
""")
|
| 292 |
if __name__ == "__main__":
|
| 293 |
main()
|