How to use mlrun - 10 common examples

To help you get started, we’ve selected a few mlrun examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mlrun / mlrun / tests / test_config.py View on Github external
def test_file(config):
    ns = 'banana'
    config_path = create_yaml_config(namespace=ns)

    with patch_env({mlconf.env_file_key: config_path}):
        mlconf.config.reload()

    assert config.namespace == ns, 'not populated from file'
github mlrun / mlrun / tests / test_config.py View on Github external
def config():
    old = mlconf.config
    mlconf.config = mlconf.Config(mlconf.default_config)
    mlconf._loaded = False

    yield mlconf.config

    mlconf.config = old
    mlconf._loaded = False
github mlrun / mlrun / mlrun / db / filedb.py View on Github external
def del_artifacts(self, name='', project='', tag='', labels=None):
        labels = [] if labels is None else labels
        tag = tag or 'latest'
        filepath = self._filepath(artifacts_dir, project, tag=tag)

        if isinstance(labels, str):
            labels = labels.split(',')
        if tag == '*':
            mask = '**/*' + name
            if name:
                mask += '*'
        else:
            mask = '**/*'

        for artifact, p in self._load_list(filepath, mask):
            if (name == '' or name == get_in(artifact, 'key', '')) and match_labels(
                get_in(artifact, 'labels', {}), labels
            ):

                self._safe_del(p)
github mlrun / mlrun / mlrun / run.py View on Github external
if kind is None or kind in ['', 'Function']:
        raise ValueError('please specify the function kind')
    elif kind in ['local']:
        r = LocalRuntime()
    elif kind in RuntimeKinds.all():
        r = get_runtime_class(kind)()
    else:
        raise ValueError('unsupported runtime ({})'.format(kind))

    name, spec, code = build_file(filename, name=name)

    if not name:
        raise ValueError('name must be specified')
    h = get_in(spec, 'spec.handler', '').split(':')
    r.handler = h[0] if len(h) <= 1 else h[1]
    r.metadata = get_in(spec, 'spec.metadata')
    r.metadata.name = name
    r.spec.image = get_in(spec, 'spec.image', image)
    build = r.spec.build
    build.code_origin = code_origin
    build.base_image = get_in(spec, 'spec.build.baseImage')
    build.commands = get_in(spec, 'spec.build.commands')
    if embed_code:
        build.functionSourceCode = get_in(spec, 'spec.build.functionSourceCode')
    else:
        if code_output:
            r.spec.command = code_output
        else:
            r.spec.command = filename

    build.image = get_in(spec, 'spec.build.image')
    build.secret = get_in(spec, 'spec.build.secret')
github mlrun / mlrun / mlrun / run.py View on Github external
def parse_command(runtime, url):
    idx = url.find('#')
    if idx > -1:
        update_in(runtime, 'spec.image', url[:idx])
        url = url[idx + 1 :]

    if url:
        arg_list = url.split()
        update_in(runtime, 'spec.command', arg_list[0])
        update_in(runtime, 'spec.args', arg_list[1:])
github mlrun / mlrun / mlrun / scheduler.py View on Github external
def _loop(self):
        while True:
            now = datetime.now()
            logger.info('scheduler loop at %s', now)
            with self.lock:
                for job in self:
                    if job.next <= now:
                        logger.info('scheduling job')
                        self.pool.submit(job.runtime.run, *job.args, **job.kw)
                        job.advance()
            sleep(self.sleep_time_sec)
github mlrun / mlrun / mlrun / __main__.py View on Github external
if kfp:
            print('Runtime:')
            pprint(runtime)
        func = new_function(runtime=runtime)
    elif func_url.startswith('db://'):
        func_url = func_url[5:]
        func = import_function(func_url)
    elif func_url:
        func_url = 'function.yaml' if func_url == '.' else func_url
        func = import_function(func_url)
    else:
        print('please specify the function path or url')
        exit(1)

    meta = func.metadata
    meta.project = project or meta.project or mlconf.default_project
    meta.name = name or meta.name
    meta.tag = tag or meta.tag

    b = func.spec.build
    if func.kind not in ['', 'local']:
        b.base_image = base_image or b.base_image
        b.commands = list(command) or b.commands
        b.image = image or b.image
        b.secret = secret_name or b.secret

    if source.endswith('.py'):
        if not path.isfile(source):
            print('source file doesnt exist ({})'.format(source))
            exit(1)
        with open(source) as fp:
            body = fp.read()
github mlrun / mlrun / mlrun / runtimes / local.py View on Github external
args_list = get_func_arg(handler, runobj, context)

    stdout = _DupStdout()
    err = ''
    val = None
    old_dir = os.getcwd()
    with redirect_stdout(stdout):
        context.set_logger_stream(stdout)
        try:
            if cwd:
                os.chdir(cwd)
            val = handler(*args_list)
            context.set_state('completed', commit=False)
        except Exception as e:
            err = str(e)
            logger.error(traceback.format_exc())
            context.set_state(error=err, commit=False)
            logger.set_logger_level(old_level)

    if cwd:
        os.chdir(old_dir)
    context.set_logger_stream(sys.stdout)
    if val:
        context.log_result('return', val)
    context.commit()
    logger.set_logger_level(old_level)
    return stdout.buf.getvalue(), err
github mlrun / mlrun / mlrun / httpd / files.py View on Github external
@app.route('/api/files', methods=['GET'])
@app.catch_err
def get_files():
    schema = request.args.get('schema', '')
    path = request.args.get('path', '')
    size = int(request.args.get('size', '0'))
    offset = int(request.args.get('offset', '0'))

    _, filename = path.split(path)

    path = get_obj_path(schema, path)
    if not path:
        return app.json_error(
            HTTPStatus.NOT_FOUND, path=path,
            err='illegal path prefix or schema')

    try:
github mlrun / mlrun / mlrun / httpd / submit.py View on Github external
@app.route('/api/submit', methods=['POST'])
@app.route('/api/submit/', methods=['POST'])
@app.route('/api/submit_job', methods=['POST'])
@app.route('/api/submit_job/', methods=['POST'])
@app.catch_err
def submit_job():
    try:
        data: dict = request.get_json(force=True)
    except ValueError:
        return app.json_error(HTTPStatus.BAD_REQUEST, reason='bad JSON body')

    app.logger.info('submit_job: {}'.format(data))
    return _submit.submit(data)