Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JWT API key authentication to YOLOv4 #65

Merged
merged 3 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add authentication header to locust testing and add API demonstration…
… test
  • Loading branch information
bgoelTT committed Dec 31, 2024
commit 297cdec8e447bde040626f903c78a094c8795220
1 change: 1 addition & 0 deletions tt-metal-yolov4/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pillow==10.3.0
locust==2.25.0
pytest==7.2.2
22 changes: 6 additions & 16 deletions tt-metal-yolov4/tests/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,16 @@
#
# SPDX-FileCopyrightText: © 2024 Tenstorrent AI ULC

import io
import requests
from PIL import Image
from locust import HttpUser, task
from utils import get_auth_header, sample_file

# Save image as JPEG in-memory for load testing
# Load sample image
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
pil_image = Image.open(requests.get(url, stream=True).raw)
pil_image = pil_image.resize((320, 320)) # Resize to target dimensions
buf = io.BytesIO()
pil_image.save(
buf,
format="JPEG",
)
byte_im = buf.getvalue()
file = {"file": byte_im}

# load sample file in memory
file = sample_file()


class HelloWorldUser(HttpUser):
@task
def hello_world(self):
self.client.post("/objdetection_v2", files=file)
headers = get_auth_header()
self.client.post("/objdetection_v2", files=file, headers=headers)
36 changes: 36 additions & 0 deletions tt-metal-yolov4/tests/test_inference_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# SPDX-License-Identifier: Apache-2.0
#
# SPDX-FileCopyrightText: © 2024 Tenstorrent AI ULC

from http import HTTPStatus
import os
import pytest
import requests
from utils import get_auth_header, sample_file


DEPLOY_URL = "http://127.0.0.1"
SERVICE_PORT = int(os.getenv("SERVICE_PORT", 7000))
API_BASE_URL = f"{DEPLOY_URL}:{SERVICE_PORT}"
API_URL = f"{API_BASE_URL}/objdetection_v2"
HEALTH_URL = f"{API_BASE_URL}/health"


def test_valid_api_call():
# get sample image file
file = sample_file()
# make request with auth headers
headers = get_auth_header()
response = requests.post(API_URL, files=file, headers=headers)
# perform status and value checking
assert response.status_code == HTTPStatus.OK
assert isinstance(response.json(), list)


@pytest.mark.skip(
reason="Not implemented, see https://github.com/tenstorrent/tt-inference-server/issues/63"
)
def test_get_health():
headers = {}
response = requests.get(HEALTH_URL, headers=headers, timeout=35)
assert response.status_code == 200
34 changes: 34 additions & 0 deletions tt-metal-yolov4/tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SPDX-License-Identifier: Apache-2.0
#
# SPDX-FileCopyrightText: © 2024 Tenstorrent AI ULC

import io
import os
from PIL import Image
import requests


def get_auth_header():
if authorization_header := os.getenv("AUTHORIZATION", None):
headers = {"Authorization": authorization_header}
return headers
else:
raise RuntimeError("AUTHORIZATION environment variable is undefined.")


# save image as JPEG in-memory
def sample_file():
# load sample image
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
pil_image = Image.open(requests.get(url, stream=True).raw)
pil_image = pil_image.resize((320, 320)) # Resize to target dimensions
# convert to bytes
buf = io.BytesIO()
# format as JPEG
pil_image.save(
buf,
format="JPEG",
)
byte_im = buf.getvalue()
file = {"file": byte_im}
return file