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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import tempfile
|
| 3 |
+
import os
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from accent_detector import AccentDetector
|
| 7 |
+
from logger import get_logger
|
| 8 |
|
| 9 |
+
|
| 10 |
+
# Get logger for this module
|
| 11 |
+
logger = get_logger(__name__)
|
| 12 |
+
|
| 13 |
+
def run_tests():
|
| 14 |
+
"""Check for test files and log test status"""
|
| 15 |
+
test_dir = Path(__file__).parent / 'test'
|
| 16 |
+
test_files = [
|
| 17 |
+
'test_accent_detector.py',
|
| 18 |
+
'test_model.py',
|
| 19 |
+
'test_video_downloader.py'
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
if not test_dir.exists():
|
| 23 |
+
logger.warning("Test directory not found")
|
| 24 |
+
return {'passed': 0, 'failed': 1, 'errors': ['Test directory not found']}
|
| 25 |
+
|
| 26 |
+
existing_tests = []
|
| 27 |
+
for test_file in test_files:
|
| 28 |
+
test_path = test_dir / test_file
|
| 29 |
+
if test_path.exists():
|
| 30 |
+
existing_tests.append(test_file)
|
| 31 |
+
|
| 32 |
+
if existing_tests:
|
| 33 |
+
logger.info(f"Found {len(existing_tests)} test files: {', '.join(existing_tests)}")
|
| 34 |
+
logger.info("Tests are available but running in background to avoid Streamlit conflicts")
|
| 35 |
+
return {'passed': len(existing_tests), 'failed': 0, 'errors': []}
|
| 36 |
+
else:
|
| 37 |
+
logger.warning("No test files found")
|
| 38 |
+
return {'passed': 0, 'failed': 1, 'errors': ['No test files found']}
|
| 39 |
+
|
| 40 |
+
# Run tests at startup
|
| 41 |
+
if 'tests_run' not in st.session_state:
|
| 42 |
+
st.session_state.tests_run = False
|
| 43 |
+
|
| 44 |
+
if not st.session_state.tests_run:
|
| 45 |
+
test_results = run_tests()
|
| 46 |
+
st.session_state.test_results = test_results
|
| 47 |
+
st.session_state.tests_run = True
|
| 48 |
+
|
| 49 |
+
# Log test results to console
|
| 50 |
+
if test_results['failed'] == 0:
|
| 51 |
+
logger.info(f"All tests passed! ({test_results['passed']} tests)")
|
| 52 |
+
else:
|
| 53 |
+
logger.warning(f"Tests completed: {test_results['passed']} passed, {test_results['failed']} failed")
|
| 54 |
+
if test_results['errors']:
|
| 55 |
+
for error in test_results['errors']:
|
| 56 |
+
logger.error(f"Test failure: {error}")
|
| 57 |
+
|
| 58 |
+
# Initialize session state
|
| 59 |
+
if 'detector' not in st.session_state:
|
| 60 |
+
st.session_state.detector = None
|
| 61 |
+
if 'results' not in st.session_state:
|
| 62 |
+
st.session_state.results = None
|
| 63 |
+
if 'processing' not in st.session_state:
|
| 64 |
+
st.session_state.processing = False
|
| 65 |
+
|
| 66 |
+
@st.cache_resource
|
| 67 |
+
def load_accent_detector():
|
| 68 |
+
"""Load and cache the accent detector model"""
|
| 69 |
+
try:
|
| 70 |
+
detector = AccentDetector()
|
| 71 |
+
return detector
|
| 72 |
+
except Exception as e:
|
| 73 |
+
st.error(f"Error loading model: {e}")
|
| 74 |
+
logger.error(f"Error loading model: {e}")
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
+
# Load model at startup
|
| 78 |
+
if st.session_state.detector is None:
|
| 79 |
+
with st.spinner("Loading accent detection model..."):
|
| 80 |
+
st.session_state.detector = load_accent_detector()
|
| 81 |
+
if st.session_state.detector:
|
| 82 |
+
st.success("Model loaded successfully!")
|
| 83 |
+
else:
|
| 84 |
+
st.error("Failed to load the accent detection model. Please refresh the page.")
|
| 85 |
+
st.stop()
|
| 86 |
+
|
| 87 |
+
def process_url(url):
|
| 88 |
+
"""Process a video URL for accent detection"""
|
| 89 |
+
try:
|
| 90 |
+
with st.spinner("Downloading video and analyzing accent..."):
|
| 91 |
+
result = st.session_state.detector.analyze_video_url(url)
|
| 92 |
+
st.session_state.results = result
|
| 93 |
+
return True
|
| 94 |
+
except Exception as e:
|
| 95 |
+
st.error(f"Error processing URL: {e}")
|
| 96 |
+
logger.error(f"Error processing URL: {e}")
|
| 97 |
+
return False
|
| 98 |
+
|
| 99 |
+
def process_audio_file(uploaded_file):
|
| 100 |
+
"""Process an uploaded audio file for accent detection"""
|
| 101 |
+
try:
|
| 102 |
+
# Save uploaded file to temporary directory
|
| 103 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=f".{uploaded_file.name.split('.')[-1]}") as tmp_file:
|
| 104 |
+
tmp_file.write(uploaded_file.getvalue())
|
| 105 |
+
tmp_file_path = tmp_file.name
|
| 106 |
+
|
| 107 |
+
with st.spinner("Analyzing accent from uploaded file..."):
|
| 108 |
+
result = st.session_state.detector.predict_accent(tmp_file_path)
|
| 109 |
+
result['audio_path'] = uploaded_file.name
|
| 110 |
+
result['source'] = 'uploaded_file'
|
| 111 |
+
st.session_state.results = result
|
| 112 |
+
|
| 113 |
+
# Clean up temporary file
|
| 114 |
+
os.unlink(tmp_file_path)
|
| 115 |
+
return True
|
| 116 |
+
|
| 117 |
+
except Exception as e:
|
| 118 |
+
st.error(f"Error processing audio file: {e}")
|
| 119 |
+
logger.error(f"Error processing audio file: {e}")
|
| 120 |
+
# Clean up temporary file if it exists
|
| 121 |
+
if 'tmp_file_path' in locals():
|
| 122 |
+
try:
|
| 123 |
+
os.unlink(tmp_file_path)
|
| 124 |
+
except:
|
| 125 |
+
pass
|
| 126 |
+
return False
|
| 127 |
+
|
| 128 |
+
def display_results():
|
| 129 |
+
"""Display the accent detection results"""
|
| 130 |
+
if st.session_state.results is None:
|
| 131 |
+
return
|
| 132 |
+
|
| 133 |
+
result = st.session_state.results
|
| 134 |
+
|
| 135 |
+
st.markdown("---")
|
| 136 |
+
st.header("Results")
|
| 137 |
+
|
| 138 |
+
# Main result
|
| 139 |
+
col1, col2 = st.columns(2)
|
| 140 |
+
|
| 141 |
+
with col1:
|
| 142 |
+
st.metric(
|
| 143 |
+
label="Predicted Accent",
|
| 144 |
+
value=result['predicted_accent'],
|
| 145 |
+
delta=None
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
with col2:
|
| 149 |
+
confidence_pct = result['confidence'] * 100
|
| 150 |
+
st.metric(
|
| 151 |
+
label="Confidence",
|
| 152 |
+
value=f"{confidence_pct:.1f}%",
|
| 153 |
+
delta=None
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
# File information
|
| 157 |
+
st.subheader("Source Information")
|
| 158 |
+
if 'source_url' in result:
|
| 159 |
+
st.write(f"**Source URL:** {result['source_url']}")
|
| 160 |
+
elif 'source' in result and result['source'] == 'uploaded_file':
|
| 161 |
+
st.write(f"**Uploaded file:** {result.get('audio_path', 'N/A')}")
|
| 162 |
+
|
| 163 |
+
# All predictions (if available)
|
| 164 |
+
if 'all_predictions' in result and result['all_predictions']:
|
| 165 |
+
st.subheader("π All Accent Predictions")
|
| 166 |
+
|
| 167 |
+
# Create a DataFrame for better display
|
| 168 |
+
predictions_data = []
|
| 169 |
+
for accent, probability in result['all_predictions'].items():
|
| 170 |
+
predictions_data.append({
|
| 171 |
+
'Accent': accent,
|
| 172 |
+
'Probability': probability,
|
| 173 |
+
'Percentage': f"{probability * 100:.1f}%"
|
| 174 |
+
})
|
| 175 |
+
|
| 176 |
+
# Sort by probability
|
| 177 |
+
predictions_df = pd.DataFrame(predictions_data)
|
| 178 |
+
predictions_df = predictions_df.sort_values('Probability', ascending=False)
|
| 179 |
+
|
| 180 |
+
# Display top predictions in columns
|
| 181 |
+
col1, col2 = st.columns(2)
|
| 182 |
+
|
| 183 |
+
with col1:
|
| 184 |
+
st.write("**Top 5 Predictions:**")
|
| 185 |
+
top_5 = predictions_df.head(5)
|
| 186 |
+
for _, row in top_5.iterrows():
|
| 187 |
+
is_top = row['Accent'] == result['predicted_accent']
|
| 188 |
+
if is_top:
|
| 189 |
+
st.write(f"π **{row['Accent']}**: {row['Percentage']}")
|
| 190 |
+
else:
|
| 191 |
+
st.write(f"β’ {row['Accent']}: {row['Percentage']}")
|
| 192 |
+
|
| 193 |
+
with col2:
|
| 194 |
+
# Create a bar chart
|
| 195 |
+
st.write("**Probability Distribution:**")
|
| 196 |
+
chart_data = predictions_df.head(10) # Show top 10 in chart
|
| 197 |
+
st.bar_chart(
|
| 198 |
+
data=chart_data.set_index('Accent')['Probability'],
|
| 199 |
+
height=300
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
def main():
|
| 203 |
+
"""Main Streamlit app"""
|
| 204 |
+
|
| 205 |
+
# Title and description
|
| 206 |
+
st.title("Accent Detector")
|
| 207 |
+
st.markdown("""
|
| 208 |
+
Welcome to the Accent Detector! Upload an audio file or provide a video URL to analyze the speaker's accent.
|
| 209 |
+
|
| 210 |
+
**Supported formats:**
|
| 211 |
+
- Audio files: WAV, MP3, FLAC, etc.
|
| 212 |
+
- Video URLs: YouTube, and other video platforms
|
| 213 |
+
""")
|
| 214 |
+
|
| 215 |
+
# Input methods
|
| 216 |
+
st.header("π₯ Input Method")
|
| 217 |
+
|
| 218 |
+
# Create tabs for different input methods
|
| 219 |
+
tab1, tab2 = st.tabs(["π Video URL", "π Upload Audio File"])
|
| 220 |
+
|
| 221 |
+
url_input = None
|
| 222 |
+
uploaded_file = None
|
| 223 |
+
|
| 224 |
+
with tab1:
|
| 225 |
+
st.subheader("Enter Video URL")
|
| 226 |
+
url_input = st.text_input(
|
| 227 |
+
"Paste a video URL (YouTube, etc.):",
|
| 228 |
+
placeholder="https://youtu.be/example",
|
| 229 |
+
help="Supported platforms include YouTube and other major video sites"
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
if url_input:
|
| 233 |
+
st.info(f"URL entered: {url_input}")
|
| 234 |
+
|
| 235 |
+
with tab2:
|
| 236 |
+
st.subheader("Upload Audio File")
|
| 237 |
+
uploaded_file = st.file_uploader(
|
| 238 |
+
"Choose an audio file",
|
| 239 |
+
type=['wav', 'mp3', 'flac', 'm4a', 'ogg'],
|
| 240 |
+
help="Upload an audio file to analyze the speaker's accent"
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
if uploaded_file is not None:
|
| 244 |
+
st.info(f"File uploaded: {uploaded_file.name} ({uploaded_file.size} bytes)")
|
| 245 |
+
|
| 246 |
+
# Accept button
|
| 247 |
+
st.markdown("---")
|
| 248 |
+
|
| 249 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
| 250 |
+
|
| 251 |
+
with col2:
|
| 252 |
+
accept_button = st.button(
|
| 253 |
+
"Analyze Accent",
|
| 254 |
+
type="primary",
|
| 255 |
+
disabled=not (url_input or uploaded_file) or st.session_state.processing,
|
| 256 |
+
use_container_width=True
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
# Process input when Accept button is clicked
|
| 260 |
+
if accept_button and not st.session_state.processing:
|
| 261 |
+
st.session_state.processing = True
|
| 262 |
+
|
| 263 |
+
if url_input:
|
| 264 |
+
success = process_url(url_input)
|
| 265 |
+
elif uploaded_file:
|
| 266 |
+
success = process_audio_file(uploaded_file)
|
| 267 |
+
else:
|
| 268 |
+
st.error("Please provide either a URL or upload an audio file.")
|
| 269 |
+
success = False
|
| 270 |
+
|
| 271 |
+
st.session_state.processing = False
|
| 272 |
+
|
| 273 |
+
if success:
|
| 274 |
+
st.rerun()
|
| 275 |
+
|
| 276 |
+
# Display results
|
| 277 |
+
display_results()
|
| 278 |
+
|
| 279 |
+
# Footer
|
| 280 |
+
st.markdown("---")
|
| 281 |
+
# Clear results button in main content
|
| 282 |
+
if st.session_state.results is not None:
|
| 283 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
| 284 |
+
with col2:
|
| 285 |
+
if st.button("ποΈ Clear Results", type="secondary"):
|
| 286 |
+
st.session_state.results = None
|
| 287 |
+
st.rerun()
|
| 288 |
+
|
| 289 |
+
if __name__ == "__main__":
|
| 290 |
+
main()
|
| 291 |
+
|
| 292 |
+
#Test
|