Пример #1
0
def predict():
    print(request)
    image = request.files["image"].read()
    image = Image.open(io.BytesIO(image))
    image = prepare_image(image)

    # Faça uma requisição para o serviço Watson Machine Learning aqui e retorne a classe detectada na variável 'resposta'
    classes = ['normal', 'praga']
			
    wml_credentials = {
        'apikey': 'B-H331DpFRKwapGsKOLjOBrnShvRCOQxokLvTbqM2OTq',
        'instance_id': '6d54ebaf-26c9-43a4-b69c-e9b8e30c138a',
        'url': 'https://us-south.ml.cloud.ibm.com'
    }
    client = WatsonMachineLearningAPIClient(wml_credentials)

    url = 'https://us-south.ml.cloud.ibm.com/v3/wml_instances/6d54ebaf-26c9-43a4-b69c-e9b8e30c138a/deployments/c1a44b0a-5106-45d5-bd2b-6c20aca51ab1/online'
    response = client.deployments.score(url, {
        'values': image
    })
    
    resposta = {
        "class": classes[response['values'][0][1]]
    }
    return resposta
Пример #2
0
def store(wml_model_name, run_uid):
    from watson_machine_learning_client import WatsonMachineLearningAPIClient

    # retrieve credentials
    wml_url = getSecret("/app/secrets/wml_url")
    wml_instance_id = getSecret("/app/secrets/wml_instance_id")
    wml_apikey = getSecret("/app/secrets/wml_apikey")

    # set up the WML client
    wml_credentials = {
        "url": wml_url,
        "instance_id": wml_instance_id,
        "apikey": wml_apikey
    }
    client = WatsonMachineLearningAPIClient(wml_credentials)

    # store the model
    stored_model_name = wml_model_name
    stored_model_details = client.repository.store_model(
        run_uid, stored_model_name)
    model_uid = client.repository.get_model_uid(stored_model_details)
    print("model_uid: ", model_uid)

    with open("/tmp/model_uid", "w") as f:
        f.write(model_uid)
    f.close()

    import time
    time.sleep(120)
Пример #3
0
    def score(payload):
        try:
            import re
            from watson_machine_learning_client import WatsonMachineLearningAPIClient
            client = WatsonMachineLearningAPIClient(params['wml_credentials'])

            maxlen = 50

            preprocessed_records = []
            complain_data = payload['values']
            word_index = params['word_index']

            for data in complain_data:
                comment = data[0]
                cleanString = re.sub(r"[!\"#$%&()*+,-./:;<=>?@[\]^_`{|}~]", "", comment)
                splitted_comment = cleanString.split()[:maxlen]
                hashed_tokens = []

                for token in splitted_comment:
                    index = word_index.get(token, 0)
                    if index < 501 and index > 0:
                        hashed_tokens.append(index)

                hashed_tokens_size = len(hashed_tokens)
                padded_tokens = [0]*(maxlen-hashed_tokens_size) + hashed_tokens
                preprocessed_records.append(padded_tokens)

            scoring_payload = {'values': preprocessed_records}
            print(str(scoring_payload))
            return client.deployments.score(params['scoring_endpoint'], scoring_payload)

        except Exception as e:
            return { "error" : repr( e ) }
Пример #4
0
    def __init__(self, url, api_key, instance_id):
        """
        Initializer

        """

        try:
            self.client = WatsonMachineLearningAPIClient({
                'url':
                url,
                'apikey':
                api_key,
                'instance_id':
                instance_id
            })
            self.client.service_instance.get_details()
        except ApiRequestFailure as arf:
            debug('Exception: {}'.format(arf))
            p = WMLWrapper.parse_WML_ApiRequestFailure(arf)
            if p:
                msg = p['error_message']
            else:
                msg = arf.error_msg
            raise WMLWrapperError(
                'Error. Cannot connect to WML service: {}'.format(msg))
        except Exception as ex:
            debug('Exception type: {}'.format(type(ex)))
            debug('Exception: {}'.format(ex))
            raise WMLWrapperError(ex)
