Skip to content

Commit

Permalink
fixes #51: Standardize headers with azureml-model-deployment being ma…
Browse files Browse the repository at this point in the history
…ndantory
  • Loading branch information
Maxence Guindon committed Feb 14, 2024
1 parent 1bc9301 commit 093e6a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
15 changes: 10 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
NACHET_MODEL = os.getenv("NACHET_MODEL")

# The following tuples will be used to store the endpoints and their respective utilitary functions
tuple_endpoints = (((endpoint_url, endpoint_api_key, ""),),((sd_endpoint, sd_api_key, utils.image_slicing),(swin_endpoint, swin_api_key, utils.swin_result_parser)))
tuple_endpoints = (
((endpoint_url, endpoint_api_key, "m-14of15seeds-6seedsmag", None),)
,((sd_endpoint, sd_api_key, "seed-detector-1", utils.image_slicing),
(swin_endpoint, swin_api_key, "swinv1-base-dataaugv2-1", utils.swin_result_parser))
)

CACHE = {
"seeds": None,
Expand Down Expand Up @@ -156,7 +160,8 @@ async def inference_request():
"""
Performs inference on an image, and returns the results.
The image and inference results are uploaded to a folder in the user's container.
"""
"""

seconds = time.perf_counter() # transform into logging
try:
print("Entering inference request") # Transform into logging
Expand Down Expand Up @@ -191,18 +196,18 @@ async def inference_request():
cache_json_result = None
for model in pipelines_endpoints.get(pipeline_name):

endpoint_url, endpoint_api_key, utilitary_function = model
endpoint_url, endpoint_api_key, model_name, utilitary_function = model

if isinstance(image_bytes, list):
result_json = []
for img in image_bytes:
req = await utils.request_factory(img, endpoint_url, endpoint_api_key)
req = await utils.request_factory(img, endpoint_url, endpoint_api_key, model_name)
response = urllib.request.urlopen(req)
result = response.read()
result_json.append(json.loads(result.decode("utf-8")))

elif isinstance(image_bytes, str):
req = await utils.request_factory(image_bytes, endpoint_url, endpoint_api_key)
req = await utils.request_factory(image_bytes, endpoint_url, endpoint_api_key, model_name)
response = urllib.request.urlopen(req)
result = response.read()
result_json = json.loads(result.decode("utf-8"))
Expand Down
20 changes: 5 additions & 15 deletions model_utilitary_functions/model_UTILS.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,14 @@ async def swin_result_parser(img_box:dict, results: dict) -> list:
list: The updated image box with modified labels and scores.
"""
for i, result in enumerate(results):
img_box[0]['boxes'][i]['label'] = result[0].get('label')
img_box[0]['boxes'][i]['score'] = result[0].get('score')
img_box[0]['boxes'][i]['label'] = [d.get("label") for d in result]
img_box[0]['boxes'][i]['score'] = [d.get("score") for d in result]

return img_box

async def seed_detector_header(api_key: str) -> dict:
return {
"Content-Type": "application/json",
"Authorization": ("Bearer " + api_key),
"azureml-model-deployment": "seed-detector-1",
}

# Eventually the goals would be to have a request factory that would return
# a request for the specified models such as the following:
async def request_factory(img_bytes: str | bytes, endpoint_url: str, api_key: str) -> Request:
async def request_factory(img_bytes: str | bytes, endpoint_url: str, api_key: str, model_name: str) -> Request:
"""
Args:
img_bytes (str | bytes): The image data as either a string or bytes.
Expand All @@ -82,14 +75,11 @@ async def request_factory(img_bytes: str | bytes, endpoint_url: str, api_key: st
Returns:
Request: The request object for calling the AI model.
"""

model_name = endpoint_url.split("/")[2].split(".")[0]

headers = {
"Content-Type": "application/json",
"Authorization": ("Bearer " + api_key),
} if model_name != "seed-detector" else await seed_detector_header(api_key)

"azureml-model-deployment": model_name,
}

if isinstance(img_bytes, str):
data = {
Expand Down

0 comments on commit 093e6a8

Please sign in to comment.