args.py 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2017 VisualDL Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =======================================================================
走神的阿圆's avatar
走神的阿圆 已提交
15
import socket
16
import sys
17 18
from argparse import ArgumentParser

P
Peter Pan 已提交
19
from visualdl import __version__
20 21
from visualdl.server.log import init_logger
from visualdl.server.log import logger
22

走神的阿圆's avatar
走神的阿圆 已提交
23
default_host = None
24 25 26
default_port = 8040
default_cache_timeout = 20
default_public_path = '/app'
27
default_product = 'normal'
28

P
Peter Pan 已提交
29 30
support_themes = ['light', 'dark']

31 32 33 34 35 36 37 38 39 40 41

class DefaultArgs(object):
    def __init__(self, args):
        self.logdir = args.get('logdir')
        self.host = args.get('host', default_host)
        self.port = args.get('port', default_port)
        self.cache_timeout = args.get('cache_timeout', default_cache_timeout)
        self.language = args.get('language')
        self.public_path = args.get('public_path')
        self.api_only = args.get('api_only', False)
        self.open_browser = args.get('open_browser', False)
走神的阿圆's avatar
走神的阿圆 已提交
42
        self.model = args.get('model', '')
43 44
        self.product = args.get('product', default_product)
        self.telemetry = args.get('telemetry', True)
P
Peter Pan 已提交
45
        self.theme = args.get('theme', None)
走神的阿圆's avatar
走神的阿圆 已提交
46 47
        self.dest = args.get('dest', '')
        self.behavior = args.get('behavior', '')
48
        self.component_tabs = args.get('component_tabs', None)
49 50


P
Peter Pan 已提交
51 52 53 54 55 56 57 58 59 60
def get_host(host=default_host, port=default_port):
    if not host:
        host = socket.getfqdn()
        try:
            socket.create_connection((host, port), timeout=1)
        except socket.error:
            host = 'localhost'
    return host


61 62 63 64 65 66 67 68 69 70 71
def validate_args(args):
    # if not in API mode, public path cannot be set to root path
    if not args.api_only and args.public_path == '/':
        logger.error('Public path cannot be set to root path.')
        sys.exit(-1)

    # public path must start with `/`
    if args.public_path is not None and not args.public_path.startswith('/'):
        logger.error('Public path should always start with a `/`.')
        sys.exit(-1)

P
Peter Pan 已提交
72 73 74 75 76
    # theme not support
    if args.theme is not None and args.theme not in support_themes:
        logger.error('Theme {} is not support.'.format(args.theme))
        sys.exit(-1)

77 78 79 80
    # input unsupported component tab name
    supported_tabs = [
        'scalar', 'image', 'text', 'embeddings', 'audio', 'histogram',
        'hyper_parameters', 'static_graph', 'dynamic_graph', 'pr_curve',
81 82
        'roc_curve', 'profiler', 'x2paddle', 'fastdeploy_server',
        'fastdeploy_client'
83 84 85 86 87 88 89 90 91
    ]
    if args.component_tabs is not None:
        for component_tab in args.component_tabs:
            if component_tab not in supported_tabs:
                logger.error(
                    'Component_tab {} is not support. Please choose tabs in {}'
                    .format(component_tab, supported_tabs))
                sys.exit(-1)

92 93 94 95 96 97 98 99 100 101 102 103

def format_args(args):
    # set default public path according to API mode option
    if args.public_path is None:
        args.public_path = '' if args.api_only else default_public_path
    else:
        args.public_path = args.public_path.rstrip('/')

    # don't open browser in API mode
    if args.api_only:
        args.open_browser = False

P
Peter Pan 已提交
104
    # set host to localhost if host is not set
走神的阿圆's avatar
走神的阿圆 已提交
105 106 107
    if not args.host:
        args.host = get_host(args.host, args.port)

108 109 110 111 112
    return args


class ParseArgs(object):
    def __init__(self, **kwargs):
P
Peter Pan 已提交
113 114 115
        args = DefaultArgs(kwargs)
        validate_args(args)
        args = format_args(args)
