def main():
    # this will avoid looped imports and will register deprecated endpoints in the blueprint
    import label_studio.deprecated

    input_args = parse_input_args()
    app = create_app(LabelStudioConfig(input_args=input_args))

    # setup logging level
    if input_args.log_level:
        logging.root.setLevel(input_args.log_level)

    # On `init` command, create directory args.project_name with initial project state and exit
    if input_args.command == 'init':
        Project.create_project_dir(input_args.project_name, input_args)
        return

    elif input_args.command == 'start':

        # If `start --init` option is specified, do the same as with `init` command, but continue to run app
        if input_args.init:
            Project.create_project_dir(input_args.project_name, input_args)

        if not os.path.exists(
                Project.get_project_dir(input_args.project_name, input_args)):
            raise FileNotFoundError(
                'Project directory "{pdir}" not found. '
                'Did you miss create it first with `label-studio init {pdir}` ?'
                .format(pdir=Project.get_project_dir(input_args.project_name,
                                                     input_args)))

    # On `start` command, launch browser if --no-browser is not specified and start label studio server
    if input_args.command == 'start':
        import label_studio.utils.functions
        import label_studio.utils.auth
        config = Project.get_config(input_args.project_name, input_args)

        # set username and password
        label_studio.utils.auth.USERNAME = input_args.username or \
            config.get('username') or label_studio.utils.auth.USERNAME
        label_studio.utils.auth.PASSWORD = input_args.password or config.get(
            'password', '')

        # set host name
        host = input_args.host or config.get('host', 'localhost')
        port = input_args.port or config.get('port', 8080)
        server_host = 'localhost' if host == 'localhost' else '0.0.0.0'  # web server host

        # ssl certificate and key
        cert_file = input_args.cert_file or config.get('cert')
        key_file = input_args.key_file or config.get('key')
        ssl_context = None
        if cert_file and key_file:
            config['protocol'] = 'https://'
            ssl_context = (cert_file, key_file)

        # check port is busy
        if not input_args.debug and check_port_in_use('localhost', port):
            old_port = port
            port = int(port) + 1
            print('\n*** WARNING! ***\n* Port ' + str(old_port) +
                  ' is in use.\n' + '* Trying to start at ' + str(port) +
                  '\n****************\n')

        # external hostname is used for data import paths, they must be absolute always,
        # otherwise machine learning backends couldn't access them
        set_web_protocol(input_args.protocol
                         or config.get('protocol', 'http://'))
        external_hostname = get_web_protocol() + host.replace(
            '0.0.0.0', 'localhost')
        if host in ['0.0.0.0', 'localhost', '127.0.0.1']:
            external_hostname += ':' + str(port)
        set_external_hostname(external_hostname)

        start_browser('http://localhost:' + str(port), input_args.no_browser)
        if input_args.use_gevent:
            app.debug = input_args.debug
            ssl_args = {
                'keyfile': key_file,
                'certfile': cert_file
            } if ssl_context else {}
            http_server = WSGIServer((server_host, port),
                                     app,
                                     log=app.logger,
                                     **ssl_args)
            http_server.serve_forever()
        else:
            app.run(host=server_host,
                    port=port,
                    debug=input_args.debug,
                    ssl_context=ssl_context)

    # On `start-multi-session` command, server creates one project per each browser sessions
    elif input_args.command == 'start-multi-session':
        server_host = input_args.host or '0.0.0.0'
        port = input_args.port or 8080

        if input_args.use_gevent:
            app.debug = input_args.debug
            http_server = WSGIServer((server_host, port), app, log=app.logger)
            http_server.serve_forever()
        else:
            app.run(host=server_host, port=port, debug=input_args.debug)
Exemple #2
0
def main():
    # this will avoid looped imports and will register deprecated endpoints in the blueprint
    import label_studio.deprecated

    input_args = parse_input_args()
    app = create_app(LabelStudioConfig(input_args=input_args))

    # setup logging level
    if input_args.log_level:
        logging.root.setLevel(input_args.log_level)

    # On `init` command, create directory args.project_name with initial project state and exit
    if input_args.command == "init":
        Project.create_project_dir(input_args.project_name, input_args)
        return

    elif input_args.command == "start":

        # If `start --init` option is specified, do the same as with `init` command, but continue to run app
        if input_args.init:
            Project.create_project_dir(input_args.project_name, input_args)

        if not os.path.exists(
                Project.get_project_dir(input_args.project_name, input_args)):
            raise FileNotFoundError(
                'Project directory "{pdir}" not found. '
                "Did you miss create it first with `label-studio init {pdir}` ?"
                .format(pdir=Project.get_project_dir(input_args.project_name,
                                                     input_args)))

    # On `start` command, launch browser if --no-browser is not specified and start label studio server
    if input_args.command == "start":
        import label_studio.utils.functions
        import label_studio.utils.auth

        config = Project.get_config(input_args.project_name, input_args)

        # set username and password
        label_studio.utils.auth.USERNAME = (input_args.username
                                            or config.get("username") or
                                            label_studio.utils.auth.USERNAME)
        label_studio.utils.auth.PASSWORD = input_args.password or config.get(
            "password", "")

        # set host name
        host = input_args.host or config.get("host", "localhost")
        port = input_args.port or config.get("port", 8080)
        server_host = ("localhost" if host == "localhost" else "0.0.0.0"
                       )  # web server host

        # ssl certificate and key
        cert_file = input_args.cert_file or config.get("cert")
        key_file = input_args.key_file or config.get("key")
        ssl_context = None
        if cert_file and key_file:
            config["protocol"] = "https://"
            ssl_context = (cert_file, key_file)

        # check port is busy
        if not input_args.debug and check_port_in_use("localhost", port):
            old_port = port
            port = int(port) + 1
            print("\n*** WARNING! ***\n* Port " + str(old_port) +
                  " is in use.\n" + "* Trying to start at " + str(port) +
                  "\n****************\n")

        # external hostname is used for data import paths, they must be absolute always,
        # otherwise machine learning backends couldn't access them
        set_web_protocol(input_args.protocol
                         or config.get("protocol", "http://"))
        external_hostname = get_web_protocol() + host.replace(
            "0.0.0.0", "localhost")
        if host in ["0.0.0.0", "localhost", "127.0.0.1"]:
            external_hostname += ":" + str(port)
        set_external_hostname(external_hostname)

        start_browser("http://localhost:" + str(port), input_args.no_browser)
        if input_args.use_gevent:
            app.debug = input_args.debug
            ssl_args = ({
                "keyfile": key_file,
                "certfile": cert_file
            } if ssl_context else {})
            http_server = WSGIServer((server_host, port),
                                     app,
                                     log=app.logger,
                                     **ssl_args)
            http_server.serve_forever()
        else:
            app.run(
                host=server_host,
                port=port,
                debug=input_args.debug,
                ssl_context=ssl_context,
            )

    # On `start-multi-session` command, server creates one project per each browser sessions
    elif input_args.command == "start-multi-session":
        server_host = input_args.host or "0.0.0.0"
        port = input_args.port or 8080

        if input_args.use_gevent:
            app.debug = input_args.debug
            http_server = WSGIServer((server_host, port), app, log=app.logger)
            http_server.serve_forever()
        else:
            app.run(host=server_host, port=port, debug=input_args.debug)