Skip to content

Commit

Permalink
refactor: rename app.py to model_runner.py
Browse files Browse the repository at this point in the history
  • Loading branch information
drduhe committed Oct 17, 2024
1 parent ee271e7 commit e667f4b
Show file tree
Hide file tree
Showing 12 changed files with 451 additions and 279 deletions.
2 changes: 1 addition & 1 deletion bin/oversightml-mr-entry-point.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from codeguru_profiler_agent import Profiler
from pythonjsonlogger import jsonlogger

from aws.osml.model_runner.app import ModelRunner
from aws.osml.model_runner import ModelRunner
from aws.osml.model_runner.common import ThreadingLocalContextFilter


Expand Down
2 changes: 2 additions & 0 deletions src/aws/osml/model_runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
# Telling flake8 to not flag errors in this file. It is normal that these classes are imported but not used in an
# __init__.py file.
# flake8: noqa

from .model_runner import ModelRunner
2 changes: 1 addition & 1 deletion src/aws/osml/model_runner/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
from .image_request import ImageRequest
from .inference import VALID_MODEL_HOSTING_OPTIONS, ModelInvokeMode
from .region_request import RegionRequest
from .request_utils import shared_properties_are_valid
from .request_utils import get_image_path, shared_properties_are_valid
from .sink import SinkMode, SinkType
38 changes: 0 additions & 38 deletions src/aws/osml/model_runner/api/image_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
from json import dumps, loads
from typing import Any, Dict, List, Optional

import boto3
import shapely.geometry
import shapely.wkt
from shapely.geometry.base import BaseGeometry

from aws.osml.model_runner.app_config import BotoConfig
from aws.osml.model_runner.common import (
FeatureDistillationAlgorithm,
FeatureDistillationNMS,
Expand All @@ -19,10 +17,8 @@
MRPostProcessing,
MRPostprocessingStep,
deserialize_post_processing_list,
get_credentials_for_assumed_role,
)

from .exceptions import InvalidS3ObjectException
from .inference import ModelInvokeMode
from .request_utils import shared_properties_are_valid
from .sink import SinkType
Expand Down Expand Up @@ -193,37 +189,3 @@ def get_feature_distillation_option(self) -> List[FeatureDistillationAlgorithm]:
if op.step == MRPostprocessingStep.FEATURE_DISTILLATION
and isinstance(op.algorithm, FeatureDistillationAlgorithm)
]

@staticmethod
def validate_image_path(image_url: str, assumed_role: str) -> bool:
"""
Validate if an image exists in S3 bucket
:param image_url: str = formatted image path to S3 bucket
:param assumed_role: str = containing a formatted arn role
:return: bool
"""
bucket, key = image_url.replace("s3://", "").split("/", 1)
if assumed_role:
assumed_credentials = get_credentials_for_assumed_role(assumed_role)
# Here we will be writing to S3 using an IAM role other than the one for this process.
s3_client = boto3.client(
"s3",
aws_access_key_id=assumed_credentials["AccessKeyId"],
aws_secret_access_key=assumed_credentials["SecretAccessKey"],
aws_session_token=assumed_credentials["SessionToken"],
config=BotoConfig.default,
)
else:
# If no invocation role is provided the assumption is that the default role for this
# container will be sufficient to read/write to the S3 bucket.
s3_client = boto3.client("s3", config=BotoConfig.default)

try:
# head_object is the fastest approach to determine if it exists in S3
# also its less expensive to do the head_object approach
s3_client.head_object(Bucket=bucket, Key=key)
return True
except Exception as err:
raise InvalidS3ObjectException("This image does not exist!") from err
59 changes: 58 additions & 1 deletion src/aws/osml/model_runner/api/request_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Copyright 2023-2024 Amazon.com, Inc. or its affiliates.

from aws.osml.model_runner.common import VALID_IMAGE_COMPRESSION, VALID_IMAGE_FORMATS
import boto3

from aws.osml.model_runner.app_config import BotoConfig
from aws.osml.model_runner.common import VALID_IMAGE_COMPRESSION, VALID_IMAGE_FORMATS, get_credentials_for_assumed_role

from .exceptions import InvalidS3ObjectException
from .inference import VALID_MODEL_HOSTING_OPTIONS


Expand Down Expand Up @@ -55,3 +59,56 @@ def shared_properties_are_valid(request) -> bool:
return False

return True


def get_image_path(image_url: str, assumed_role: str) -> str:
"""
Returns the formatted image path for GDAL to read the image, either from S3 or a local file.
If the image URL points to an S3 path, this method validates the image's existence in S3
and reformats the path to use GDAL's /vsis3/ driver. Otherwise, it returns the local or
network image path.
:param image_url: str = formatted image path to S3 bucket
:param assumed_role: str = containing a formatted arn role
:return: The formatted image path.
"""
if "s3:/" in image_url:
validate_image_path(image_url, assumed_role)
return image_url.replace("s3:/", "/vsis3", 1)
return image_url


def validate_image_path(image_url: str, assumed_role: str) -> bool:
"""
Validate if an image exists in S3 bucket
:param image_url: str = formatted image path to S3 bucket
:param assumed_role: str = containing a formatted arn role
:return: bool
"""
bucket, key = image_url.replace("s3://", "").split("/", 1)
if assumed_role:
assumed_credentials = get_credentials_for_assumed_role(assumed_role)
# Here we will be writing to S3 using an IAM role other than the one for this process.
s3_client = boto3.client(
"s3",
aws_access_key_id=assumed_credentials["AccessKeyId"],
aws_secret_access_key=assumed_credentials["SecretAccessKey"],
aws_session_token=assumed_credentials["SessionToken"],
config=BotoConfig.default,
)
else:
# If no invocation role is provided the assumption is that the default role for this
# container will be sufficient to read/write to the S3 bucket.
s3_client = boto3.client("s3", config=BotoConfig.default)

try:
# head_object is the fastest approach to determine if it exists in S3
# also its less expensive to do the head_object approach
s3_client.head_object(Bucket=bucket, Key=key)
return True
except Exception as err:
raise InvalidS3ObjectException("This image does not exist!") from err
210 changes: 0 additions & 210 deletions src/aws/osml/model_runner/app.py

This file was deleted.

Loading

0 comments on commit e667f4b

Please sign in to comment.