Пример #5
0
def predict():
    print(request)
    image = request.files["image"].read()
    image = Image.open(io.BytesIO(image))
    image = prepare_image(image)

    # Faça uma requisição para o serviço Watson Machine Learning aqui e retorne a classe detectada na variável 'resposta'
    wml_credentials  = {
        "apikey": "w0Jfv25Y1emvcA6aA-Gf_Eys9ZzPpHj0EQdASS3Kbldz",
        "iam_apikey_description": "Auto-generated for key b1e50421-929e-4d18-98e9-32729c898390",
        "iam_apikey_name": "wdp-writer",
        "iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Writer",
        "iam_serviceid_crn": "crn:v1:bluemix:public:iam-identity::a/c803aecf7aac4d009194a0cce5abb600::serviceid:ServiceId-7cdc734b-12d3-4b8c-90fd-8ac4d377d601",
        "instance_id": "bdd4074e-e090-4d18-998d-c2cf18a1f3e6",
        "url": "https://us-south.ml.cloud.ibm.com"
    }
    client = WatsonMachineLearningAPIClient( wml_credentials )
    model_payload = { "values" : image }
    ai_parms = { "wml_credentials" : wml_credentials, "model_endpoint_url" : 'https://us-south.ml.cloud.ibm.com/v3/wml_instances/bdd4074e-e090-4d18-998d-c2cf18a1f3e6/deployments/8165db11-40d7-438b-ac99-714156339e5f/online' }
    model_result = client.deployments.score( ai_parms["model_endpoint_url"], model_payload )


    resposta = {
        "class": "praga" if model_result['values'][0][1][0] == 1 else "normal"
    }
    return resposta
def connect_to_wml():
    wml_url = os.getenv('ICP4D_CLUSTER_HOST', False)
    if not wml_url:
        sys.exit('missing ICP4D_CLUSTER_HOST env var')
        return

    wml_port = os.getenv('ICP4D_CLUSTER_PORT', False)
    if not wml_port:
        sys.exit('missing ICP4D_CLUSTER_PORT env var')
        return

    wml_user = os.getenv('ICP4D_CLUSTER_USER', False)
    if not wml_user:
        sys.exit('missing ICP4D_CLUSTER_USER env var')
        return

    wml_password = os.getenv('ICP4D_CLUSTER_PASSWORD', False)
    if not wml_password:
        sys.exit('missing ICP4D_CLUSTER_PASSWORD env var')
        return

    wml_credentials = {
        "url": f"https://{wml_url}",
        "username": f"{wml_user}",
        "password": f"{wml_password}",
        "instance_id": "icp"
    }
    return WatsonMachineLearningAPIClient(wml_credentials)
Пример #7
0
def lambda_handler(event, context):
    # List of Feature lists [[...,...,...],[...,...,...]]
    features = event["features"]

    wml_credentials = {
        'url': os.getenv("IBM_URL"),
        'username': os.getenv("IBM_USERNAME"),
        'password': os.getenv("IBM_PASSWORD"),
        'instance_id': os.getenv("IBM_INSTANCE_ID"),
        'apikey': os.getenv("IBM_API_KEY"),
        'iam_apikey_name': os.getenv("IBM_API_KEY_NAME"),
        'iam_role_crn': os.getenv("IBM_ROLE_CRN"),
        'iam_serviceid_crn': os.getenv("IBM_SERVICE_ID_CRN")
    }

    client = WatsonMachineLearningAPIClient(wml_credentials)

    scoring_url = 'https://us-south.ml.cloud.ibm.com/v3/wml_instances/cc2d5c29-9b6a-4e09-a82c-8279e879d544/deployments/5a9d3cb5-aae1-49bc-be7f-26539c9e7ec5/online'
    payload_scoring = {
        'fields': [
            'acousticness', 'loudness', 'instrumentalness', 'danceability',
            'valence', 'energy', 'duration_ms'
        ],
        'values':
        features
    }

    res = client.deployments.score(scoring_url, payload_scoring)
    return res
Пример #8
0
def deploy(args):
    from watson_machine_learning_client import WatsonMachineLearningAPIClient
    from minio import Minio
    import os

    wml_model_name = args.model_name
    wml_scoring_payload = args.scoring_payload
    model_uid = args.model_uid

    # retrieve credentials
    wml_url = getSecret("/app/secrets/wml_url")
    wml_username = getSecret("/app/secrets/wml_username")
    wml_password = getSecret("/app/secrets/wml_password")
    wml_instance_id = getSecret("/app/secrets/wml_instance_id")

    cos_endpoint = getSecret("/app/secrets/cos_endpoint")
    cos_access_key = getSecret("/app/secrets/cos_access_key")
    cos_secret_key = getSecret("/app/secrets/cos_secret_key")

    cos_input_bucket = getSecret("/app/secrets/cos_input_bucket")

    # set up the WML client
    wml_credentials = {
        "url": wml_url,
        "username": wml_username,
        "password": wml_password,
        "instance_id": wml_instance_id
    }
    client = WatsonMachineLearningAPIClient(wml_credentials)

    # deploy the model
    deployment_name = wml_model_name
    deployment_desc = "deployment of %s" % wml_model_name
    deployment = client.deployments.create(model_uid, deployment_name,
                                           deployment_desc)
    scoring_endpoint = client.deployments.get_scoring_url(deployment)
    print("scoring_endpoint: ", scoring_endpoint)

    # download scoring payload
    payload_file = os.path.join('/app', wml_scoring_payload)

    cos = Minio(cos_endpoint,
                access_key=cos_access_key,
                secret_key=cos_secret_key)
    cos.fget_object(cos_input_bucket, wml_scoring_payload, payload_file)

    # scoring the deployment
    import json
    with open(payload_file) as data_file:
        test_data = json.load(data_file)
    payload = test_data['payload']
    data_file.close()

    print("Scoring result: ")
    result = client.deployments.score(scoring_endpoint, payload)
    print(result)

    with open("/tmp/output", "w") as f:
        print(result, file=f)
    f.close()
