-
Notifications
You must be signed in to change notification settings - Fork 1
/
playground.py
189 lines (167 loc) · 5.57 KB
/
playground.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#%%
import boto3
import time
import uuid
import json
from configure import (
APIS,
REGION,
EVENT_TRACKERS,
PYTHON_LAMBDA_CONFIG,
BASE_ENV_VARIABLES,
BASE_INTEGRATION_CONFIG,
BASE_METHOD_RESPONSE)
# %%
personalize = boto3.client('personalize')
personalize_runtime = boto3.client('personalize-runtime', region_name=REGION)
# Establish a connection to Personalize's event streaming
personalize_events = boto3.client(service_name='personalize-events', region_name=REGION)
# %%
EVENT_TRACKERS
# %%
get_recommendations_response = personalize_runtime.get_recommendations(
campaignArn = APIS['sims']['CAMPAIN_ARN'],
itemId = '300',
filterArn = 'arn:aws:personalize:us-west-2:625806755153:filter/Drama'
)
get_recommendations_response
# %%
get_recommendations_response = personalize_runtime.get_recommendations(
campaignArn = APIS['recommend']['CAMPAIN_ARN'],
userId = '300',
filterArn = 'arn:aws:personalize:us-west-2:625806755153:filter/Shounen'
)
get_recommendations_response
# %%
get_recommendations_response = personalize_runtime.get_personalized_ranking(
campaignArn = APIS['rerank']['CAMPAIN_ARN'],
userId = '300',
filterArn = 'arn:aws:personalize:us-west-2:625806755153:filter/Comedy',
inputList= ['3000', '3001', '2500']
)
get_recommendations_response
#%%
event = {
"itemId": '16300',
}
event_json = json.dumps(event)
put_event_response = personalize_events.put_events(
trackingId = EVENT_TRACKERS['eventtracker']['TRACKING_ID'],
userId= '10000',
sessionId = str(uuid.uuid1()),
eventList = [{
'sentAt': int(time.time()),
'eventType': 'RATING',
'eventValue': 9,
'properties': event_json
}]
)
put_event_response
#%%
str(uuid.uuid1())
# %%
str(uuid.uuid4())
# %%
import json
json.loads('{"itemId": "16300",\n "eventType: "RATING",\n "eventValue": "1",\n "sessionId": "10000"\n}')
# %%
import boto3
client = boto3.client('sts')
# %%
client.get_caller_identity()
# %%
personalize_client = boto3.client('personalize', region_name='us-west-2')
# %%
personalize_client.list_campaigns()['campaigns']
# %%
#personalize_client.list_event_trackers(datasetGroupArn='arn:aws:personalize:us-west-2:625806755153:dataset-group/personalize-anime')['eventTrackers']
personalize_client.describe_event_tracker(
eventTrackerArn='arn:aws:personalize:us-west-2:625806755153:event-tracker/d2e0162e'
)['eventTracker']
# %%
personalize_client.list_dataset_groups()
# %%
def get_personalize_event_trackers(client):
dsgs = client.list_dataset_groups()['datasetGroups']
dsg_arns = []
for dsg in dsgs:
if dsg['status'] == 'ACTIVE':
dsg_arns.append(dsg['datasetGroupArn'])
event_trackers = []
for dsg_arn in dsg_arns:
e_trackers = client.list_event_trackers(datasetGroupArn=dsg_arn)['eventTrackers']
for et in e_trackers:
if et['status'] == 'ACTIVE':
details = client.describe_event_tracker(eventTrackerArn=et['eventTrackerArn'])['eventTracker']
event_trackers.append({
'name': et['name'],
'arn':et['eventTrackerArn'],
'trackingId': details['trackingId']
})
return event_trackers
get_personalize_event_trackers(personalize_client)
# %%
def get_personalize_filters(client):
dsgs = client.list_dataset_groups()['datasetGroups']
dsg_arns = []
for dsg in dsgs:
if dsg['status'] == 'ACTIVE':
dsg_arns.append(dsg['datasetGroupArn'])
filters = []
for dsg_arn in dsg_arns:
ffilters = client.list_filters(datasetGroupArn=dsg_arn)['Filters']
for ffilter in ffilters:
if ffilter['status'] == 'ACTIVE':
filters.append({'name': ffilter['name'], 'arn':ffilter['filterArn']})
return filters
get_personalize_filters(personalize_client)
#%%
personalize_client.list_recipes()['recipes']
# %%
personalize_client.describe_campaign(campaignArn='arn:aws:personalize:us-west-2:625806755153:campaign/personalize-anime-SIMS')['campaign']
# %%
personalize_client.describe_solution_version(solutionVersionArn='arn:aws:personalize:us-west-2:625806755153:solution/personalize-anime-sims/4a25f136')['solutionVersion']
# %%
def get_personalize_campains(client):
campaigns = client.list_campaigns()['campaigns']
camps = []
for c in campaigns:
if c['status'] == 'ACTIVE':
details = client.describe_campaign(campaignArn=c['campaignArn'])['campaign']
solution_version_arn = details['solutionVersionArn']
solution_details = client.describe_solution_version(solutionVersionArn=solution_version_arn)['solutionVersion']
recipe_arn = solution_details['recipeArn']
camps.append({
'name': c['name'],
'arn': c['campaignArn'],
'recipe': recipe_arn.split('/')[-1]
})
return camps
# %%
campanas = get_personalize_campains(personalize_client)
# %%
campanas
# %%
APIS = []
for c in campanas:
if 'sims' in c['recipe']:
c['type'] = 'sims'
elif 'ranking' in c['recipe']:
c['type'] = 'rerank'
elif ('user-personalization' in c['recipe']) or ('hrnn' in c['recipe']) or ('popularity-count' in c['recipe']):
c['type'] = 'recommend'
APIS.append({
'CAMPAIGN_ARN': c['arn'],
'API_NAME': c['name'].lower(),
'CAMPAIGN_TYPE': c['type']
})
APIS
# %%
import boto3
account_id = boto3.client('sts').get_caller_identity()['Account']
account_id_anonymized = '******'+str(account_id)[6:]
# %%
#account_id_anonymized[:6] = '******'
# %%
account_id, account_id_anonymized
# %%