Esempio n. 1
0
def main(settings, log_config, config, verbose):
    """Train, test or run a config file's model."""
    # Initialize logging before any library code so that mllp can log stuff
    if log_config:
        settings.logger = logutil.init_logging(log_config, verbose=verbose)
    else:
        settings.logger = logutil.init_logging(verbose=verbose)
    settings.verbose = verbose
    settings.conf_file = config
Esempio n. 2
0
def main():
    fullCmdArguments = sys.argv
    argumentList = fullCmdArguments[1:]
    unixOptions = "hvtrpac:l:g:"
    gnuOptions = [
        "help",
        "version",
        "train",
        "retest",
        "predict",
        "api",
        "config=",
        "logconfig=",
        "generateraml=",
    ]
    try:
        arguments, values = getopt.getopt(argumentList, unixOptions,
                                          gnuOptions)
    except getopt.error as err:
        # output error, and return with an error code
        print(str(err), file=sys.stderr)
        sys.exit(2)
    # evaluate given options
    cmd = None
    conf = None
    logger = None
    raml_ds = None
    for currentArgument, currentValue in arguments:
        if currentArgument in ("-c", "--config"):
            conf = lp.get_validated_config(currentValue)
        elif currentArgument in ("-l", "--logconfig"):
            logger = logutil.init_logging(currentValue)
        elif currentArgument in ("-t", "--train"):
            cmd = "train"
        elif currentArgument in ("-r", "--retest"):
            cmd = "retest"
        elif currentArgument in ("-p", "--predict"):
            cmd = "predict"
        elif currentArgument in ("-a", "--api"):
            cmd = "api"
        elif currentArgument in ("-g", "--generateraml"):
            cmd = "genraml"
            raml_ds = currentValue
        elif currentArgument in ("-h", "--help"):
            print(HELP_STRING, file=sys.stderr)
            exit(0)
        elif currentArgument in ("-v", "--version"):
            print("ML Launchpad version " + lp.__version__, file=sys.stderr)
            exit(0)

    if cmd is None:
        print("\nNo command given.", file=sys.stderr)
        print(HELP_STRING, file=sys.stderr)
        exit(1)

    logger = logger or logutil.init_logging()
    conf = conf or lp.get_validated_config()
    if cmd == "train":
        model, metrics = lp.train_model(conf)
    elif cmd == "retest":
        _ = lp.retest(conf)
    elif cmd == "predict":
        # TODO decide: get batch args from config? Don't support arguments?
        _ = lp.predict(conf, arg_dict={})
    elif cmd == "api":
        logger.warning(
            "Starting Flask debug server. In production, please "
            "use a WSGI server, e.g. "
            "'export LAUNCHPAD_CFG=addition_cfg.yml'"
            "'gunicorn -w 4 -b 127.0.0.1:5000 mllaunchpad.wsgi:application'")
        app = Flask(__name__)
        ModelApi(conf, app)
        app.run(debug=True)
    elif cmd == "genraml":
        print(generate_raml(conf, data_source_name=raml_ds))
Esempio n. 3
0
Example:
    `$ gunicorn -w 4 --bind 127.0.0.1:5000 mllaunchpad.wsgi:application`
"""

# Stdlib imports
import logging
from typing import Dict, Optional

# Third-party imports
from flask import Flask

# Project imports
from mllaunchpad import config, logutil
from mllaunchpad.api import ModelApi

logutil.init_logging()
logger = logging.getLogger(__name__)

# In order to be able to generate API docs automatically, it is unfortunately
# necessary to wrap the preparatory code in a try:except: statement.
conf: Optional[Dict]
try:
    conf = config.get_validated_config()
except FileNotFoundError:
    logger.error(
        "Config file could not be loaded. Starting the Flask application "
        "will fail.")
    conf = None

if conf:
    # if you change the name of the application variable, you need to
Esempio n. 4
0
def test_init_logging():
    _ = lu.init_logging()
Esempio n. 5
0
def test_init_logging_file(dc):
    mo = mock.mock_open(read_data=logging_config)
    with mock.patch("builtins.open", mo, create=True):
        _ = lu.init_logging("some_file.yml")
    mo.assert_called_once()
    dc.assert_called_once()
Esempio n. 6
0
def test_init_logging_verbose():
    _ = lu.init_logging(verbose=True)