Spaces:
Sleeping
Sleeping
Michael Hu
commited on
Commit
Β·
43a2be0
1
Parent(s):
1990005
app is blank page, add exception handling
Browse files
app.py
CHANGED
|
@@ -223,11 +223,16 @@ def get_supported_configurations() -> dict:
|
|
| 223 |
dict: Supported configurations
|
| 224 |
"""
|
| 225 |
try:
|
|
|
|
| 226 |
container = get_global_container()
|
|
|
|
| 227 |
audio_service = container.resolve(AudioProcessingApplicationService)
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
| 229 |
except Exception as e:
|
| 230 |
-
logger.
|
| 231 |
# Return fallback configurations
|
| 232 |
return {
|
| 233 |
'asr_models': ['whisper-small', 'parakeet'],
|
|
@@ -262,112 +267,132 @@ def main():
|
|
| 262 |
"""Main application workflow"""
|
| 263 |
logger.info("Starting application")
|
| 264 |
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
|
|
|
| 270 |
|
| 271 |
-
|
| 272 |
-
|
| 273 |
|
| 274 |
-
|
| 275 |
-
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
-
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
"Speech Speed",
|
| 300 |
-
config['speed_range']['min'],
|
| 301 |
-
config['speed_range']['max'],
|
| 302 |
-
1.0,
|
| 303 |
-
0.1
|
| 304 |
-
)
|
| 305 |
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
"Chinese (Mandarin)": "zh",
|
| 317 |
-
"Spanish": "es",
|
| 318 |
-
"French": "fr",
|
| 319 |
-
"German": "de",
|
| 320 |
-
"English": "en"
|
| 321 |
-
}
|
| 322 |
-
|
| 323 |
-
selected_language_display = st.selectbox(
|
| 324 |
-
"Target Language",
|
| 325 |
-
list(language_options.keys()),
|
| 326 |
-
index=0,
|
| 327 |
-
help="Select the target language for translation"
|
| 328 |
-
)
|
| 329 |
-
target_language = language_options[selected_language_display]
|
| 330 |
-
|
| 331 |
-
# File upload
|
| 332 |
-
uploaded_file = st.file_uploader(
|
| 333 |
-
f"Select Audio File ({', '.join(config['audio_formats']).upper()})",
|
| 334 |
-
type=config['audio_formats'],
|
| 335 |
-
accept_multiple_files=False,
|
| 336 |
-
help=f"Maximum file size: {config['max_file_size_mb']}MB"
|
| 337 |
-
)
|
| 338 |
|
| 339 |
-
|
| 340 |
-
|
|
|
|
| 341 |
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
audio_upload = create_audio_upload_dto(uploaded_file)
|
| 345 |
-
|
| 346 |
-
# Display file information
|
| 347 |
-
st.info(f"π **File:** {audio_upload.filename} ({audio_upload.size / 1024:.1f} KB)")
|
| 348 |
-
|
| 349 |
-
# Process button
|
| 350 |
-
if st.button("π Process Audio", type="primary"):
|
| 351 |
-
# Process the audio
|
| 352 |
-
result = handle_file_processing(
|
| 353 |
-
audio_upload=audio_upload,
|
| 354 |
-
asr_model=asr_model,
|
| 355 |
-
target_language=target_language,
|
| 356 |
-
voice=selected_voice,
|
| 357 |
-
speed=speed,
|
| 358 |
-
source_language="en" # Assume English source for now
|
| 359 |
-
)
|
| 360 |
-
|
| 361 |
-
# Store result in session state
|
| 362 |
-
st.session_state.processing_result = result
|
| 363 |
-
|
| 364 |
-
# Display results if available
|
| 365 |
-
if st.session_state.processing_result:
|
| 366 |
-
render_results(st.session_state.processing_result)
|
| 367 |
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
|
| 372 |
if __name__ == "__main__":
|
| 373 |
main()
|
|
|
|
| 223 |
dict: Supported configurations
|
| 224 |
"""
|
| 225 |
try:
|
| 226 |
+
logger.info("Getting global container...")
|
| 227 |
container = get_global_container()
|
| 228 |
+
logger.info("Resolving AudioProcessingApplicationService...")
|
| 229 |
audio_service = container.resolve(AudioProcessingApplicationService)
|
| 230 |
+
logger.info("Getting supported configurations from service...")
|
| 231 |
+
config = audio_service.get_supported_configurations()
|
| 232 |
+
logger.info(f"Retrieved configurations: {config}")
|
| 233 |
+
return config
|
| 234 |
except Exception as e:
|
| 235 |
+
logger.error(f"Failed to get configurations: {e}", exc_info=True)
|
| 236 |
# Return fallback configurations
|
| 237 |
return {
|
| 238 |
'asr_models': ['whisper-small', 'parakeet'],
|
|
|
|
| 267 |
"""Main application workflow"""
|
| 268 |
logger.info("Starting application")
|
| 269 |
|
| 270 |
+
try:
|
| 271 |
+
# Configure page
|
| 272 |
+
configure_page()
|
| 273 |
+
|
| 274 |
+
# Initialize session state first
|
| 275 |
+
initialize_session_state()
|
| 276 |
|
| 277 |
+
# Initialize application
|
| 278 |
+
initialize_application()
|
| 279 |
|
| 280 |
+
st.title("π§ High-Quality Audio Translation System")
|
| 281 |
+
st.markdown("Upload English Audio β Get Chinese Speech Output")
|
| 282 |
|
| 283 |
+
# Get supported configurations with error handling
|
| 284 |
+
try:
|
| 285 |
+
config = get_supported_configurations()
|
| 286 |
+
logger.info("Successfully retrieved configurations")
|
| 287 |
+
except Exception as e:
|
| 288 |
+
logger.error(f"Failed to get configurations: {e}")
|
| 289 |
+
st.error(f"Configuration error: {str(e)}")
|
| 290 |
+
# Use fallback configuration
|
| 291 |
+
config = {
|
| 292 |
+
'asr_models': ['whisper-small', 'parakeet'],
|
| 293 |
+
'voices': ['kokoro', 'dia', 'cosyvoice2', 'dummy'],
|
| 294 |
+
'languages': ['en', 'zh', 'es', 'fr', 'de'],
|
| 295 |
+
'audio_formats': ['wav', 'mp3'],
|
| 296 |
+
'max_file_size_mb': 100,
|
| 297 |
+
'speed_range': {'min': 0.5, 'max': 2.0}
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
# Voice selection in sidebar
|
| 301 |
+
st.sidebar.header("TTS Settings")
|
| 302 |
+
|
| 303 |
+
# Map voice display names to internal IDs
|
| 304 |
+
voice_options = {
|
| 305 |
+
"Kokoro": "kokoro",
|
| 306 |
+
"Dia": "dia",
|
| 307 |
+
"CosyVoice2": "cosyvoice2",
|
| 308 |
+
"Dummy (Test)": "dummy"
|
| 309 |
+
}
|
| 310 |
|
| 311 |
+
selected_voice_display = st.sidebar.selectbox(
|
| 312 |
+
"Select Voice",
|
| 313 |
+
list(voice_options.keys()),
|
| 314 |
+
index=0
|
| 315 |
+
)
|
| 316 |
+
selected_voice = voice_options[selected_voice_display]
|
| 317 |
+
|
| 318 |
+
speed = st.sidebar.slider(
|
| 319 |
+
"Speech Speed",
|
| 320 |
+
config['speed_range']['min'],
|
| 321 |
+
config['speed_range']['max'],
|
| 322 |
+
1.0,
|
| 323 |
+
0.1
|
| 324 |
+
)
|
| 325 |
|
| 326 |
+
# Model selection
|
| 327 |
+
asr_model = st.selectbox(
|
| 328 |
+
"Select Speech Recognition Model",
|
| 329 |
+
options=config['asr_models'],
|
| 330 |
+
index=0,
|
| 331 |
+
help="Choose the ASR model for speech recognition"
|
| 332 |
+
)
|
| 333 |
|
| 334 |
+
# Language selection
|
| 335 |
+
language_options = {
|
| 336 |
+
"Chinese (Mandarin)": "zh",
|
| 337 |
+
"Spanish": "es",
|
| 338 |
+
"French": "fr",
|
| 339 |
+
"German": "de",
|
| 340 |
+
"English": "en"
|
| 341 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
+
selected_language_display = st.selectbox(
|
| 344 |
+
"Target Language",
|
| 345 |
+
list(language_options.keys()),
|
| 346 |
+
index=0,
|
| 347 |
+
help="Select the target language for translation"
|
| 348 |
+
)
|
| 349 |
+
target_language = language_options[selected_language_display]
|
| 350 |
+
|
| 351 |
+
# File upload
|
| 352 |
+
uploaded_file = st.file_uploader(
|
| 353 |
+
f"Select Audio File ({', '.join(config['audio_formats']).upper()})",
|
| 354 |
+
type=config['audio_formats'],
|
| 355 |
+
accept_multiple_files=False,
|
| 356 |
+
help=f"Maximum file size: {config['max_file_size_mb']}MB"
|
| 357 |
+
)
|
| 358 |
|
| 359 |
+
if uploaded_file:
|
| 360 |
+
logger.info(f"File uploaded: {uploaded_file.name}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
|
| 362 |
+
try:
|
| 363 |
+
# Create audio upload DTO
|
| 364 |
+
audio_upload = create_audio_upload_dto(uploaded_file)
|
| 365 |
|
| 366 |
+
# Display file information
|
| 367 |
+
st.info(f"π **File:** {audio_upload.filename} ({audio_upload.size / 1024:.1f} KB)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
+
# Process button
|
| 370 |
+
if st.button("π Process Audio", type="primary"):
|
| 371 |
+
# Process the audio
|
| 372 |
+
result = handle_file_processing(
|
| 373 |
+
audio_upload=audio_upload,
|
| 374 |
+
asr_model=asr_model,
|
| 375 |
+
target_language=target_language,
|
| 376 |
+
voice=selected_voice,
|
| 377 |
+
speed=speed,
|
| 378 |
+
source_language="en" # Assume English source for now
|
| 379 |
+
)
|
| 380 |
+
|
| 381 |
+
# Store result in session state
|
| 382 |
+
st.session_state.processing_result = result
|
| 383 |
+
|
| 384 |
+
# Display results if available
|
| 385 |
+
if st.session_state.processing_result:
|
| 386 |
+
render_results(st.session_state.processing_result)
|
| 387 |
+
|
| 388 |
+
except Exception as e:
|
| 389 |
+
st.error(f"Error processing file: {str(e)}")
|
| 390 |
+
logger.error(f"File processing error: {e}")
|
| 391 |
+
|
| 392 |
+
except Exception as e:
|
| 393 |
+
logger.error(f"Main application error: {str(e)}", exc_info=True)
|
| 394 |
+
st.error(f"Application error: {str(e)}")
|
| 395 |
+
st.exception(e)
|
| 396 |
|
| 397 |
if __name__ == "__main__":
|
| 398 |
main()
|