From 3e75dc48556a43285f317720a8c852400cb08908 Mon Sep 17 00:00:00 2001 From: Elite Encoder Date: Wed, 3 Jul 2024 12:05:51 -0400 Subject: [PATCH] Improve error handling in pipeline --- runner/app/routes/speech_to_text.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/runner/app/routes/speech_to_text.py b/runner/app/routes/speech_to_text.py index ba9101c3..456b53c3 100644 --- a/runner/app/routes/speech_to_text.py +++ b/runner/app/routes/speech_to_text.py @@ -61,12 +61,22 @@ async def speech_to_text( audio.file.write(converted_bytes) audio.file.seek(0) logger.info("Converted m4a file to mp3") + except Exception as e: + return JSONResponse( + status_code=400, + content=http_error(f"Error processing audio file: {str(e)}"), + ) + try: return pipeline( audio=audio.file.read(), ) except Exception as e: + status_code = 500 + if "Soundfile is either not in the correct format or is malformed" in str(e): + status_code = 400 + return JSONResponse( - status_code=400, + status_code=status_code, content=http_error(f"Error processing audio file: {str(e)}"), )