-
Notifications
You must be signed in to change notification settings - Fork 0
/
LF2.py
118 lines (102 loc) · 3.32 KB
/
LF2.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import json
import string
import boto3
from opensearchpy import OpenSearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
from botocore.exceptions import ClientError
from inflection import singularize
REGION = 'us-east-1'
HOST = 'search-photos-cf-27dfs4gpdvp4vxd7iezhtax2xy.us-east-1.es.amazonaws.com'
INDEX = 'photos-cf'
from requests_aws4auth import AWS4Auth
def get_awsauth(region, service):
cred = boto3.Session().get_credentials()
return AWS4Auth(cred.access_key,
cred.secret_key,
region,
service,
session_token=cred.token)
def query(term):
q = { "size": 5,
"query": {
"bool": {
"must": {
"match": {
"labels": term
}
}
}
}
}
client = OpenSearch(hosts=[{
'host': HOST,
'port': 443
}],
http_auth=get_awsauth(REGION, 'es'),
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection)
res = client.search(index=INDEX, body=q)
#print(res)
hits = res['hits']['hits']
results = []
for hit in hits:
img = {}
img['url'] = 'https://' + hit['_source']['bucket'] + '.s3.amazonaws.com/' + hit['_source']['objectKey']
img['labels'] = hit['_source']['labels']
results.append(img)
return results
def dispatch(event):
client = boto3.client('lexv2-runtime')
msg_from_user = event["queryStringParameters"]['q']
print(f"Message from frontend: {msg_from_user}")
# Initiate conversation with Lex
response = client.recognize_text(
botId='I3TNCJ7UW1', # MODIFY HERE
botAliasId='RXVDTKMQ7J', # MODIFY HERE
localeId='en_US',
sessionId='testuser',
text=msg_from_user)
print(f"Response from lex: {response}")
msg_from_lex = response.get('messages', [])
if msg_from_lex:
labels = msg_from_lex[0]['content']
print(f"labels interpreted: ", labels)
labels = labels.split(', ')
img1 = img2 = []
if labels[0] != 'label1':
word = string.capwords(labels[0])
#word = TextBlob(word).words[0]
img1 = query(singularize(word))
if labels[1] != 'label2':
word = string.capwords(labels[1])
#word = TextBlob(word).words[0]
img2 = query(singularize(word))
else:
labels = [labels[0]]
label = [{'url':None, 'labels': labels}]
json_resp = {'results': label + img1 + img2}
resp = {
"isBase64Encoded": False,
'statusCode': 200,
"headers": {"Access-Control-Allow-Origin": "*"},
'body': json.dumps(json_resp)
}
print(resp)
return resp
else:
json_resp = {'results': label}
resp = {
"isBase64Encoded": False,
'statusCode': 403,
"headers": {"Access-Control-Allow-Origin": "*"},
'body': json.dumps(json_resp)
}
print(resp)
return resp
def lambda_handler(event, context):
print(f"event: {event}")
print(f"context: {context}")
response = dispatch(event)
return response