Пример #9
0
    def configure_utilities(self, cos_credentials, wml_credentials):

        self.cos_credentials = cos_credentials
        self.wml_credentials = wml_credentials

        self.cos_utils = CosUtils(self.cos_credentials, self.region)
        self.wml_client = WatsonMachineLearningAPIClient(self.wml_credentials)
        print("WML client version: %s" % self.wml_client.version)
Пример #10
0
def get_prediction(http_ref):
    image = get_frame(http_ref)
    client = WatsonMachineLearningAPIClient(cos_credentials)
    prep = process_image(image).tolist()
    scoring_endpoint = client.deployments.get_scoring_url(deployment)
    scoring_payload = {'values': prep}
    scores = client.deployments.score(scoring_endpoint, scoring_payload)
    return np.sum(np.array(scores['values']))
Пример #11
0
def predict():
    print(request)
    image = request.files["image"].read()
    image = Image.open(io.BytesIO(image))
    image = prepare_image(image)

    # Faça uma requisição para o serviço Watson Machine Learning aqui e retorne a classe detectada na variável 'resposta'
    # Credenciais do Watson Machine Learning
    wml_credentials = {
        "apikey": "K9SRZk1WdW9oA_uEbSUV6IBDnYOz5RC8FTSnqB0pWiPT",
        "iam_apikey_description":
        "Auto-generated for key 8d98bf3c-518d-492f-a271-d0b6b7ebd362",
        "iam_apikey_name": "wdp-writer",
        "iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Writer",
        "iam_serviceid_crn":
        "crn:v1:bluemix:public:iam-identity::a/1de11f71360f42fd9ba01f2b76bced35::serviceid:ServiceId-e809b862-d7cc-4230-8f62-7c56d45935f2",
        "instance_id": "f872faaa-1d3b-4d8a-9cf7-1eef9672debb",
        "url": "https://us-south.ml.cloud.ibm.com"
    }
    client = WatsonMachineLearningAPIClient(wml_credentials)

    # # Definição de metadados do modelo (versao de python, framework, libs e etc)
    # sample_saved_model_filename = 'model_WSTUDIO.tar.gz'
    # metadata = {
    #     client.repository.ModelMetaNames.NAME: 'MY_FIRST_SUBMIT',
    #     client.repository.ModelMetaNames.FRAMEWORK_NAME: 'tensorflow',
    #     client.repository.ModelMetaNames.FRAMEWORK_VERSION: '1.11',
    #     client.repository.ModelMetaNames.RUNTIME_NAME: 'python',
    #     client.repository.ModelMetaNames.RUNTIME_VERSION: '3.6',
    #     client.repository.ModelMetaNames.FRAMEWORK_LIBRARIES:  [{"name": "keras", "version": "2.2.4"}]
    # }

    # # Conexão com o WML
    # model_details = client.repository.store_model(
    #     sample_saved_model_filename, meta_props=metadata, training_data=None)

    # # Deploy do modelo
    # model_id = model_details["metadata"]["guid"]
    # model_deployment_details = client.deployments.create(
    #     artifact_uid=model_id, name="MY FIRST SUBMIT D9 Behind The Code")

    # # Retrieve da URL da API para consumo da mesma
    # model_endpoint_url = client.deployments.get_scoring_url(
    #     model_deployment_details)
    # client.deployments.get_scoring_url()
    # print("A URL de chamada da sua API é : ", model_endpoint_url)
    # ####
    model_endpoint_url = "https://us-south.ml.cloud.ibm.com/v3/wml_instances/f872faaa-1d3b-4d8a-9cf7-1eef9672debb/deployments/3250b14b-3ec3-4c75-8165-8a0e58bed380/online"

    model_result = client.deployments.score(model_endpoint_url,
                                            {'values': image})

    classes = ['CLEAN', 'DIRTY']
    resposta = {"class": classes[model_result['values'][0][1]]}
    return resposta
