Update app.py
Browse files
app.py
CHANGED
@@ -8,150 +8,100 @@ import torchaudio
|
|
8 |
import torch
|
9 |
import ffmpeg
|
10 |
|
11 |
-
#
|
12 |
try:
|
13 |
from speechbrain.inference import EncoderClassifier
|
14 |
-
|
15 |
source="speechbrain/lang-id-commonlanguage_ecapa",
|
16 |
savedir="pretrained_models/lang-id-commonlanguage_ecapa"
|
17 |
)
|
18 |
-
|
19 |
except Exception as e:
|
20 |
-
st.warning(
|
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 |
-
for
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
def analyze_accent(self, audio_path):
|
70 |
-
if not SPEECHBRAIN_LOADED:
|
71 |
-
return self._simulate_accent_classification(audio_path)
|
72 |
-
|
73 |
-
try:
|
74 |
-
signal, sr = torchaudio.load(audio_path)
|
75 |
-
duration = signal.shape[1] / sr
|
76 |
-
if duration < 1.0:
|
77 |
-
raise ValueError("Audio too short to analyze.")
|
78 |
-
|
79 |
-
if signal.shape[0] > 1:
|
80 |
-
signal = signal.mean(dim=0, keepdim=True)
|
81 |
-
if sr != 16000:
|
82 |
-
signal = torchaudio.transforms.Resample(sr, 16000)(signal)
|
83 |
-
signal = signal.unsqueeze(0) # [1, 1, time]
|
84 |
-
|
85 |
-
pred = speechbrain_classifier.classify_batch(signal)
|
86 |
-
probs = pred[0].squeeze(0).tolist()
|
87 |
-
labels = pred[1][0]
|
88 |
-
scores = {speechbrain_classifier.hparams.label_encoder.ind2lab[i]: p * 100 for i, p in enumerate(probs)}
|
89 |
-
|
90 |
-
if labels[0] == 'en':
|
91 |
-
result = self._simulate_accent_classification(audio_path)
|
92 |
-
result["all_scores"] = scores
|
93 |
-
return result
|
94 |
-
return {
|
95 |
-
"accent_type": labels[0],
|
96 |
-
"confidence": max(probs) * 100,
|
97 |
-
"explanation": f"Detected language: **{labels[0]}** ({max(probs)*100:.1f}%)",
|
98 |
-
"all_scores": scores
|
99 |
-
}
|
100 |
-
except Exception as e:
|
101 |
-
st.warning(f"Fallback to simulation: {e}")
|
102 |
-
return self._simulate_accent_classification(audio_path)
|
103 |
-
|
104 |
-
def download_and_extract_audio(url_or_path, is_upload=False):
|
105 |
temp_dir = tempfile.mkdtemp()
|
106 |
-
video_path = os.path.join(temp_dir, "
|
107 |
audio_path = os.path.join(temp_dir, "audio.wav")
|
108 |
|
109 |
if is_upload:
|
110 |
with open(video_path, "wb") as f:
|
111 |
-
f.write(
|
112 |
else:
|
113 |
-
with requests.get(
|
114 |
r.raise_for_status()
|
115 |
with open(video_path, 'wb') as f:
|
116 |
for chunk in r.iter_content(chunk_size=8192):
|
117 |
f.write(chunk)
|
118 |
|
119 |
-
(
|
120 |
-
ffmpeg
|
121 |
-
.input(video_path)
|
122 |
-
.output(audio_path, ar=16000, ac=1, format='wav')
|
123 |
-
.run(quiet=True, overwrite_output=True)
|
124 |
-
)
|
125 |
return audio_path
|
126 |
|
127 |
-
# --- Streamlit
|
128 |
-
st.set_page_config(page_title="Accent Analyzer", layout="
|
129 |
-
st.title("π£οΈ English Accent
|
130 |
|
131 |
-
st.markdown("
|
132 |
|
133 |
-
|
134 |
-
uploaded_file = st.file_uploader("π Or upload a file
|
135 |
|
136 |
-
if st.button("Analyze"):
|
137 |
-
if not
|
138 |
-
st.error("Please
|
139 |
else:
|
140 |
-
|
141 |
-
|
142 |
-
audio_path =
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
ax.bar(labels, values, color='skyblue')
|
151 |
-
ax.set_ylabel('Confidence (%)')
|
152 |
-
ax.set_title('Accent/Language Confidence')
|
153 |
-
plt.xticks(rotation=45)
|
154 |
-
st.pyplot(fig)
|
155 |
-
|
156 |
-
except Exception as e:
|
157 |
-
st.error(f"Failed to analyze: {e}")
|
|
|
8 |
import torch
|
9 |
import ffmpeg
|
10 |
|
11 |
+
# Try loading SpeechBrain
|
12 |
try:
|
13 |
from speechbrain.inference import EncoderClassifier
|
14 |
+
classifier = EncoderClassifier.from_hparams(
|
15 |
source="speechbrain/lang-id-commonlanguage_ecapa",
|
16 |
savedir="pretrained_models/lang-id-commonlanguage_ecapa"
|
17 |
)
|
18 |
+
SB_READY = True
|
19 |
except Exception as e:
|
20 |
+
st.warning(" SpeechBrain model load failed. Falling back to simulation.")
|
21 |
+
SB_READY = False
|
22 |
+
|
23 |
+
# Accent Profiles for English detection
|
24 |
+
accent_profiles = {
|
25 |
+
"American": ["rhotic", "flapped_t", "cot_caught_merger"],
|
26 |
+
"British": ["non_rhotic", "t_glottalization", "trap_bath_split"],
|
27 |
+
"Australian": ["non_rhotic", "flat_a", "high_rising_terminal"],
|
28 |
+
"Canadian": ["rhotic", "canadian_raising", "eh_tag"],
|
29 |
+
"Indian": ["retroflex_consonants", "monophthongization", "syllable_timing"]
|
30 |
+
}
|
31 |
+
|
32 |
+
def simulate_accent_classification():
|
33 |
+
accent = random.choice(list(accent_profiles.keys()))
|
34 |
+
confidence = random.uniform(75, 98)
|
35 |
+
return {
|
36 |
+
"accent": accent,
|
37 |
+
"confidence": round(confidence, 2),
|
38 |
+
"summary": f"Simulated detection: {accent} accent with {confidence:.2f}% confidence."
|
39 |
+
}
|
40 |
+
|
41 |
+
def real_accent_classification(audio_path):
|
42 |
+
try:
|
43 |
+
signal, sr = torchaudio.load(audio_path)
|
44 |
+
if signal.shape[0] > 1:
|
45 |
+
signal = signal.mean(dim=0, keepdim=True)
|
46 |
+
if sr != 16000:
|
47 |
+
signal = torchaudio.transforms.Resample(sr, 16000)(signal)
|
48 |
+
signal = signal.unsqueeze(0)
|
49 |
+
|
50 |
+
pred = classifier.classify_batch(signal)
|
51 |
+
probs = pred[0].squeeze(0).tolist()
|
52 |
+
labels = pred[1][0]
|
53 |
+
|
54 |
+
lang_scores = {classifier.hparams.label_encoder.ind2lab[i]: p * 100 for i, p in enumerate(probs)}
|
55 |
+
top_lang = max(lang_scores, key=lang_scores.get)
|
56 |
+
|
57 |
+
if top_lang != "en":
|
58 |
+
return {"accent": "Non-English", "confidence": lang_scores[top_lang], "summary": f"Detected language: {top_lang}"}
|
59 |
+
|
60 |
+
# Simulate accent if English
|
61 |
+
result = simulate_accent_classification()
|
62 |
+
result["summary"] += f" (Base language: English)"
|
63 |
+
return result
|
64 |
+
except Exception as e:
|
65 |
+
return simulate_accent_classification()
|
66 |
+
|
67 |
+
def extract_audio(url_or_file, is_upload=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
temp_dir = tempfile.mkdtemp()
|
69 |
+
video_path = os.path.join(temp_dir, "input_video.mp4")
|
70 |
audio_path = os.path.join(temp_dir, "audio.wav")
|
71 |
|
72 |
if is_upload:
|
73 |
with open(video_path, "wb") as f:
|
74 |
+
f.write(url_or_file.read())
|
75 |
else:
|
76 |
+
with requests.get(url_or_file, stream=True) as r:
|
77 |
r.raise_for_status()
|
78 |
with open(video_path, 'wb') as f:
|
79 |
for chunk in r.iter_content(chunk_size=8192):
|
80 |
f.write(chunk)
|
81 |
|
82 |
+
ffmpeg.input(video_path).output(audio_path, ar=16000, ac=1).run(overwrite_output=True, quiet=True)
|
|
|
|
|
|
|
|
|
|
|
83 |
return audio_path
|
84 |
|
85 |
+
# --- Streamlit UI ---
|
86 |
+
st.set_page_config(page_title="English Accent Analyzer", layout="centered")
|
87 |
+
st.title("π£οΈ English Accent Analyzer")
|
88 |
|
89 |
+
st.markdown("### π― Objective:\nUpload or link a video/audio of a speaker. Weβll detect if they're speaking English and simulate the accent.")
|
90 |
|
91 |
+
url_input = st.text_input("π Paste public Loom or direct MP4/WAV link:")
|
92 |
+
uploaded_file = st.file_uploader("π Or upload a video/audio file", type=["mp4", "wav"])
|
93 |
|
94 |
+
if st.button(" Analyze"):
|
95 |
+
if not url_input and not uploaded_file:
|
96 |
+
st.error("Please provide a valid URL or upload a file.")
|
97 |
else:
|
98 |
+
with st.spinner("Analyzing..."):
|
99 |
+
try:
|
100 |
+
audio_path = extract_audio(uploaded_file if uploaded_file else url_input, is_upload=bool(uploaded_file))
|
101 |
+
result = real_accent_classification(audio_path) if SB_READY else simulate_accent_classification()
|
102 |
+
|
103 |
+
st.success(f"π§ Detected Accent: **{result['accent']}**")
|
104 |
+
st.metric("Confidence", f"{result['confidence']}%")
|
105 |
+
st.markdown(f"π {result['summary']}")
|
106 |
+
except Exception as e:
|
107 |
+
st.error(f"β Error during analysis: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|