-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (39 loc) · 1.14 KB
/
main.py
File metadata and controls
53 lines (39 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import io
import os
import base64
# import requests
import json
from sentence_transformers import SentenceTransformer, util
from PIL import Image
import boto3
s3 = boto3.client('s3')
model = None
def handler(event, context):
global model
if model is None:
print("Loadin clip model")
model = SentenceTransformer('clip-ViT-B-32', cache_folder="/var/task/.cache")
print("Loaded clip")
path = event["path"]
image = None
body = event["body"]
if event["isBase64Encoded"]:
body = base64.b64decode(body)
if path == 'processs3':
imageId = json.loads(body)["imageid"]
bucket = os.getenv("BUCKET")
result = s3.get_object(Bucket = bucket, Key = imageId)
contents = result['Body'].read()
image = Image.open(io.BytesIO(contents))
elif path == 'process':
image = Image.open(io.BytesIO(body))
print(image)
img_emb = model.encode(image)
payload = {"embedding": img_emb.tolist()}
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps(payload)
}