Пример #12
0
    def __init__(self, deployment_uid=None):
        from watson_machine_learning_client import WatsonMachineLearningAPIClient

        self.deployment_uid = deployment_uid

        wml_credentials = {
            'apikey': WML_API_KEY,
            'instance_id': WML_INSTANCE_ID,
            'url': WML_URL
        }
        self.wml_client = WatsonMachineLearningAPIClient(wml_credentials)
Пример #13
0
def get_scoring_url():
    wml_credentials = CONFIG["wml_credentials"]
    global client
    client = WatsonMachineLearningAPIClient(wml_credentials)
    # print(client.repository.list_models())
    # print(client.deployments.get_details())
    deployment_details = client.deployments.get_details(
        CONFIG["deployment_id"])
    scoring_url = client.deployments.get_scoring_url(deployment_details)
    print(scoring_url)
    # scoring_url = 'https://ibm-watson-ml.mybluemix.net/v3/wml_instances/e7e44faf-ff8d-4183-9f37-434e2dcd6852/deployments/9ed7fe34-d927-4111-8b95-0e72a3bde6f8/online'
    return scoring_url
Пример #14
0
def put_visitor():
    """Gets the top recommendations for a given user
    """
    # catch invalid usernames and recommend random articles if invalic
    try:
        # connect to the client
        print("Attempting to connect...")
        wml_client = WatsonMachineLearningAPIClient(wml_credentials)

        # debugging
        # print(wml_credentials)
        # Get the article scores for every user, sort them, and serve them

        # fetch the username 
        user = request.json['name']
        user_id = user_map_articles[user]


        # get the keys for all the article ids
        all_articles = list(article_map.keys())

        # create a request
        input_json = {
        'fields': ["indexed_email", "article_id"],
        'values': [[user_id,float(article_id)] for article_id in all_articles]} # list comprehension points to all articles

        # score
        predictions = client.deployments.score(scoring_url, input_json)

        # we are only interested in the prediction and article_id
        vals = [(p[1], p[-1]) for p in predictions['values']]

        # sort the tuples
        vals.sort(key=operator.itemgetter(1))

        # return the titles
        titles = [article_map[v[0]] for v in vals[-5:]]

        # pretty print
        ret = (''.join(["{}. {} \n".format(i+1, t) for i,t in enumerate(titles)]))
        return ret

    except:
        # return random ones now 
        # Student TODO students can change to most popular
        import random
        article_titles = list(article_map.values())
        random.shuffle(article_titles)
        titles = article_titles[:5]
        ret = (''.join(["{}. {} \n".format(i+1, t) for i,t in enumerate(titles)]))
        return ret
Пример #15
0
def connect_wml_service():
    """
    Instantiate a client using credentials
    """

    wmlcreds = load_wml_credentials()
    wml_credentials = {
        "apikey": wmlcreds['apikey'],
        "instance_id": wmlcreds['instance_id'],
        "url": wmlcreds['url'],
    }

    client = WatsonMachineLearningAPIClient(wml_credentials)
    return(client)
def get_watson_client():
    wml_credentials = {
        "apikey": settings.APIKEY,
        "iam_apikey_description":
        "Auto-generated for key 692bdd58-8640-4cba-a350-e09c2cdff284",
        "iam_apikey_name": "Service credentials-1",
        "iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Writer",
        "iam_serviceid_crn":
        "crn:v1:bluemix:public:iam-identity::a/19854af9b0b64470afa45df27dda27a5::serviceid:ServiceId-44f19be1-dd11-440c-86ac-bbb52552c780",
        "instance_id": settings.INSTANCE_ID,
        "url": "https://eu-gb.ml.cloud.ibm.com"
    }
    client = WatsonMachineLearningAPIClient(wml_credentials)
    return client
Пример #17
0
def apicall(id):
    wml_credential = {
                "apikey": "ZFx_bv7PIYgofeIUX_rRo2KBAo-CQ7dGjuSFstoYqKpu",
                "instance_id": "17c49ad1-7170-4aaf-9b56-3b17bc91419b",
                "url": "https://us-south.ml.cloud.ibm.com"
            }
    client = WatsonMachineLearningAPIClient(wml_credential)

    scoring_endpoint = "https://us-south.ml.cloud.ibm.com/v4/deployments/eecc4287-a6bc-44f6-90d7-1628e4e066bd/predictions"

    payload_scoring = {"input_data": [{"fields": ["Loan_ID", "Gender", "Married", "Dependents", "Education",
                                              "Self_Employed", "ApplicantIncome", "CoapplicantIncome", "LoanAmount",
                                              "Loan_Amount_Term", "Credit_History", "Property_Area"], "values": [id]}]}

    return client.deployments.score(scoring_endpoint, payload_scoring)
