From 41c9693d107a20a430f56bdf62df10d2cf3afb08 Mon Sep 17 00:00:00 2001 From: lebaudantoine Date: Wed, 16 Oct 2024 12:14:56 +0200 Subject: [PATCH] wip handle if file doesn't exist --- src/backend/core/api/demo.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/backend/core/api/demo.py b/src/backend/core/api/demo.py index d9ea2021..1703b125 100644 --- a/src/backend/core/api/demo.py +++ b/src/backend/core/api/demo.py @@ -79,21 +79,30 @@ def minio_webhook(request): secret_key=settings.AWS_S3_SECRET_ACCESS_KEY, ) + temp_file_path = None + try: logger.info('downloading file %s', filename) - audio_file_stream = client.get_object(settings.AWS_STORAGE_BUCKET_NAME, object_name=filename) + + try: + audio_file_stream = client.get_object(settings.AWS_STORAGE_BUCKET_NAME, object_name=filename) - with tempfile.NamedTemporaryFile(delete=False, suffix='.ogg') as temp_audio_file: - for data in audio_file_stream.stream(32*1024): - temp_audio_file.write(data) + with tempfile.NamedTemporaryFile(delete=False, suffix='.ogg') as temp_audio_file: + for data in audio_file_stream.stream(32*1024): + temp_audio_file.write(data) - temp_file_path = temp_audio_file.name - logger.info('Temporary file created at %s', temp_file_path) + temp_file_path = temp_audio_file.name + logger.info('Temporary file created at %s', temp_file_path) - audio_file_stream.close() - audio_file_stream.release_conn() + audio_file_stream.close() + audio_file_stream.release_conn() - if settings.OPENAI_ENABLE: + except Exception as e: + + logger.error("An error occurred while accessing the object: %s", str(e)) + return Response("Error accessing file", status=500) + + if settings.OPENAI_ENABLE and temp_file_path: openai_client = openai.OpenAI( api_key=settings.OPENAI_API_KEY,