wip add some logs and allow toggling openai request

This commit is contained in:
lebaudantoine
2024-10-16 12:00:07 +02:00
parent ac183c9eb9
commit e8618099ac
3 changed files with 66 additions and 6 deletions
+48 -6
View File
@@ -4,11 +4,14 @@ from rest_framework.response import Response
from minio import Minio
from django.conf import settings
import openai
import logging
import tempfile
import os
openai_client = openai.OpenAI(
api_key=settings.OPENAI_API_KEY,
)
logger = logging.getLogger(__name__)
# todo - discuss retry policy if the webhook fail
@api_view(["POST"])
@@ -16,6 +19,8 @@ def minio_webhook(request):
data = request.data
logger.info('Minio webhook sent %s', data)
record = data["Records"][0]
s3 = record['s3']
bucket = s3['bucket']
@@ -29,7 +34,8 @@ def minio_webhook(request):
if object['contentType'] != 'audio/ogg':
return Response("Not interested in this file type")
print('file received', filename)
room_id = filename.split("_")[2].split(".")[0]
logger.info('file received %s for room %s', filename, room_id)
client = Minio(
settings.AWS_S3_ENDPOINT_URL,
@@ -37,9 +43,45 @@ def minio_webhook(request):
secret_key=settings.AWS_S3_SECRET_ACCESS_KEY,
)
room_id = filename.split("_")[2].split(".")[0]
try:
logger.info('downloading file %s', filename)
audio_file_stream = client.get_object(settings.AWS_STORAGE_BUCKET_NAME, object_name=filename)
print('room_id', room_id, 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)
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()
if settings.OPENAI_ENABLE:
openai_client = openai.OpenAI(
api_key=settings.OPENAI_API_KEY,
)
with open(temp_file_path, "rb") as audio_file:
logger.info('Querying transcription …')
transcript = openai_client.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
logger.info(transcript)
except Exception as e:
logger.error("An error occurred: %s", str(e))
raise
finally:
if temp_file_path and os.path.exists(temp_file_path):
os.remove(temp_file_path)
logger.info("Temporary file %s has been deleted.", temp_file_path)
return Response("")
+17
View File
@@ -408,6 +408,9 @@ class Base(Configuration):
OPENAI_API_KEY = values.Value(
None, environ_name="OPENAI_API_KEY", environ_prefix=None
)
OPENAI_ENABLE = values.BooleanValue(
True, environ_name="OPENAI_ENABLE", environ_prefix=None
)
# pylint: disable=invalid-name
@property
@@ -552,6 +555,20 @@ class Production(Base):
ALLOWED_HOSTS=["foo.com", "foo.fr"]
"""
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
},
"root": {
"handlers": ["console"],
"level": "INFO",
},
}
# Security
ALLOWED_HOSTS = [
*values.ListValue([], environ_name="ALLOWED_HOSTS"),
@@ -118,6 +118,7 @@ backend:
secretKeyRef:
name: backend
key: OPENAI_API_KEY
OPENAI_ENABLE: False
createsuperuser:
command: