Skip to content

Commit

Permalink
Add API authentication to YOLOv4 backend
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoelTT committed Jan 2, 2025
1 parent cd5c6f7 commit 927aadc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 11 additions & 1 deletion app/api/model_control/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from .serializers import InferenceSerializer, ModelWeightsSerializer
from model_control.model_utils import (
encoded_jwt,
get_deploy_cache,
stream_response_from_external_api,
health_check,
Expand Down Expand Up @@ -123,7 +124,16 @@ def post(self, request, *args, **kwargs):
)
byte_im = buf.getvalue()
file = {"file": byte_im}
inference_data = requests.post(internal_url, files=file, timeout=5)
try:
headers = {"Authorization": f"Bearer {encoded_jwt}"}
inference_data = requests.post(internal_url, files=file, headers=headers, timeout=5)
inference_data.raise_for_status()
except requests.exceptions.HTTPError as http_err:
if inference_data.status_code == status.HTTP_401_UNAUTHORIZED:
return Response(status=status.HTTP_401_UNAUTHORIZED)
else:
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)

return Response(inference_data.json(), status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
6 changes: 3 additions & 3 deletions app/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
- "8000:8000"
# command: bash
# dev server can be used for breakpoint debugging, does not support streaming
# command: ./manage.py runserver 0.0.0.0:8000
# command: python ./manage.py runserver 0.0.0.0:8000
# gunicorn is used from production, supports streaming

command: gunicorn --workers 3 --bind 0.0.0.0:8000 --preload --timeout 1200 api.wsgi:application
Expand Down Expand Up @@ -52,7 +52,7 @@ services:
# On first application load resources for transformers/etc
# are downloaded. The UI should not start until these resources
# have been downloaded. Adjust timeout if on a very slow connection
test: ["CMD", "curl", "-f", "http://localhost:8000/up/"]
test: [ "CMD", "curl", "-f", "http://localhost:8000/up/" ]
timeout: 120s
interval: 10s
retries: 5
Expand Down Expand Up @@ -93,7 +93,7 @@ services:
- "8111:8111"
healthcheck:
# Adjust below to match your container port
test: ["CMD", "curl", "-f", "http://localhost:8111/api/v1/heartbeat"]
test: [ "CMD", "curl", "-f", "http://localhost:8111/api/v1/heartbeat" ]
interval: 10s
timeout: 10s
retries: 3
Expand Down

0 comments on commit 927aadc

Please sign in to comment.