|
| 1 | +from azure.cognitiveservices.personalizer import PersonalizerClient |
| 2 | +import azure.cognitiveservices.personalizer.models as models |
| 3 | +from msrest.authentication import CognitiveServicesCredentials |
| 4 | +from helpers import SlidingAverage |
| 5 | + |
| 6 | +from datetime import datetime |
| 7 | +from random import randint |
| 8 | + |
| 9 | +def run(user_preference, actions, client, duration_sec): |
| 10 | + start = datetime.now() |
| 11 | + ctr = SlidingAverage(window_size = 20) |
| 12 | + while (datetime.now() - start).total_seconds() < duration_sec: |
| 13 | + index = randint(0, len(user_preference) - 1) |
| 14 | + |
| 15 | + user = user_preference[index][0] |
| 16 | + preference = user_preference[index][1] |
| 17 | + |
| 18 | + request=models.RankRequest( |
| 19 | + context_features=user, |
| 20 | + actions=actions |
| 21 | + ) |
| 22 | + response=client.rank(request) |
| 23 | + reward = 1.0 if response.reward_action_id==preference else 0.0 |
| 24 | + client.events.reward(event_id=response.event_id, value=reward) |
| 25 | + |
| 26 | + ctr.update(reward) |
| 27 | + print('CTR: ' + str(ctr.get())) |
| 28 | + for action in response.ranking: |
| 29 | + print(action.id + ': ' + str(action.probability)) |
| 30 | + |
| 31 | + |
| 32 | +def main(): |
| 33 | + |
| 34 | + client = PersonalizerClient(endpoint="", # Put your endpoint here |
| 35 | + credentials=CognitiveServicesCredentials("")) # Put your credentials here |
| 36 | + |
| 37 | + #Available content |
| 38 | + actions=[ |
| 39 | + models.RankableAction( |
| 40 | + id='politics', |
| 41 | + features=[{'topic': 'politics'}]), |
| 42 | + models.RankableAction( |
| 43 | + id='sports', |
| 44 | + features=[{'topic': 'sports'}]), |
| 45 | + models.RankableAction( |
| 46 | + id='music', |
| 47 | + features=[{'topic': 'music'}] |
| 48 | + )] |
| 49 | + |
| 50 | + #User features |
| 51 | + Tom = {'name': 'Tom'} |
| 52 | + Anna = {'name': 'Anna'} |
| 53 | + |
| 54 | + #Time features |
| 55 | + Monday = {'day': 'Monday'} |
| 56 | + Sunday = {'day': 'Sunday'} |
| 57 | + |
| 58 | + context = [Tom, Monday] |
| 59 | + |
| 60 | + request=models.RankRequest( |
| 61 | + context_features=context, |
| 62 | + actions=actions |
| 63 | + ) |
| 64 | + |
| 65 | + response=client.rank(request) |
| 66 | + # Show content to user, evaluate and provide reward back to service |
| 67 | + reward = 1.0 |
| 68 | + client.events.reward(event_id=response.event_id, value=reward) |
| 69 | + |
| 70 | + # Since we are doing cold start and there is no model, all probabilities are the same |
| 71 | + for action in response.ranking: |
| 72 | + print(action.id + ': ' + str(action.probability)) |
| 73 | + |
| 74 | + |
| 75 | + # Tom and Anna have certain preferences what to read on Monday and Sunday |
| 76 | + scenario = [([Tom, Monday], 'politics'), |
| 77 | + ([Tom, Sunday], 'music'), |
| 78 | + ([Anna, Monday], 'sports'), |
| 79 | + ([Anna, Sunday], 'politics')] |
| 80 | + |
| 81 | + run(scenario, actions, client, 5400) |
| 82 | + |
| 83 | + # Olympics started and both Tom and Anna are following sport news during weekend |
| 84 | + scenario = [([Tom, Monday], 'politics'), |
| 85 | + ([Tom, Sunday], 'sports'), |
| 86 | + ([Anna, Monday], 'sports'), |
| 87 | + ([Anna, Sunday], 'sports')] |
| 88 | + run(scenario, actions, client, 5400) |
| 89 | + |
| 90 | +if __name__== '__main__': |
| 91 | + main() |
0 commit comments