def register_custom_model_wml(df, columns=[]):
    # initialize WML client
    wml_creds_path = 'wml_credentials.json'
    wml_credentials = json.loads(open(wml_creds_path).read())
    client = WatsonMachineLearningAPIClient(wml_credentials)
    # create a model (using IsolationForest here with default settings)
    clf = IsolationForest()
    # place classifier in a sklearn "pipeline"
    pipeline = Pipeline(steps=[("classifier", clf)])
    # clean data, reduce to relevant input columns
    if len(columns) < 0:
        s_df = df[columns]
    else:
        print("please specify your input columns")
        exit()
        # s_df = df[:]
    # create list of df rows
    rows = [list(r) for i, r in s_df.iterrows()]
    # fit df rows in pipeline
    pipeline.fit(rows)
    # pipeline.predict(rows)

    # publish pipeline to WML
    sk_version = sklearn.__version__
    metadata = {
        client.repository.ModelMetaNames.FRAMEWORK_VERSION: 'scikit-learn',
        client.repository.ModelMetaNames.FRAMEWORK_VERSION: sk_version,
        client.repository.ModelMetaNames.NAME: 'anomaly_model',
        client.repository.ModelMetaNames.RUNTIME_NAME: 'python',
        client.repository.ModelMetaNames.RUNTIME_VERSION: '3.6'
    }
    model_details_inmem = client.repository.store_model(pipeline,
                                                        meta_props=metadata)
    model_id_inmem = model_details_inmem["metadata"]["guid"]
    deployment_details_inmem = client.deployments.create(
        artifact_uid=model_id_inmem, name="anomaly_model")
    deployment_id = deployment_details_inmem["metadata"]["guid"]
    # Test model
    # data_to_post = {"values": rows}
    model_endpoint_url_inmem = client.deployments.get_scoring_url(
        deployment_details_inmem)
    print("model invocation endpoint: " + model_endpoint_url_inmem)
    # client.deployments.score( model_endpoint_url_inmem, data_to_post )
    print("Place below line in .env file like so")
    # print("model_id: " + model_id)
    print("WATSON_ML_DEPLOYMENT_ID=" + deployment_id)
    return deployment_id
Пример #19
0
def main():
    """
  Connect to a copy of the model deployed via the deploy_wml.py script,
  generate a web service request, pass that request through the model,
  and print the result.

  Before running this script, you need to perform the following manual steps:
  * Perform the manual steps outlined in the deploy_wml.py script.
  * Run the deploy_wml.py script
  * Enter the deployment URL that the deploy_wml.py script prints out into
    the local file `ibm_cloud_credentials.json` under the key
    "WML_function_url".
  """
    if not os.path.isdir(_TMP_DIR):
        os.mkdir(_TMP_DIR)

    # Prepare a request
    image_path = util.fetch_or_use_cached(_TMP_DIR, "panda.jpg",
                                          _PANDA_PIC_URL)
    with open(image_path, "rb") as f:
        image_data = f.read()
    thresh = 0.7

    # Note that "values" tag at the top level. This tag is a requirement of the
    # WML API standard.
    # Under the "values" tag, you must place a list of tuples. Each tuple must
    # be represented as a JSON list of values. Tensor-valued values must be
    # represented as lists of numbers.
    request_json = {
        "fields": ["image", "threshold"],
        "values": [[
            # TensorFlow only decodes URL-safe base64
            base64.urlsafe_b64encode(image_data).decode("utf-8"),
            thresh
        ]]
    }

    # Connect to Watson Machine Learning Python API
    with open("./ibm_cloud_credentials.json") as f:
        creds_json = json.load(f)
    _WML_CREDENTIALS = creds_json["WML_credentials"]
    _WML_FUNCTION_URL = creds_json["WML_function_url"]
    client = WatsonMachineLearningAPIClient(_WML_CREDENTIALS)

    response = client.deployments.score(_WML_FUNCTION_URL, request_json)
    print("Response: {}".format(response))
Пример #20
0
    def score( function_payload ):

        try:
            pred_data = function_payload["values"]
            max_values, min_values = recover_array_from_string(max_string, min_string)
            pred_data = refine_data(pred_data, num_input, max_values, min_values).tolist()
            model_payload = { "values" : pred_data }
            
            from watson_machine_learning_client import WatsonMachineLearningAPIClient
            client       = WatsonMachineLearningAPIClient( parms["wml_credentials"] )
            model_result = client.deployments.score( parms["model_endpoint_url"], model_payload )
            human_result  = int2string(model_result["values"])
            return human_result

        except Exception as e:
            
            return { "Error" : repr( e ) }
