Exemple #1
0
def print_model(model, language_version):
    # create hash of requirements
    with open("flaskr/V/Models/" + model + "/requirements.txt", 'rb') as fd:
        m = requirement.create_hash_of_requirements(fd.read(), 'r', language_version)
    x = subprocess.run(
        'cd flaskr/VENV/r/ENV-' + m.hexdigest() + '; ../../../interpreters/r/R-' + language_version + '/bin/Rscript ../../../additional_scripts/PRINTMODEL.r ' + model,
        stdout=subprocess.PIPE, shell=True)
    return x.stdout
Exemple #2
0
def print_model(model, language_version):
    # creating hash of the requirements
    with open("flaskr/V/Models/" + model + "/requirements.txt", 'rb') as fd:
        m = requirement.create_hash_of_requirements(fd.read(), 'python',
                                                    language_version)

    # printing model in its environment
    x = subprocess.run([
        "flaskr/VENV/python/ENV-" + m.hexdigest() + "/bin/python",
        "flaskr/additional_scripts/PRINTMODEL.py", model
    ],
                       stdout=subprocess.PIPE)

    return x.stdout
Exemple #3
0
def predict(model, language_version, date, type, is_hash, hash, target):
    """Function makes a prediction using model written in Python in its virtual evironment.

    Parameters
    ----------
    model : FileStorage
        model in binary wrapped in FileStorage
    language_version : str
        version of the language
    date : str
        timestamp
    type : str
        type of the prediction
    is_hash : bool
        flag if dataset provided previously was hash
    hash : bool
        hash of the dataset
    target : str
        name of the target column

    Returns
    -------
    """

    # creating hash of requirements
    with open("flaskr/V/Models/" + model + "/requirements.txt", 'rb') as fd:
        m = requirement.create_hash_of_requirements(fd.read(), 'python',
                                                    language_version)

    # running script "PREDICT.py" in the virtual environment
    if is_hash == 1:
        # case when dataset provided previously was hash
        x = subprocess.run([
            "flaskr/VENV/python/ENV-" + m.hexdigest() + "/bin/python",
            "flaskr/additional_scripts/PREDICT.py", model, date, type,
            str(is_hash), hash, target
        ],
                           stdout=subprocess.PIPE)
    else:
        # case when dataset provided previously was csv
        x = subprocess.run([
            "flaskr/VENV/python/ENV-" + m.hexdigest() + "/bin/python",
            "flaskr/additional_scripts/PREDICT.py", model, date, type,
            str(is_hash)
        ],
                           stdout=subprocess.PIPE)
Exemple #4
0
def create_environment(requirements_path, language_version, timestamp):
    # creating hash of the requirements
    m = None
    with open(requirements_path, 'rb') as fd:
        m = requirement.create_hash_of_requirements(fd.read(), 'r', language_version)

    # checking if requested environment already exists
    if os.path.exists("flaskr/VENV/r/ENV-" + m.hexdigest()):
        return

    os.mkdir("flaskr/VENV/r/ENV-" + m.hexdigest())

    # create virtual environment
    subprocess.run(['flaskr/interpreters/r/R-' + language_version + '/bin/Rscript',
                    'flaskr/additional_scripts/CREATEENVIRONMENT.r', m.hexdigest(), requirements_path, timestamp])

    os.remove('flaskr/tmp/status_' + timestamp + '.txt')
Exemple #5
0
def audit(model_name, dataset, is_hash, target, data_name, data_desc, measure, user, language_version, date):
    # creating hash of requirements
    with open("flaskr/V/Models/" + model_name + "/requirements.txt", 'rb') as fd:
        m = requirement.create_hash_of_requirements(fd.read(), 'r', language_version)

    if is_hash == '0':
        is_hash = False
    else:
        is_hash = True

    hash, exists, alias = data.save_data(dataset, data_name, data_desc, user, is_hash)

    check = database.check_audit(model_name, hash, measure)

    if check:
        x = subprocess.run(
            'cd flaskr/VENV/r/ENV-' + m.hexdigest() + '; ../../../interpreters/r/R-' + language_version + '/bin/Rscript ../../../additional_scripts/AUDIT.r ' + model_name + ' ' + hash + ' ' + target + ' ' + measure + ' ' + str(
                date), stdout=subprocess.PIPE, shell=True)

    return check, hash, exists, alias
