|
| 1 | +import os |
| 2 | +import sys, getopt, argparse |
| 3 | +import logging |
| 4 | +from random import randint,random,uniform |
| 5 | +import json |
| 6 | +import urllib |
| 7 | + |
| 8 | +PREDICT_TEMPLATE = 'http://seldon-server/js/predict?json=%JSON%&consumer_key=%KEY%&jsonpCallback=j' |
| 9 | + |
| 10 | +class ReplayCreate(object): |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + self.features = [] |
| 14 | + |
| 15 | + def get_key(self,filename): |
| 16 | + with open(filename) as f: |
| 17 | + for line in f: |
| 18 | + line = line.rstrip() |
| 19 | + j = json.loads(line) |
| 20 | + self.key = j[0]["key"] |
| 21 | + |
| 22 | + def parse_features(self,features): |
| 23 | + for f in features: |
| 24 | + j = json.loads(f) |
| 25 | + self.features.append(j) |
| 26 | + |
| 27 | + def construct_json(self): |
| 28 | + j = {} |
| 29 | + for f in self.features: |
| 30 | + |
| 31 | + if f["type"] == "numeric": |
| 32 | + fval = uniform(f["min"],f["max"]) |
| 33 | + j[f["name"]] = fval |
| 34 | + return json.dumps(j) |
| 35 | + |
| 36 | + def create_replay(self,filename,num): |
| 37 | + with open(filename,"w") as f: |
| 38 | + for i in range (0,num): |
| 39 | + jStr = self.construct_json() |
| 40 | + jEncoded = urllib.quote_plus(jStr) |
| 41 | + url = PREDICT_TEMPLATE.replace("%KEY%",self.key).replace("%JSON%",jEncoded)+"\n" |
| 42 | + f.write(url) |
| 43 | + |
| 44 | +if __name__ == '__main__': |
| 45 | + import logging |
| 46 | + logger = logging.getLogger() |
| 47 | + logging.basicConfig(format='%(asctime)s : %(levelname)s : %(name)s : %(message)s', level=logging.DEBUG) |
| 48 | + logger.setLevel(logging.INFO) |
| 49 | + |
| 50 | + parser = argparse.ArgumentParser(prog='create_replay') |
| 51 | + parser.add_argument('--key', help='file containing output of seldon-cli keys call', required=True) |
| 52 | + parser.add_argument('--replay', help='replay file to create', required=True) |
| 53 | + parser.add_argument('--num', help='number of actions and recommendation pair calls to create', required=False, type=int, default=1000) |
| 54 | + parser.add_argument('--feature', help='feature to add ', required=True, action='append') |
| 55 | + |
| 56 | + args = parser.parse_args() |
| 57 | + opts = vars(args) |
| 58 | + |
| 59 | + rc = ReplayCreate() |
| 60 | + rc.get_key(args.key) |
| 61 | + rc.parse_features(args.feature) |
| 62 | + rc.create_replay(args.replay,args.num) |
0 commit comments