Пример #21
0
        def score(payload):
            try:
                import re
                import numpy as np
                from watson_machine_learning_client import WatsonMachineLearningAPIClient
                client = WatsonMachineLearningAPIClient(params['wml_credentials'])

                preprocessed_records = []
                texts = payload['values']
                word_index = params['word_index']
                maxlen = 50
                for text in texts:
                    cleanString = re.sub(r"[!\"#$%&()*+,-./:;<=>?@[\]^_`{|}~]", "", text)
                    splitted_text = cleanString.split()[:maxlen]
                    hashed_tokens = []
                    for token in splitted_text:
                        index = word_index.get(token, 0)
                        # index = scoring_params["word_index"].get(token, 0)
                        if index < 501 and index > 0:
                            hashed_tokens.append(index)

                    hashed_tokens_size = len(hashed_tokens)
                    padded_tokens = [0]*(maxlen - hashed_tokens_size) + hashed_tokens
                    preprocessed_records.append(padded_tokens)

                to_predict_arr = np.asarray(preprocessed_records)
                scoring_payload = {'values': to_predict_arr.tolist()}

                # print(str(scoring_payload))
                resp = client.deployments.score(params['scoring_endpoint'], scoring_payload)
                return_list = []
                ERROR_THRESHOLD = 0.15
                for val in resp["values"]:
                    result = val[0]
                    result = [[i,r] for i,r in enumerate(result) if r>ERROR_THRESHOLD]
                    # sort by strength of probability
                    result.sort(key=lambda x: x[1], reverse=True)
                    classifyResp = []
                    for r in result:
                        classifyResp.append((params["intents"][r[0]], r[1]))
                    return_list.append(classifyResp)
                return return_list

            except Exception as e:
                return { "error" : repr( e ) }
Пример #22
0
def predict(parameters):
    """
    Predicts percentage of fraud
    :param parameters:
    :return:
    """
    # Parse json
    fields, values = parse_json_values(parameters, schema=SCHEMA)

    # Creates connection and gets the predicted value
    client = WatsonMachineLearningAPIClient(WML_CREDENTIALS)
    scoring_payload = {"input_data": [{"fields": fields, "values": [values]}]}
    predictions = client.deployments.score(deployment_url, scoring_payload)

    is_fraud = predictions["predictions"][0]["values"][0][0] == 1.0
    fraud_percentage = predictions["predictions"][0]["values"][0][1][1]

    return is_fraud, fraud_percentage
Пример #23
0
def predict():
    print(request)
    image = request.files["image"].read()
    image = Image.open(io.BytesIO(image))
    image = prepare_image(image)

    client = WatsonMachineLearningAPIClient(wml_credentials)
    ai_parms = { "wml_credentials" : wml_credentials, "model_endpoint_url" : "https://us-south.ml.cloud.ibm.com/v3/wml_instances/a8f286bb-6e41-4ddf-a97b-340ffcd6d33e/deployments/aab63322-d564-477b-b8ea-731a139bd547/online" }
    model_payload = { "values" : image }
    model_result = client.deployments.score( ai_parms["model_endpoint_url"], model_payload )
    data = model_result

    classes = ['ELEFANTE', 'GIRAFA', 'LEAO']
    index = data['values'][0][0].index(max(data['values'][0][0]))
    print(data)

    resposta = {
        "class": classes[index]
    }
    return resposta
    def __init__(self, wml_credentials, space_name=None, model_name=None, deployment_uid=None):
        """Initialize the interface object.
        If the deployment_uuid is specified (WS Cloud), the space_name and model_name are optional.
        If no deployment_uuid (CPD), specify both the model and space name.
        Will find UUID based on space and model id.

        Args:
            model_name (str): name of deployed model (CPD)
            space_name (str): name of deployment space (CPD)
            deployment_uuid (str): Deployment UUID (WS Cloud)
        """

        # Inputs
        self.wml_credentials = wml_credentials
        self.space_name = space_name
        self.model_name = model_name
        self.deployment_uid = deployment_uid
        self.max_run_time = 60  # seconds: Job will be deleted
        # self.time_limit = 600  # in milliseconds. timeLimit for DO model cancel
        #         self.inputs = inputs
        #         self.debug = debug
        #         self.debug_file_dir = debug_file_dir
        #         self.log_file_name = log_file_name

        # State:
        self.solve_status = None
        self.objective = None
        self.solve_details = {}
        self.outputs = {}
        self.run_time = 0  # Run-time of job in seconds
        self.job_details = None

        # Setup and connection to deployed model
        self.client = WatsonMachineLearningAPIClient(wml_credentials)

        if self.deployment_uid is None:
            space_id = [x['metadata']['id'] for x in self.client.spaces.get_details()['resources'] if
                        x['entity']['name'] == space_name][0]
            self.client.set.default_space(space_id)
            self.deployment_uid = [x['metadata']['guid'] for x in self.client.deployments.get_details()['resources'] if
                                   x['entity']['name'] == model_name][0]
