[refactor, fix] some minor fixes
Browse files- dockerfiles/dockerfile-lambda-gdal-runner +1 -1
- requirements.txt +2 -1
- src/app.py +16 -14
dockerfiles/dockerfile-lambda-gdal-runner
CHANGED
|
@@ -11,7 +11,7 @@ RUN echo "ENV RIE: $RIE ..."
|
|
| 11 |
WORKDIR ${LAMBDA_TASK_ROOT}
|
| 12 |
COPY requirements.txt ${LAMBDA_TASK_ROOT}/requirements.txt
|
| 13 |
|
| 14 |
-
RUN apt update && apt install -y curl python3-pip
|
| 15 |
RUN which python
|
| 16 |
RUN python --version
|
| 17 |
RUN python -m pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
|
|
|
|
| 11 |
WORKDIR ${LAMBDA_TASK_ROOT}
|
| 12 |
COPY requirements.txt ${LAMBDA_TASK_ROOT}/requirements.txt
|
| 13 |
|
| 14 |
+
RUN apt update && apt install -y curl python3-pip libgl1
|
| 15 |
RUN which python
|
| 16 |
RUN python --version
|
| 17 |
RUN python -m pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
|
requirements.txt
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
-
awslambdaric
|
| 2 |
aws-lambda-powertools
|
|
|
|
| 3 |
bson
|
| 4 |
geojson-pydantic
|
|
|
|
| 5 |
python-dotenv
|
| 6 |
segment-anything-fast
|
| 7 |
segment-geospatial
|
|
|
|
|
|
|
| 1 |
aws-lambda-powertools
|
| 2 |
+
awslambdaric
|
| 3 |
bson
|
| 4 |
geojson-pydantic
|
| 5 |
+
jmespath
|
| 6 |
python-dotenv
|
| 7 |
segment-anything-fast
|
| 8 |
segment-geospatial
|
src/app.py
CHANGED
|
@@ -24,16 +24,15 @@ class LatLngTupleLeaflet(BaseModel):
|
|
| 24 |
class RequestBody(BaseModel):
|
| 25 |
ne: LatLngTupleLeaflet
|
| 26 |
sw: LatLngTupleLeaflet
|
| 27 |
-
points: List[LatLngTupleLeaflet]
|
| 28 |
model: str = MODEL_NAME
|
| 29 |
zoom: float = ZOOM
|
| 30 |
|
| 31 |
|
| 32 |
class ResponseBody(BaseModel):
|
|
|
|
|
|
|
|
|
|
| 33 |
geojson: Dict = None
|
| 34 |
-
request_id: str
|
| 35 |
-
duration_run: float
|
| 36 |
-
message: str
|
| 37 |
|
| 38 |
|
| 39 |
def get_response(status: int, start_time: float, request_id: str, response_body: ResponseBody = None) -> str:
|
|
@@ -50,6 +49,7 @@ def get_response(status: int, start_time: float, request_id: str, response_body:
|
|
| 50 |
str: json response
|
| 51 |
|
| 52 |
"""
|
|
|
|
| 53 |
response_body.duration_run = time.time() - start_time
|
| 54 |
response_body.message = CUSTOM_RESPONSE_MESSAGES[status]
|
| 55 |
response_body.request_id = request_id
|
|
@@ -65,12 +65,17 @@ def get_response(status: int, start_time: float, request_id: str, response_body:
|
|
| 65 |
|
| 66 |
|
| 67 |
def get_parsed_bbox_points(request_input: RequestBody) -> Dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
return {
|
| 69 |
"bbox": [
|
| 70 |
-
|
| 71 |
-
|
| 72 |
],
|
| 73 |
-
"
|
|
|
|
| 74 |
}
|
| 75 |
|
| 76 |
|
|
@@ -101,12 +106,9 @@ def lambda_handler(event: dict, context: LambdaContext):
|
|
| 101 |
app_logger.info(f"body:{body}...")
|
| 102 |
|
| 103 |
try:
|
| 104 |
-
|
| 105 |
-
zoom = body["zoom"] if "zoom" in body else ZOOM
|
| 106 |
-
body_request_validated = RequestBody(ne=body["ne"], sw=body["sw"], points=body["points"], model=model_name, zoom=zoom)
|
| 107 |
-
body_request = get_parsed_bbox_points(body_request_validated)
|
| 108 |
app_logger.info(f"validation ok - body_request:{body_request}, starting prediction...")
|
| 109 |
-
output_geojson_dict = base_predict(bbox=body_request["bbox"], model_name=
|
| 110 |
|
| 111 |
# raise ValidationError in case this is not a valid geojson by GeoJSON specification rfc7946
|
| 112 |
PolygonFeatureCollectionModel(**output_geojson_dict)
|
|
@@ -114,10 +116,10 @@ def lambda_handler(event: dict, context: LambdaContext):
|
|
| 114 |
response = get_response(HTTPStatus.OK.value, start_time, context.aws_request_id, body_response)
|
| 115 |
except ValidationError as ve:
|
| 116 |
app_logger.error(f"validation error:{ve}.")
|
| 117 |
-
response = get_response(HTTPStatus.UNPROCESSABLE_ENTITY.value, start_time, context.aws_request_id)
|
| 118 |
except Exception as e:
|
| 119 |
app_logger.error(f"exception:{e}.")
|
| 120 |
-
response = get_response(HTTPStatus.INTERNAL_SERVER_ERROR.value, start_time, context.aws_request_id)
|
| 121 |
|
| 122 |
app_logger.info(f"response_dumped:{response}...")
|
| 123 |
return response
|
|
|
|
| 24 |
class RequestBody(BaseModel):
|
| 25 |
ne: LatLngTupleLeaflet
|
| 26 |
sw: LatLngTupleLeaflet
|
|
|
|
| 27 |
model: str = MODEL_NAME
|
| 28 |
zoom: float = ZOOM
|
| 29 |
|
| 30 |
|
| 31 |
class ResponseBody(BaseModel):
|
| 32 |
+
request_id: str = None
|
| 33 |
+
duration_run: float = None
|
| 34 |
+
message: str = None
|
| 35 |
geojson: Dict = None
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
def get_response(status: int, start_time: float, request_id: str, response_body: ResponseBody = None) -> str:
|
|
|
|
| 49 |
str: json response
|
| 50 |
|
| 51 |
"""
|
| 52 |
+
app_logger.info(f"response_body:{response_body}.")
|
| 53 |
response_body.duration_run = time.time() - start_time
|
| 54 |
response_body.message = CUSTOM_RESPONSE_MESSAGES[status]
|
| 55 |
response_body.request_id = request_id
|
|
|
|
| 65 |
|
| 66 |
|
| 67 |
def get_parsed_bbox_points(request_input: RequestBody) -> Dict:
|
| 68 |
+
model_name = request_input["model"] if "model" in request_input else MODEL_NAME
|
| 69 |
+
zoom = request_input["zoom"] if "zoom" in request_input else ZOOM
|
| 70 |
+
app_logger.info(f"try to validate input request {request_input}...")
|
| 71 |
+
request_body = RequestBody(ne=request_input["ne"], sw=request_input["sw"], model=model_name, zoom=zoom)
|
| 72 |
return {
|
| 73 |
"bbox": [
|
| 74 |
+
request_body.ne.lat, request_body.sw.lat,
|
| 75 |
+
request_body.ne.lng, request_body.sw.lng
|
| 76 |
],
|
| 77 |
+
"model": request_body.model,
|
| 78 |
+
"zoom": request_body.zoom
|
| 79 |
}
|
| 80 |
|
| 81 |
|
|
|
|
| 106 |
app_logger.info(f"body:{body}...")
|
| 107 |
|
| 108 |
try:
|
| 109 |
+
body_request = get_parsed_bbox_points(body)
|
|
|
|
|
|
|
|
|
|
| 110 |
app_logger.info(f"validation ok - body_request:{body_request}, starting prediction...")
|
| 111 |
+
output_geojson_dict = base_predict(bbox=body_request["bbox"], model_name=body_request["model"], zoom=body_request["zoom"])
|
| 112 |
|
| 113 |
# raise ValidationError in case this is not a valid geojson by GeoJSON specification rfc7946
|
| 114 |
PolygonFeatureCollectionModel(**output_geojson_dict)
|
|
|
|
| 116 |
response = get_response(HTTPStatus.OK.value, start_time, context.aws_request_id, body_response)
|
| 117 |
except ValidationError as ve:
|
| 118 |
app_logger.error(f"validation error:{ve}.")
|
| 119 |
+
response = get_response(HTTPStatus.UNPROCESSABLE_ENTITY.value, start_time, context.aws_request_id, ResponseBody())
|
| 120 |
except Exception as e:
|
| 121 |
app_logger.error(f"exception:{e}.")
|
| 122 |
+
response = get_response(HTTPStatus.INTERNAL_SERVER_ERROR.value, start_time, context.aws_request_id, ResponseBody())
|
| 123 |
|
| 124 |
app_logger.info(f"response_dumped:{response}...")
|
| 125 |
return response
|