visualDL 5.2 KB
Newer Older
Y
Yan Chunwei 已提交
1 2
#! /user/bin/env python

Q
Qiao Longfei 已提交
3 4 5 6 7
import json
import os
import sys
from optparse import OptionParser

8 9
import visualdl.mock.data as mock_data
import visualdl.mock.tags as mock_tags
S
superjom 已提交
10 11
from flask import (Flask, Response, redirect, request, send_file,
                   send_from_directory)
Q
Qiao Longfei 已提交
12

13 14 15
import visualdl.server.graph as vdl_graph
from visualdl.python.storage import LogReader
from visualdl.server import lib
Y
Yan Chunwei 已提交
16 17 18
from visualdl.server import log as logger
from visualdl.server.mock import data as mock_data
from visualdl.server.mock import data as mock_tags
Q
Qiao Longfei 已提交
19 20

app = Flask(__name__, static_url_path="")
S
superjom 已提交
21 22
# set static expires in a short time to reduce browser's memory usage.
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 30
Q
Qiao Longfei 已提交
23 24 25 26 27 28 29 30 31 32 33


def option_parser():
    """
    :return:
    """
    parser = OptionParser(usage="usage: visual_dl visual_dl.py "\
                          "-p port [options]")
    parser.add_option(
        "-p",
        "--port",
Q
Qiao Longfei 已提交
34
        type=int,
Q
Qiao Longfei 已提交
35 36 37
        default=8040,
        action="store",
        dest="port",
S
debug  
superjom 已提交
38 39 40 41 42 43 44 45
        help="api service port")
    parser.add_option(
        "-t",
        "--host",
        type=str,
        default="0.0.0.0",
        action="store",
        help="api service ip")
Q
Qiao Longfei 已提交
46 47 48 49 50 51 52 53 54 55
    parser.add_option(
        "--logdir", action="store", dest="logdir", help="log file directory")
    return parser.parse_args()


options, args = option_parser()
server_path = os.path.abspath(os.path.dirname(sys.argv[0]))
static_file_path = "./frontend/dist/"
mock_data_path = "./mock_data/"

Y
Yan Chunwei 已提交
56
log_reader = LogReader(options.logdir)
S
debug  
superjom 已提交
57

Q
Qiao Longfei 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

# return data
# status, msg, data
def gen_result(status, msg, data):
    """
    :param status:
    :param msg:
    :return:
    """
    result = dict()
    result['status'] = status
    result['msg'] = msg
    result['data'] = data
    return result


@app.route("/")
def index():
    return redirect('/static/index.html', code=302)


@app.route('/static/<path:filename>')
def serve_static(filename):
    return send_from_directory(
        os.path.join(server_path, static_file_path), filename)


@app.route('/data/logdir')
def logdir():
    result = gen_result(0, "", {"logdir": options.logdir})
    return Response(json.dumps(result), mimetype='application/json')


@app.route('/data/runs')
def runs():
93
    result = gen_result(0, "", lib.get_modes(log_reader))
Q
Qiao Longfei 已提交
94 95 96 97
    return Response(json.dumps(result), mimetype='application/json')


@app.route("/data/plugin/scalars/tags")
S
superjom 已提交
98 99
def scalar_tags():
    mode = request.args.get('mode')
Q
Qiao Longfei 已提交
100
    is_debug = bool(request.args.get('debug'))
S
debug  
superjom 已提交
101 102 103
    if is_debug:
        result = mock_tags.data()
    else:
104
        result = lib.get_scalar_tags(log_reader)
S
superjom 已提交
105 106 107 108 109
    result = gen_result(0, "", result)
    return Response(json.dumps(result), mimetype='application/json')


@app.route("/data/plugin/images/tags")
S
superjom 已提交
110
def image_tags():
S
superjom 已提交
111
    mode = request.args.get('run')
112
    result = lib.get_image_tags(log_reader)
113 114 115 116 117 118 119 120
    result = gen_result(0, "", result)
    return Response(json.dumps(result), mimetype='application/json')


@app.route("/data/plugin/histograms/tags")
def histogram_tags():
    mode = request.args.get('run')
    result = lib.get_histogram_tags(log_reader)
S
debug  
superjom 已提交
121
    result = gen_result(0, "", result)
Q
Qiao Longfei 已提交
122 123 124 125 126 127 128 129
    return Response(json.dumps(result), mimetype='application/json')


@app.route('/data/plugin/scalars/scalars')
def scalars():
    run = request.args.get('run')
    tag = request.args.get('tag')
    is_debug = bool(request.args.get('debug'))
S
debug  
superjom 已提交
130
    if is_debug:
S
superjom 已提交
131
        result = mock_data.sequence_data()
S
debug  
superjom 已提交
132
    else:
133
        result = lib.get_scalar(log_reader, run, tag)
S
debug  
superjom 已提交
134

S
superjom 已提交
135
    result = gen_result(0, "", result)
Q
Qiao Longfei 已提交
136 137 138
    return Response(json.dumps(result), mimetype='application/json')


S
superjom 已提交
139 140
@app.route('/data/plugin/images/images')
def images():
S
superjom 已提交
141
    mode = request.args.get('run')
S
superjom 已提交
142
    tag = request.args.get('tag')
S
superjom 已提交
143

144
    result = lib.get_image_tag_steps(log_reader, mode, tag)
S
superjom 已提交
145
    result = gen_result(0, "", result)
S
superjom 已提交
146 147 148 149 150 151

    return Response(json.dumps(result), mimetype='application/json')


@app.route('/data/plugin/images/individualImage')
def individual_image():
S
superjom 已提交
152
    mode = request.args.get('run')
S
superjom 已提交
153
    tag = request.args.get('tag')  # include a index
S
superjom 已提交
154
    step_index = int(request.args.get('index'))  # index of step
S
superjom 已提交
155 156
    offset = 0

157
    imagefile = lib.get_invididual_image(log_reader, mode, tag, step_index)
S
superjom 已提交
158 159 160 161
    response = send_file(
        imagefile, as_attachment=True, attachment_filename='img.png')
    return response

S
superjom 已提交
162

163 164 165 166 167 168 169 170 171
@app.route('/data/plugin/histograms/histograms')
def histogram():
    run = request.args.get('run')
    tag = request.args.get('tag')
    result = lib.get_histogram(log_reader, run, tag)
    result = gen_result(0, "", result)
    return Response(json.dumps(result), mimetype='application/json')


Q
qiaolongfei 已提交
172
@app.route('/data/plugin/graphs/graphs')
Q
qiaolongfei 已提交
173
def graph():
Q
Qiao Longfei 已提交
174
    # run = request.args.get('run')
Q
qiaolongfei 已提交
175 176 177
    # model_json_str = mock_data.graph_data()
    # model_json = json.loads(model_json_str)
    model_json = vdl_graph.load_model(server_path + "/mock/squeezenet_model.pb")
Q
Qiao Longfei 已提交
178 179
    result = gen_result(0, "", model_json)
    return Response(json.dumps(result), mimetype='application/json')
Q
qiaolongfei 已提交
180

S
superjom 已提交
181

Q
Qiao Longfei 已提交
182 183
if __name__ == '__main__':
    logger.info(" port=" + str(options.port))
Y
Yan Chunwei 已提交
184 185 186 187
    if not options.logdir:
        logger.error("should pass in logdir option")
        sys.exit(-1)

S
superjom 已提交
188
    app.run(debug=False, host=options.host, port=options.port)