Пример #25
0
def store(wml_model_name, run_uid, framework, framework_version,
          runtime_version, output_model_uid_path):
    from watson_machine_learning_client import WatsonMachineLearningAPIClient
    from pathlib import Path

    # retrieve credentials
    wml_url = getSecret("/app/secrets/wml_url")
    wml_instance_id = getSecret("/app/secrets/wml_instance_id")
    wml_apikey = getSecret("/app/secrets/wml_apikey")

    runtime_uid = framework + '_' + framework_version + '-py' + runtime_version
    runtime_type = framework + '_' + framework_version

    print("runtime_uid:", runtime_uid)
    print("runtime_type:", runtime_type)
    # set up the WML client
    wml_credentials = {
        "url": wml_url,
        "instance_id": wml_instance_id,
        "apikey": wml_apikey
    }
    client = WatsonMachineLearningAPIClient(wml_credentials)

    # store the model
    meta_props_tf = {
        client.repository.ModelMetaNames.NAME: wml_model_name,
        client.repository.ModelMetaNames.RUNTIME_UID: runtime_uid,
        client.repository.ModelMetaNames.TYPE: runtime_type
    }

    model_details = client.repository.store_model(run_uid,
                                                  meta_props=meta_props_tf)

    model_uid = client.repository.get_model_uid(model_details)
    print("model_uid: ", model_uid)

    Path(output_model_uid_path).parent.mkdir(parents=True, exist_ok=True)
    Path(output_model_uid_path).write_text(model_uid)

    import time
    time.sleep(120)
Пример #26
0
def store(wml_model_name, run_uid, framework, framework_version,
          runtime_version):
    from watson_machine_learning_client import WatsonMachineLearningAPIClient

    # retrieve credentials
    wml_url = getSecret("/app/secrets/wml_url")
    wml_instance_id = getSecret("/app/secrets/wml_instance_id")
    wml_apikey = getSecret("/app/secrets/wml_apikey")

    runtime_uid = framework + '_' + framework_version + '-py' + runtime_version
    runtime_type = framework + '_' + framework_version

    print("runtime_uid:", runtime_uid)
    print("runtime_type:", runtime_type)
    # set up the WML client
    wml_credentials = {
        "url": wml_url,
        "instance_id": wml_instance_id,
        "apikey": wml_apikey
    }
    client = WatsonMachineLearningAPIClient(wml_credentials)

    # store the model
    meta_props_tf = {
        client.repository.ModelMetaNames.NAME: wml_model_name,
        client.repository.ModelMetaNames.RUNTIME_UID: runtime_uid,
        client.repository.ModelMetaNames.TYPE: runtime_type
    }

    model_details = client.repository.store_model(run_uid,
                                                  meta_props=meta_props_tf)

    model_uid = client.repository.get_model_uid(model_details)
    print("model_uid: ", model_uid)

    with open("/tmp/model_uid", "w") as f:
        f.write(model_uid)
    f.close()

    import time
    time.sleep(120)
Пример #27
0
def init_content():
    set_config()
    print(FLAGS.from_cloud)
    global model_handler
    try:
        model_handler
    except NameError:
        model_handler = get_model_handler()
    if FLAGS.from_cloud:
        wml_credentials = SECRET_CONFIG["wml_credentials"]
        global client
        client = WatsonMachineLearningAPIClient(wml_credentials)
        scoring_endpoint = get_scoring_url()
        with open('data/word_index.json') as f:
            word_index = json.load(f)
        global scoring_params
        scoring_params = {
            "scoring_endpoint": scoring_endpoint,
            "intents": model_handler.data_handler.get_intents(),
            "word_index": word_index
        }
Пример #28
0
def upload():
    file = request.files['file']
    file_name = file.filename
    notebook = str(file.read().decode('UTF-8'))
    data = {'notebook': notebook}
    #if client:
    #my_document = db.create_document(data)
    #data['_id'] = my_document['_id']
    returnstring = ''
    if 'wnlc' in request.form:
        file = open('dlwords.txt', 'r')
        words = file.read().split()
        bag = []
        for w in words:
            bag.append(parsenb(notebook).count(w))
        bag = [bag]
        m = {'values': bag}
        endpoint = 'https://us-south.ml.cloud.ibm.com/v3/wml_instances/b07b565e-1dea-481a-8427-ca86b142271b/deployments/0030b4c9-a084-491a-98b0-dc06bc807733/online'
        wml_credentials = {
            "apikey": "ez9iIZqgjrmDch2Masvn-PPONgr5JkxYHoaHvSSdd2F-",
            "iam_apikey_description":
            "Auto-generated for key 88d33208-48dc-427e-a5af-b418c521795b",
            "iam_apikey_name": "wdp-writer",
            "iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Writer",
            "iam_serviceid_crn":
            "crn:v1:bluemix:public:iam-identity::a/2645902d64b933b58d39837cd4e91c09::serviceid:ServiceId-f37d1930-1057-4c75-8106-364f99dfc181",
            "instance_id": "b07b565e-1dea-481a-8427-ca86b142271b",
            "url": "https://us-south.ml.cloud.ibm.com"
        }
        wmlclient = WatsonMachineLearningAPIClient(wml_credentials)
        results = wmlclient.deployments.score(endpoint, m)
        topclass = results['values'][0][1][0]

        returnstring = 'your top framework is:<br>'
        if topclass == 0:
            returnstring = returnstring + 'pytorch<br><br>'
        elif topclass == 1:
            returnstring = returnstring + 'tensorflow<br><br>'
        returnstring = returnstring + str(results)
    return returnstring
