Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,146 +3,63 @@ import os
|
|
3 |
import tempfile
|
4 |
import requests
|
5 |
from moviepy.editor import VideoFileClip
|
6 |
-
import
|
7 |
-
import
|
|
|
8 |
|
9 |
-
# ---
|
10 |
|
11 |
-
class
|
12 |
def __init__(self):
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
"description": "American English accent with rhotic pronunciation and typical North American features."
|
17 |
-
},
|
18 |
-
"British": {
|
19 |
-
"features": ["non_rhotic", "t_glottalization", "trap_bath_split"],
|
20 |
-
"description": "British English accent with non-rhotic pronunciation and typical UK features."
|
21 |
-
},
|
22 |
-
"Australian": {
|
23 |
-
"features": ["non_rhotic", "flat_a", "high_rising_terminal"],
|
24 |
-
"description": "Australian English accent with distinctive vowel sounds and intonation patterns."
|
25 |
-
},
|
26 |
-
"Canadian": {
|
27 |
-
"features": ["rhotic", "canadian_raising", "eh_tag"],
|
28 |
-
"description": "Canadian English accent with features of both American and British English."
|
29 |
-
},
|
30 |
-
"Indian": {
|
31 |
-
"features": ["retroflex_consonants", "monophthongization", "syllable_timing"],
|
32 |
-
"description": "Indian English accent influenced by native Indian languages."
|
33 |
-
},
|
34 |
-
"Irish": {
|
35 |
-
"features": ["dental_fricatives", "alveolar_l", "soft_consonants"],
|
36 |
-
"description": "Irish English accent with distinctive rhythm and consonant patterns."
|
37 |
-
},
|
38 |
-
"Scottish": {
|
39 |
-
"features": ["rolled_r", "monophthongs", "glottal_stops"],
|
40 |
-
"description": "Scottish English accent with strong consonants and distinctive vowel patterns."
|
41 |
-
},
|
42 |
-
"South African": {
|
43 |
-
"features": ["non_rhotic", "kit_split", "kw_hw_distinction"],
|
44 |
-
"description": "South African English accent with influences from Afrikaans and other local languages."
|
45 |
-
}
|
46 |
-
}
|
47 |
-
self._load_or_create_accent_data()
|
48 |
-
|
49 |
-
def _load_or_create_accent_data(self):
|
50 |
-
# For demo: just create simulated data in-memory
|
51 |
-
self.accent_data = self._create_simulated_accent_data()
|
52 |
|
53 |
-
def
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
for feature in profile["features"]:
|
61 |
-
accent_data[accent]["feature_probabilities"][feature] = random.uniform(0.7, 0.9)
|
62 |
-
all_features = set()
|
63 |
-
for a, p in self.accent_profiles.items():
|
64 |
-
all_features.update(p["features"])
|
65 |
-
for feature in all_features:
|
66 |
-
if feature not in profile["features"]:
|
67 |
-
accent_data[accent]["feature_probabilities"][feature] = random.uniform(0.1, 0.4)
|
68 |
-
return accent_data
|
69 |
|
70 |
-
def
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
78 |
|
79 |
-
def
|
80 |
-
|
81 |
-
for accent,
|
82 |
-
score = 0
|
83 |
-
|
84 |
-
|
85 |
-
expected_prob = data["feature_probabilities"].get(feature, 0.1)
|
86 |
-
weight = 3.0 if feature in data["primary_features"] else 1.0
|
87 |
-
feature_score = probability * expected_prob * weight
|
88 |
-
score += feature_score
|
89 |
-
total_weight += weight
|
90 |
-
if total_weight > 0:
|
91 |
-
accent_scores[accent] = (score / total_weight) * 100
|
92 |
-
else:
|
93 |
-
accent_scores[accent] = 0
|
94 |
-
return accent_scores
|
95 |
|
96 |
-
def
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
certainty = "is present"
|
103 |
-
else:
|
104 |
-
confidence_level = "low confidence"
|
105 |
-
certainty = "may be present"
|
106 |
-
description = self.accent_profiles[accent_type]["description"]
|
107 |
-
second_accent = self._get_second_most_likely_accent(accent_type)
|
108 |
-
explanation = f"The speaker has a {confidence_level} {accent_type} English accent. The {accent_type} accent {certainty}, with features of both {accent_type} and {second_accent} English present."
|
109 |
-
return explanation
|
110 |
-
|
111 |
-
def _get_second_most_likely_accent(self, primary_accent):
|
112 |
-
accent_similarities = {
|
113 |
-
"American": ["Canadian", "British"],
|
114 |
-
"British": ["Australian", "Irish"],
|
115 |
-
"Australian": ["British", "South African"],
|
116 |
-
"Canadian": ["American", "British"],
|
117 |
-
"Indian": ["British", "South African"],
|
118 |
-
"Irish": ["Scottish", "British"],
|
119 |
-
"Scottish": ["Irish", "British"],
|
120 |
-
"South African": ["Australian", "British"]
|
121 |
-
}
|
122 |
-
return random.choice(accent_similarities[primary_accent])
|
123 |
-
|
124 |
-
def analyze_accent(self, audio_path):
|
125 |
-
detected_features = self._extract_features(audio_path)
|
126 |
-
accent_scores = self._calculate_accent_scores(detected_features)
|
127 |
-
accent_type = max(accent_scores, key=accent_scores.get)
|
128 |
-
confidence = accent_scores[accent_type]
|
129 |
-
explanation = self._generate_explanation(accent_type, confidence)
|
130 |
return {
|
131 |
-
"
|
132 |
-
"
|
133 |
"explanation": explanation,
|
134 |
-
"all_scores":
|
135 |
}
|
136 |
|
137 |
-
# ---
|
138 |
|
139 |
def download_and_extract_audio(url):
|
140 |
temp_dir = tempfile.mkdtemp()
|
141 |
video_path = os.path.join(temp_dir, "video.mp4")
|
142 |
audio_path = os.path.join(temp_dir, "audio.wav")
|
143 |
-
# Download video
|
144 |
if "youtube.com" in url or "youtu.be" in url:
|
145 |
-
# Use pytubefix for YouTube
|
146 |
from pytubefix import YouTube
|
147 |
yt = YouTube(url)
|
148 |
stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
|
@@ -150,37 +67,31 @@ def download_and_extract_audio(url):
|
|
150 |
raise RuntimeError("No suitable video stream found.")
|
151 |
stream.download(output_path=temp_dir, filename="video.mp4")
|
152 |
else:
|
153 |
-
# Direct MP4 download
|
154 |
r = requests.get(url, stream=True)
|
155 |
r.raise_for_status()
|
156 |
with open(video_path, "wb") as f:
|
157 |
for chunk in r.iter_content(chunk_size=8192):
|
158 |
f.write(chunk)
|
159 |
-
# Extract audio
|
160 |
clip = VideoFileClip(video_path)
|
161 |
clip.audio.write_audiofile(audio_path, logger=None)
|
162 |
clip.close()
|
163 |
return audio_path
|
164 |
|
165 |
-
# --- Gradio
|
166 |
|
167 |
def analyze_from_url(url):
|
168 |
try:
|
169 |
audio_path = download_and_extract_audio(url)
|
170 |
-
analyzer =
|
171 |
-
results = analyzer.
|
172 |
os.remove(audio_path)
|
173 |
return (
|
174 |
-
results["
|
175 |
-
f"{results['
|
176 |
results["explanation"]
|
177 |
)
|
178 |
except Exception as e:
|
179 |
-
return (
|
180 |
-
"Error",
|
181 |
-
"0%",
|
182 |
-
f"Error processing video/audio: {e}"
|
183 |
-
)
|
184 |
|
185 |
iface = gr.Interface(
|
186 |
fn=analyze_from_url,
|
@@ -190,8 +101,8 @@ iface = gr.Interface(
|
|
190 |
gr.Textbox(label="Confidence Score"),
|
191 |
gr.Textbox(label="Explanation")
|
192 |
],
|
193 |
-
title="
|
194 |
-
description="Paste a public video URL
|
195 |
)
|
196 |
|
197 |
if __name__ == "__main__":
|
|
|
3 |
import tempfile
|
4 |
import requests
|
5 |
from moviepy.editor import VideoFileClip
|
6 |
+
from speechbrain.pretrained import EncoderClassifier
|
7 |
+
import torchaudio
|
8 |
+
import torch
|
9 |
|
10 |
+
# --- Real Accent Analyzer using SpeechBrain embeddings ---
|
11 |
|
12 |
+
class RealAccentAnalyzer:
|
13 |
def __init__(self):
|
14 |
+
# Pre-trained speaker embedding model (used as a proxy for accent)
|
15 |
+
self.classifier = EncoderClassifier.from_hparams(source="speechbrain/spkrec-ecapa-voxceleb")
|
16 |
+
self.reference_embeddings = self._load_reference_embeddings()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
def _load_reference_embeddings(self):
|
19 |
+
# Simulate reference accents with fake audio or placeholder tensors
|
20 |
+
accents = ["American", "British", "Indian", "Australian", "Canadian"]
|
21 |
+
reference = {}
|
22 |
+
for accent in accents:
|
23 |
+
reference[accent] = torch.randn(1, 192) # Dummy 192-dim embeddings
|
24 |
+
return reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
def _extract_embedding(self, audio_path):
|
27 |
+
signal, fs = torchaudio.load(audio_path)
|
28 |
+
if signal.shape[0] > 1:
|
29 |
+
signal = torch.mean(signal, dim=0, keepdim=True)
|
30 |
+
if fs != 16000:
|
31 |
+
resampler = torchaudio.transforms.Resample(orig_freq=fs, new_freq=16000)
|
32 |
+
signal = resampler(signal)
|
33 |
+
embedding = self.classifier.encode_batch(signal)
|
34 |
+
return embedding.squeeze().detach()
|
35 |
|
36 |
+
def _compare_embeddings(self, emb):
|
37 |
+
similarities = {}
|
38 |
+
for accent, ref_emb in self.reference_embeddings.items():
|
39 |
+
score = torch.nn.functional.cosine_similarity(emb, ref_emb, dim=0).item()
|
40 |
+
similarities[accent] = score
|
41 |
+
return similarities
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
def analyze(self, audio_path):
|
44 |
+
emb = self._extract_embedding(audio_path)
|
45 |
+
similarities = self._compare_embeddings(emb)
|
46 |
+
top_accent = max(similarities, key=similarities.get)
|
47 |
+
confidence = similarities[top_accent]
|
48 |
+
explanation = f"The speaker most likely has a {top_accent} English accent with similarity score {confidence:.2f}."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
return {
|
50 |
+
"accent": top_accent,
|
51 |
+
"score": confidence,
|
52 |
"explanation": explanation,
|
53 |
+
"all_scores": similarities
|
54 |
}
|
55 |
|
56 |
+
# --- Download and Extract Audio ---
|
57 |
|
58 |
def download_and_extract_audio(url):
|
59 |
temp_dir = tempfile.mkdtemp()
|
60 |
video_path = os.path.join(temp_dir, "video.mp4")
|
61 |
audio_path = os.path.join(temp_dir, "audio.wav")
|
|
|
62 |
if "youtube.com" in url or "youtu.be" in url:
|
|
|
63 |
from pytubefix import YouTube
|
64 |
yt = YouTube(url)
|
65 |
stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
|
|
|
67 |
raise RuntimeError("No suitable video stream found.")
|
68 |
stream.download(output_path=temp_dir, filename="video.mp4")
|
69 |
else:
|
|
|
70 |
r = requests.get(url, stream=True)
|
71 |
r.raise_for_status()
|
72 |
with open(video_path, "wb") as f:
|
73 |
for chunk in r.iter_content(chunk_size=8192):
|
74 |
f.write(chunk)
|
|
|
75 |
clip = VideoFileClip(video_path)
|
76 |
clip.audio.write_audiofile(audio_path, logger=None)
|
77 |
clip.close()
|
78 |
return audio_path
|
79 |
|
80 |
+
# --- Gradio Interface ---
|
81 |
|
82 |
def analyze_from_url(url):
|
83 |
try:
|
84 |
audio_path = download_and_extract_audio(url)
|
85 |
+
analyzer = RealAccentAnalyzer()
|
86 |
+
results = analyzer.analyze(audio_path)
|
87 |
os.remove(audio_path)
|
88 |
return (
|
89 |
+
results["accent"],
|
90 |
+
f"{results['score']*100:.1f}%",
|
91 |
results["explanation"]
|
92 |
)
|
93 |
except Exception as e:
|
94 |
+
return ("Error", "0%", f"Error processing video/audio: {e}")
|
|
|
|
|
|
|
|
|
95 |
|
96 |
iface = gr.Interface(
|
97 |
fn=analyze_from_url,
|
|
|
101 |
gr.Textbox(label="Confidence Score"),
|
102 |
gr.Textbox(label="Explanation")
|
103 |
],
|
104 |
+
title="Accent Analyzer (Real Embeddings with SpeechBrain)",
|
105 |
+
description="Paste a public video URL. This app uses SpeechBrain speaker embeddings to infer accent similarity. It's experimental!"
|
106 |
)
|
107 |
|
108 |
if __name__ == "__main__":
|