116 117 118 119 120 121 122 123 124

        self.logdir = args.logdir
        self.host = args.host
        self.port = args.port
        self.cache_timeout = args.cache_timeout
        self.language = args.language
        self.public_path = args.public_path
        self.api_only = args.api_only
        self.open_browser = args.open_browser
125
        self.model = args.model
走神的阿圆's avatar
走神的阿圆 已提交
126
        self.product = args.product
127
        self.telemetry = args.telemetry
P
Peter Pan 已提交
128
        self.theme = args.theme
走神的阿圆's avatar
走神的阿圆 已提交
129 130
        self.dest = args.dest
        self.behavior = args.behavior
131
        self.component_tabs = args.component_tabs
132 133 134 135 136 137


def parse_args():
    """
    :return:
    """
P
Peter Pan 已提交
138 139 140 141 142 143
    parser = ArgumentParser(
        prog="VisualDL",
        description="VisualDL, a tool to visualize deep learning.",
        epilog="For more information: https://github.com/PaddlePaddle/VisualDL"
    )

144
    parser.add_argument(
145 146 147 148
        "--logdir", action="store", nargs="+", help="log file directory")

    parser.add_argument(
        "--component_tabs",
149 150
        action="store",
        nargs="+",
151 152
        help="component tabs presented in html page.")

153 154 155 156 157 158
    parser.add_argument(
        "-p",
        "--port",
        type=int,
        default=default_port,
        action="store",
P
Peter Pan 已提交
159
        help="port of %(prog)s board")
160 161 162 163 164 165
    parser.add_argument(
        "-t",
        "--host",
        type=str,
        default=default_host,
        action="store",
P
Peter Pan 已提交
166
        help="bind %(prog)s board to ip/host")
167 168 169 170 171 172
    parser.add_argument(
        "--model",
        type=str,
        action="store",
        default="",
        help="model file path")
173
    parser.add_argument(
P
Peter Pan 已提交
174
        "--cache-timeout",
175 176 177 178
        action="store",
        dest="cache_timeout",
        type=float,
        default=default_cache_timeout,
179 180
        help="memory cache timeout duration in seconds (default: %(default)s)",
    )
181 182 183 184 185 186
    parser.add_argument(
        "-L",
        "--language",
        type=str,
        action="store",
        default=None,
P
Peter Pan 已提交
187
        help="specify the default language")
188 189 190 191
    parser.add_argument(
        "--public-path",
        type=str,
        action="store",
P
Peter Pan 已提交
192
        dest="public_path",
193
        default=None,
194
        help="set public path")
195 196 197
    parser.add_argument(
        "--api-only",
        action="store_true",
P
Peter Pan 已提交
198
        dest="api_only",
199
        default=False,
200
        help="serve api only")
P
Peter Pan 已提交
201 202 203 204 205
    parser.add_argument(
        "--verbose",
        "-v",
        action="count",
        default=0,
206
        help="set log level, use -vvv... to get more information")
P
Peter Pan 已提交
207 208 209
    parser.add_argument(
        "--version",
        action="version",
210
        version="%(prog)s {}".format(__version__))
走神的阿圆's avatar
走神的阿圆 已提交
211 212 213 214
    parser.add_argument(
        "--product",
        type=str,
        action="store",
215
        default=default_product,
走神的阿圆's avatar
走神的阿圆 已提交
216
        help="specify the product")
217 218 219 220 221
    parser.add_argument(
        "--disable-telemetry",
        action="store_false",
        dest="telemetry",
        default=True,
222
        help="disable telemetry")
P
Peter Pan 已提交
223 224 225 226 227 228
    parser.add_argument(
        "--theme",
        action="store",
        dest="theme",
        default=None,
        choices=support_themes,
229 230 231
        help="set theme")
    parser.add_argument('dest', nargs='?', help='set destination for log')
    parser.add_argument("behavior", nargs='?')
232 233 234

    args = parser.parse_args()

P
Peter Pan 已提交
235 236 237
    init_logger(args.verbose)

    return vars(args)