def realtime_predict()

in ml-tools-python/realtime.py [0:0]


def realtime_predict(ml_model_id, record):
    """Takes a string ml_model_id, and a dict record, and makes a realtime
    prediction call, if the ML Model has a realtime endpoint.
    If the ML Model doesn't have a realtime endpoint, it creates one instead
    of calling predict()
    """
    ml = boto.connect_machinelearning()
    model = ml.get_ml_model(ml_model_id)
    endpoint = model.get('EndpointInfo', {}).get('EndpointUrl', '')
    #endpoint = endpoint.replace("https://", "")  # This shouldn't be needed
    if endpoint:
        print('ml.predict("%s", %s, "%s") # returns...' % (ml_model_id,
                                                           json.dumps(record, indent=2), endpoint))
        start = time.time()
        prediction = ml.predict(ml_model_id, record, predict_endpoint=endpoint)
        latency_ms = (time.time() - start)*1000
        print(json.dumps(prediction, indent=2))
        print("Latency: %.2fms" % latency_ms)
    else:
        print(
            '# Missing realtime endpoint\nml.create_realtime_endpoint("%s")' % ml_model_id)
        result = ml.create_realtime_endpoint(ml_model_id)
        print(json.dumps(result, indent=2))
        print("""# Predictions will fail until the endpoint has been fully created.
# Note that you will be charged a reservation fee until this endpoint is deleted.
# Delete with:
    python realtime.py %s --deleteEndpoint""" % ml_model_id)