EdgarDataScientist commited on
Commit
9e56648
·
verified ·
1 Parent(s): cc81f61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -36
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import gradio as gr
2
  import os
3
  import tempfile
4
  import requests
@@ -8,7 +8,7 @@ import matplotlib.pyplot as plt
8
  import torchaudio
9
  import torch
10
 
11
- # --- Load SpeechBrain ---
12
  try:
13
  from speechbrain.inference import EncoderClassifier
14
  speechbrain_classifier = EncoderClassifier.from_hparams(
@@ -17,10 +17,9 @@ try:
17
  )
18
  SPEECHBRAIN_LOADED = True
19
  except Exception as e:
20
- print(f"Error loading SpeechBrain model: {e}. Simulated mode ON.")
21
  SPEECHBRAIN_LOADED = False
22
 
23
- # --- Accent Analyzer Class ---
24
  class AccentAnalyzer:
25
  def __init__(self):
26
  self.accent_profiles = {
@@ -91,10 +90,9 @@ class AccentAnalyzer:
91
  "all_scores": scores
92
  }
93
  except Exception as e:
94
- print(f"Fallback to simulation: {e}")
95
  return self._simulate_accent_classification(audio_path)
96
 
97
- # --- Download & Extract Audio ---
98
  def download_and_extract_audio(url):
99
  temp_dir = tempfile.mkdtemp()
100
  video_path = os.path.join(temp_dir, "video.mp4")
@@ -102,7 +100,7 @@ def download_and_extract_audio(url):
102
 
103
  if "youtube.com" in url or "youtu.be" in url:
104
  from pytubefix import YouTube
105
- yt = YouTube(url, use_oauth=True, allow_oauth_cache=True, client="WEB")
106
  stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
107
  stream.download(output_path=temp_dir, filename="video.mp4")
108
  else:
@@ -112,41 +110,33 @@ def download_and_extract_audio(url):
112
  for chunk in r.iter_content(chunk_size=8192):
113
  f.write(chunk)
114
 
115
- # Extract audio using ffmpeg
116
  subprocess.run([
117
  "ffmpeg", "-i", video_path, "-ar", "16000", "-ac", "1", "-f", "wav", audio_path, "-y"
118
  ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
119
 
120
  return audio_path
121
 
122
- # --- Gradio Function ---
123
- def analyze_from_url_gradio(url):
124
- if not url:
125
- return "Please enter a URL.", plt.figure()
126
- try:
127
- audio_path = download_and_extract_audio(url)
128
- analyzer = AccentAnalyzer()
129
- results = analyzer.analyze_accent(audio_path)
130
-
131
- labels, values = zip(*results["all_scores"].items())
132
- fig, ax = plt.subplots()
133
- ax.bar(labels, values)
134
- ax.set_ylabel('Confidence (%)')
135
- ax.set_title('Accent/Language Confidence')
136
- plt.xticks(rotation=45)
137
- plt.tight_layout()
138
 
139
- return results["explanation"], fig
140
- except Exception as e:
141
- return f"Error: {e}", plt.figure()
 
 
 
 
 
142
 
143
- # --- Gradio Interface ---
144
- iface = gr.Interface(
145
- fn=analyze_from_url_gradio,
146
- inputs=gr.Textbox(label="Enter Public Video URL (YouTube or MP4)"),
147
- outputs=[gr.Textbox(label="Result"), gr.Plot(label="Confidence Plot")],
148
- title="English Accent or Language Analyzer",
149
- description="Paste a public video URL. The system will detect the accent or language spoken using SpeechBrain or simulation."
150
- )
151
 
152
- iface.launch()
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
  import os
3
  import tempfile
4
  import requests
 
8
  import torchaudio
9
  import torch
10
 
11
+ # Load SpeechBrain
12
  try:
13
  from speechbrain.inference import EncoderClassifier
14
  speechbrain_classifier = EncoderClassifier.from_hparams(
 
17
  )
18
  SPEECHBRAIN_LOADED = True
19
  except Exception as e:
20
+ st.warning(f"Error loading SpeechBrain model: {e}. Running in simulation mode.")
21
  SPEECHBRAIN_LOADED = False
22
 
 
23
  class AccentAnalyzer:
24
  def __init__(self):
25
  self.accent_profiles = {
 
90
  "all_scores": scores
91
  }
92
  except Exception as e:
93
+ st.warning(f"Fallback to simulation: {e}")
94
  return self._simulate_accent_classification(audio_path)
95
 
 
96
  def download_and_extract_audio(url):
97
  temp_dir = tempfile.mkdtemp()
98
  video_path = os.path.join(temp_dir, "video.mp4")
 
100
 
101
  if "youtube.com" in url or "youtu.be" in url:
102
  from pytubefix import YouTube
103
+ yt = YouTube(url, use_po_token=True, client="WEB")
104
  stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
105
  stream.download(output_path=temp_dir, filename="video.mp4")
106
  else:
 
110
  for chunk in r.iter_content(chunk_size=8192):
111
  f.write(chunk)
112
 
 
113
  subprocess.run([
114
  "ffmpeg", "-i", video_path, "-ar", "16000", "-ac", "1", "-f", "wav", audio_path, "-y"
115
  ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
116
 
117
  return audio_path
118
 
119
+ # Streamlit UI
120
+ st.title("English Accent or Language Analyzer")
121
+ url = st.text_input("Enter Public Video URL (YouTube or MP4)")
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
+ if st.button("Analyze"):
124
+ if not url:
125
+ st.error("Please enter a URL.")
126
+ else:
127
+ try:
128
+ audio_path = download_and_extract_audio(url)
129
+ analyzer = AccentAnalyzer()
130
+ results = analyzer.analyze_accent(audio_path)
131
 
132
+ st.markdown(results["explanation"])
 
 
 
 
 
 
 
133
 
134
+ labels, values = zip(*results["all_scores"].items())
135
+ fig, ax = plt.subplots()
136
+ ax.bar(labels, values)
137
+ ax.set_ylabel('Confidence (%)')
138
+ ax.set_title('Accent/Language Confidence')
139
+ plt.xticks(rotation=45)
140
+ st.pyplot(fig)
141
+ except Exception as e:
142
+ st.error(f"Error: {e}")