in backend/lambda/app.py [0:0]
def lambda_handler(event, context):
# elasticsearch variables
service = 'es'
region = environ['AWS_REGION']
elasticsearch_endpoint = environ['ES_ENDPOINT']
session = boto3.session.Session()
credentials = session.get_credentials()
awsauth = AWS4Auth(
credentials.access_key,
credentials.secret_key,
region,
service,
session_token=credentials.token
)
es = Elasticsearch(
hosts=[{'host': elasticsearch_endpoint, 'port': 443}],
http_auth=awsauth,
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection
)
# sagemaker variables
sagemaker_endpoint = environ['SM_ENDPOINT']
api_payload = json.loads(event['body'])
k = api_payload['k']
if event['path'] == '/postURL':
image = download_file(api_payload['url'])
else:
img_string = api_payload['base64img']
print(img_string)
image = BytesIO(base64.b64decode(img_string))
features = get_features(sm_runtime_client, sagemaker_endpoint, image)
s3_uris_neighbors = get_neighbors(features, es, k_neighbors=k)
s3_presigned_urls = generate_presigned_urls(s3_uris_neighbors)
return {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "*"
},
"body": json.dumps({
"images": s3_presigned_urls,
}),
}