Пример #29
0
    def __init__(self, wml_vcap):
        self.logger = logging.getLogger(self.__class__.__name__)
        self.logger.setLevel(logging.DEBUG)

        self.logger.info("Client authentication. Instance id: {}".format(
            wml_vcap["instance_id"]))
        self.client = WatsonMachineLearningAPIClient(wml_vcap.copy())
        self.deployment_list = self.client.deployments.get_details(
        )['resources']

        self.transaction_id = 'transaction-id-' + uuid.uuid4().hex
        self.logger.info("Transaction ID: {}".format(self.transaction_id))

        self.area_action_deployment_guid = ""
        self.satisfaction_deployment_guid = ""
        self.area_action_scoring_url = ""
        self.satisfaction_scoring_url = ""

        self.update_scoring_functions()

        self.neutral_templates = [
            "We’re sorry that you were unhappy with your experience with Cars4U. We will open a case to investigate the issue with <b>{} {}</b>. In the meantime, we’d like to offer you a <b>{}</b> on your next rental with us.",
            "We're very sorry for the trouble you experienced with Cars4U. We will open a case to investigate the issue with <b>{} {}</b>. In the meantime, we’d like to offer you a <b>{}</b> on your next rental with us.",
            "We sincerely apologize for this experience with Cars4U. We will open a case to investigate the issue with <b>{} {}</b>. In the meantime, we’d like to offer you a <b>{}</b> on your next rental with us.",
            "I am very disappointed to hear about your experience with Cars4U. We will open a case to investigate the issue with <b>{} {}</b>. In the meantime, we’d like to offer you a <b>{}</b> on your next rental with us."
        ]

        self.negative_templates = [
            "We’re sorry that you were unhappy with your experience with Cars4U. We will open a case to investigate the issue with <b>{} {}</b>. Our customer agent will contact you shortly.",
            "We're very sorry for the trouble you experienced with Cars4U. We will open a case to investigate the issue with <b>{} {}</b>. Our customer agent will contact you shortly.",
            "We sincerely apologize for this experience with Cars4U. We will open a case to investigate the issue with <b>{} {}</b>. Our customer agent will contact you shortly.",
            "I am very disappointed to hear about your experience with Cars4U. We will open a case to investigate the issue with <b>{} {}</b>. Our customer agent will contact you shortly."
        ]

        self.positive_templates = [
            "We are very happy to have provided you with such a positive experience!",
            "We are glad to hear you had such a great experience! ",
            "We appreciate your positive review about your recent experience with us!"
        ]
Пример #30
0
    def __init__(self, url, api_key, instance_id):
        """
        Initializer

        """

        # verify that the specified WML service supports
        # the deep learning functionality we want to use
        # Current list (as of 01/2020): 'us-south' and 'eu-de'
        h = urlparse(url).hostname
        if len([sh for sh in ['us-south', 'eu-de'] if h.startswith(sh)]) == 0:
            raise WMLWrapperError(
                'Error. WML service location "{}" does not '
                'provide the required training functionality.'.format(h))

        try:
            self.client = WatsonMachineLearningAPIClient({
                'url':
                url,
                'apikey':
                api_key,
                'instance_id':
                instance_id
            })
            self.client.service_instance.get_details()
        except ApiRequestFailure as arf:
            debug('Exception: {}'.format(arf))
            p = WMLWrapper.parse_WML_ApiRequestFailure(arf)
            if p:
                msg = p['error_message']
            else:
                msg = arf.error_msg
            raise WMLWrapperError(
                'Error. Cannot connect to WML service: {}'.format(msg))
        except Exception as ex:
            debug('Exception type: {}'.format(type(ex)))
            debug('Exception: {}'.format(ex))
            raise WMLWrapperError(ex)