Exemple #6
0
def explain_func(model, func):
    """
    Returns fields of the explainer. Only for scikit-learn models.
    """

    # creating hash of requirements
    with current_app.open_resource("V/Models/" + model +
                                   "/requirements.txt") as fd:
        m = requirement.create_hash_of_requirements(fd.read())

    # path to model
    model = "flaskr/V/Models/" + model + "/model"

    # running script "explain.r" in virtual environment
    x = subprocess.run([
        "Rscript", "flaskr/explain.r", model,
        "flaskr/VENV/python/ENV-" + m.hexdigest(), "flaskr/X", "flaskr/Y", func
    ],
                       stdout=subprocess.PIPE)

    return x.stdout
Exemple #7
0
def create_environment(requirements_path, language_version, timestamp):
    # creating hash of the requirements
    m = None
    with open(requirements_path, 'rb') as fd:
        m = requirement.create_hash_of_requirements(fd.read(), 'python',
                                                    language_version)

    # checking if requested environment already exists
    if os.path.exists("flaskr/VENV/python/ENV-" + m.hexdigest()):
        return

    # create virtual environment
    subprocess.run([
        "virtualenv", "--python=flaskr/interpreters/python/Python-" +
        language_version + '/bin/python3',
        "flaskr/VENV/python/ENV-" + m.hexdigest()
    ])

    # install required packages
    subprocess.run([
        "flaskr/VENV/python/ENV-" + m.hexdigest() + "/bin/pip", "install",
        "-r", requirements_path
    ])
Exemple #8
0
def predict(model, language_version, date, type, is_hash, hash, target):
    """Make a prediction with R model in the base

    Parameters
    ----------
    model : FileStorage
        model in binary (RDS) wrapped in FileStorage
    language_version : str
        version of the language
    date : str
        timestamp
    type : str
        type of the prediction
    is_hash : bool
        flag if dataset provided previously was hash
    hash : str
        hash of the dataset
    target : str
        name of the target column

    Returns
    -------
    """
    # creating hash of requirements
    with open("flaskr/V/Models/" + model + "/requirements.txt", 'rb') as fd:
        m = requirement.create_hash_of_requirements(fd.read(), 'r', language_version)
    print(m.hexdigest())
    # running script "PREDICT.py" in the virtual environment
    if is_hash == 1:
        x = subprocess.run(
            'cd flaskr/VENV/r/ENV-' + m.hexdigest() + '; ../../../interpreters/r/R-' + language_version + '/bin/Rscript ../../../additional_scripts/PREDICT.r ' + model + ' ' + date + ' ' + type + ' ' + str(
                is_hash) + ' ' + hash + ' ' + target, stdout=subprocess.PIPE, shell=True)
    else:
        x = subprocess.run(
            'cd flaskr/VENV/r/ENV-' + m.hexdigest() + '; ../../../interpreters/r/R-' + language_version + '/bin/Rscript ../../../additional_scripts/PREDICT.r ' + model + ' ' + date + ' ' + type + ' ' + str(
                is_hash), stdout=subprocess.PIPE, shell=True)
Exemple #9
0
def audit(model_name, dataset, is_hash, target, data_name, data_desc, measure,
          user, language_version, date):
    """Function to audit the Python model in the base

    Parameters
    ----------
    model_name : str
        name of the model
    dataset : str
        dataset in the csv format or hash of already uploaded
    is_hash : bool
        flag if dataset is a hash
    target : str
        name of the target column
    data_name : str
        name of the dataset
    data_desc : str
        description of the dataset
    measure : str
        name of the measure used in auditting
    user : str
        user's name
    language_version : str
        version of the language
    date : str
        timestamp

    Returns
    -------
    bool
        flag if audit has not exiested yet
    str
        hash of the dataset
    bool
        flag if dataset existed
    bool
        flag if alias for dataset was added
    """
    # creating hash of requirements
    with open("flaskr/V/Models/" + model_name + "/requirements.txt",
              'rb') as fd:
        m = requirement.create_hash_of_requirements(fd.read(), 'python',
                                                    language_version)

    # translating from str to bool
    if is_hash == '0':
        is_hash = False
    else:
        is_hash = True

    # saving metadata about the dataset
    hash, exists, alias = data.save_data(dataset, data_name, data_desc, user,
                                         is_hash)

    # check if audit has existed yet
    check = database.check_audit(model_name, hash, measure)

    if check:
        # case when audit has not existed yet
        x = subprocess.run([
            "flaskr/VENV/python/ENV-" + m.hexdigest() + "/bin/python",
            "flaskr/additional_scripts/AUDIT.py", model_name, hash, target,
            measure, date
        ],
                           stdout=subprocess.PIPE)

    return check, hash, exists, alias