Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +99 -38
src/streamlit_app.py
CHANGED
@@ -1,40 +1,101 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
11 |
-
forums](https://discuss.streamlit.io).
|
12 |
-
|
13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
14 |
-
"""
|
15 |
-
|
16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
18 |
-
|
19 |
-
indices = np.linspace(0, 1, num_points)
|
20 |
-
theta = 2 * np.pi * num_turns * indices
|
21 |
-
radius = indices
|
22 |
-
|
23 |
-
x = radius * np.cos(theta)
|
24 |
-
y = radius * np.sin(theta)
|
25 |
-
|
26 |
-
df = pd.DataFrame({
|
27 |
-
"x": x,
|
28 |
-
"y": y,
|
29 |
-
"idx": indices,
|
30 |
-
"rand": np.random.randn(num_points),
|
31 |
-
})
|
32 |
-
|
33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
34 |
-
.mark_point(filled=True)
|
35 |
-
.encode(
|
36 |
-
x=alt.X("x", axis=None),
|
37 |
-
y=alt.Y("y", axis=None),
|
38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
40 |
-
))
|
|
|
1 |
+
import os
|
2 |
+
os.environ["STREAMLIT_WATCHER_TYPE"] = "none"
|
3 |
+
|
4 |
+
import torch
|
5 |
+
if hasattr(torch, "classes"):
|
6 |
+
try:
|
7 |
+
torch.classes.__path__ = []
|
8 |
+
except Exception:
|
9 |
+
pass
|
10 |
+
|
11 |
import streamlit as st
|
12 |
+
import tempfile
|
13 |
+
import requests
|
14 |
+
import subprocess
|
15 |
+
import torchaudio
|
16 |
+
from speechbrain.pretrained.interfaces import foreign_class
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
# Load model using custom interface
|
21 |
+
@st.cache_resource
|
22 |
+
def load_model():
|
23 |
+
try:
|
24 |
+
os.environ["SPEECHBRAIN_CACHE"] = os.path.join(os.getcwd(), "models")
|
25 |
+
return foreign_class(
|
26 |
+
source="Jzuluaga/accent-id-commonaccent_xlsr-en-english",
|
27 |
+
pymodule_file="custom_interface.py",
|
28 |
+
classname="CustomEncoderWav2vec2Classifier"
|
29 |
+
)
|
30 |
+
except Exception as e:
|
31 |
+
st.error(f"❌ Model failed to load: {e}")
|
32 |
+
raise
|
33 |
+
|
34 |
+
# Download video from a public URL
|
35 |
+
def download_video(url, temp_dir):
|
36 |
+
video_path = os.path.join(temp_dir, "video.mp4")
|
37 |
+
r = requests.get(url, stream=True)
|
38 |
+
with open(video_path, 'wb') as f:
|
39 |
+
for chunk in r.iter_content(chunk_size=1024):
|
40 |
+
f.write(chunk)
|
41 |
+
return video_path
|
42 |
+
|
43 |
+
import imageio_ffmpeg
|
44 |
+
|
45 |
+
def extract_audio(video_path, temp_dir):
|
46 |
+
audio_path = os.path.join(temp_dir, "audio.wav")
|
47 |
+
ffmpeg_path = imageio_ffmpeg.get_ffmpeg_exe() # Get bundled FFmpeg path
|
48 |
+
|
49 |
+
command = [
|
50 |
+
ffmpeg_path,
|
51 |
+
"-y", "-i", video_path,
|
52 |
+
"-vn", "-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1",
|
53 |
+
audio_path
|
54 |
+
]
|
55 |
+
|
56 |
+
try:
|
57 |
+
subprocess.run(command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
58 |
+
except subprocess.CalledProcessError as e:
|
59 |
+
raise RuntimeError(f"FFmpeg failed: {e}")
|
60 |
+
return audio_path
|
61 |
+
|
62 |
+
def classify_accent(audio_path, model):
|
63 |
+
out_prob, score, index, label = model.classify_file(audio_path)
|
64 |
+
return label, score * 100, out_prob
|
65 |
+
|
66 |
+
# Streamlit UI
|
67 |
+
st.set_page_config(page_title="Accent Classifier", layout="centered")
|
68 |
+
st.title("English Accent Detection")
|
69 |
+
|
70 |
+
st.markdown("Paste a link or upload a video to analyze the speaker's English accent.")
|
71 |
+
|
72 |
+
video_url = st.text_input("Paste a direct link to a video (MP4 URL)")
|
73 |
+
st.markdown("**OR**")
|
74 |
+
uploaded_file = st.file_uploader("Upload a video file (MP4 format)", type=["mp4"])
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
if uploaded_file or video_url:
|
79 |
+
with st.spinner("Processing video..."):
|
80 |
+
try:
|
81 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
82 |
+
# Get video path from upload or URL
|
83 |
+
if uploaded_file:
|
84 |
+
video_path = os.path.join(temp_dir, uploaded_file.name)
|
85 |
+
with open(video_path, 'wb') as f:
|
86 |
+
f.write(uploaded_file.read())
|
87 |
+
else:
|
88 |
+
video_path = download_video(video_url, temp_dir)
|
89 |
+
|
90 |
+
audio_path = extract_audio(video_path, temp_dir)
|
91 |
+
model = load_model()
|
92 |
+
label, confidence, probs = classify_accent(audio_path, model)
|
93 |
+
|
94 |
+
# Ensure proper formatting
|
95 |
+
label = label if isinstance(label, str) else label[0]
|
96 |
+
st.success(f"Detected Accent: **{label}**")
|
97 |
+
st.info(f"Confidence Score: **{confidence.item():.1f}%**")
|
98 |
|
99 |
+
|
100 |
+
except Exception as e:
|
101 |
+
st.error(f"❌ Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|