#!/usr/bin/env python # -*- coding: utf-8 -*- from time import clock import sys import os from quick_server import create_server, msg addr = '' port = 8000 server = create_server((addr, port)) server.bind_path('/', '..') server.add_default_white_list() server.link_empty_favicon_fallback() start = clock() @server.json_get('/api/uptime/') def uptime(req, args): global count_uptime count_uptime += 1 return { "uptime": req.log_elapsed_time_string((clock() - start) * 1000.0).strip() } def complete_requests(_args, text): return [ "uptime" ] if "uptime".startswith(text) else [] count_uptime = 0 @server.cmd(1, complete_requests) def requests(args): if args[0] != 'uptime':
parser.add_argument('-p', type=int, default=8080, help="specifies the server port") parser.add_argument( '--www', type=str, default='..', help="the folder to serve files from (defaults to parent folder)") args = parser.parse_args() addr = args.a port = args.p www = args.www server = create_server((addr, port)) server.bind_path('/', www) server.directory_listing = True server.add_default_white_list() server.link_empty_favicon_fallback() server.suppress_noise = True server.report_slow_requests = True msg("{0}", " ".join(sys.argv)) msg("starting server at {0}:{1}", addr if addr else 'localhost', port) server.serve_forever() msg("shutting down..") server.server_close()
def get_server(addr, port, explainer_obj, cache): server = create_server((addr, port)) if heroku: server.no_command_loop = True server.bind_path('/', '.') prefix = '' else: server.bind_path('/', '..') prefix = '/' + os.path.basename(os.path.normpath(server.base_path)) server.directory_listing = True server.add_default_white_list() server.link_empty_favicon_fallback() server.suppress_noise = True server.report_slow_requests = True server.link_worker_js(prefix + '/js/worker.js') server.cache = cache def canonic_ixs(ixs): return sorted(frozenset(convert_ixs(ixs))) def convert_ixs(ixs): if ixs is None: return [] return [int(ix) for ix in ixs] def get_thresholds(args): return (float(args["l"]), float(args["r"])) def optional_bool(key, args, default): res = args[key] if key in args else default if res is None: return default return bool(res) ### explainer ### @server.json_get(prefix + '/explainer_roc_curve', 0) def json_explainer_roc_curve(req, args): args = args["query"] token = args["token"] if "token" in args and args["token"] else None pred_ixs = optional_bool("pred_ixs", args, False) res = explainer_obj.get_roc_curve() res["token"] = server.create_token() if token is None else token if pred_ixs: res["pred_ixs"] = explainer_obj.get_pred_ixs() return res def token_obj(args): token = args["token"] obj = server.get_token_obj(token) if "ixss" not in obj: obj["ixss"] = [canonic_ixs(explainer_obj.get_all_ixs())] obj["msgs"] = ["all: " + str(len(obj["ixss"][-1]))] return obj token_lock = threading.Lock() def get_ixs(args): with token_lock: return token_obj(args)["ixss"][-1] @server.json_post(prefix + '/explainer_ixs_get', 0) def json_explainer_ixs_get(req, args): with token_lock: args = args["post"] obj = token_obj(args) return [msg for msg in obj["msgs"]] @server.json_post(prefix + '/explainer_ixs_put', 0) def json_explainer_ixs_put(req, args): with token_lock: args = args["post"] if "ixs" in args: new_ixs = canonic_ixs(args["ixs"]) pre_text = args["msg"] + ": " if "msg" in args and args[ "msg"] else "" new_text = pre_text + str(len(new_ixs)) else: new_ixs = None if "pos" in args: new_pos = int(args["pos"]) else: new_pos = None obj = token_obj(args) if new_pos is not None: obj["ixss"] = obj["ixss"][:(new_pos + 1)] obj["msgs"] = obj["msgs"][:(new_pos + 1)] if new_ixs is not None: ixss = obj["ixss"] found = None if len(new_ixs) == 0: found = len(ixss) else: for (pos, ixs) in enumerate(ixss): if ixs == new_ixs: found = pos + 1 break if found is not None: obj["ixss"] = obj["ixss"][:found] obj["msgs"] = obj["msgs"][:found] else: obj["ixss"].append(new_ixs) obj["msgs"].append(new_text) return [msg for msg in obj["msgs"]] @server.json_post(prefix + '/explainer_page_get', 0) def json_explainer_page_get(req, args): with token_lock: args = args["post"] page = args["page"] obj = token_obj(args) if page not in obj: obj[page] = {} return obj[page] @server.json_post(prefix + '/explainer_page_put', 0) def json_explainer_page_put(req, args): with token_lock: args = args["post"] page = args["page"] obj = token_obj(args) if page not in obj: obj[page] = {} put = args["put"] for (k, v) in put.items(): obj[page][k] = v return obj[page] @server.json_post(prefix + '/explainer_page_clear', 0) def json_explainer_page_clear(req, args): with token_lock: args = args["post"] page = args["page"] obj = token_obj(args) obj[page] = {} return obj[page] @server.json_post(prefix + '/explainer_expls_stats', 0) def json_explainer_expls_stats(req, args): args = args["post"] ixs = get_ixs(args) score = get_thresholds(args) return { "expls": explainer_obj.get_expl_stats(ixs, score), "totals": explainer_obj.get_stats(ixs, score), "all": explainer_obj.get_stats(explainer_obj.get_all_ixs(), score), } @server.json_post(prefix + '/explainer_granular', 0) def json_explainer_granular(req, args): args = args["post"] score = get_thresholds(args) ixs = get_ixs(args) expl = args["expl"] partial = optional_bool("partial", args, False) compact = optional_bool("ignore_irrelevant_features", args, False) groups, features, feature_importances \ = explainer_obj.get_granular_expl(ixs, score, expl, partial, compact) return { "groups": groups, "features": features, "importances": feature_importances, } ### commands ### def complete_cache_clear(args, text): if args: return [] return [ section for section in cache.list_sections() if section.startswith(text) ] @server.cmd(complete=complete_cache_clear) def cache_clear(args): if len(args) > 1: msg("too many extra arguments! expected one got {0}", ' '.join(args)) return msg("clear {0}cache{1}{2}", "" if args else "all ", " " if args else "s", args[0] if args else "") cache.clean_cache(args[0] if args else None) return server, prefix
def get_server(addr, port, full_spec, output): spec, title, prefix = full_spec server = create_server((addr, port)) prefix = '/' + (os.path.basename(os.path.normpath(server.base_path)) if prefix is None else prefix) server.link_empty_favicon_fallback() server.favicon_everywhere = True server.suppress_noise = True server.report_slow_requests = True token_lock = threading.RLock() def token_obj(token): with token_lock: res = server.get_token_obj(token) tfile = os.path.join(output, "{0}.json".format(token)) if not len(res.keys()) and os.path.exists(tfile): with open(tfile, 'r') as fin: res = json.load(fin) return res def write_token(token, tobj): with token_lock: if not os.path.exists(output): os.makedirs(output) with open(os.path.join(output, "{0}.json".format(token)), 'w') as out: json.dump(tobj, fp=out, sort_keys=True, indent=2) @server.text_get(prefix + '/img/', 1) def text_file(req, args): new_name = args["paths"][0] fname = get_rev_file(new_name) if fname is None: return None with open(fname, 'r') as f_in: ext = get_extension(fname) mime = { "svg": "svg+xml", "jpg": "jpeg", }.get(ext, ext) return Response(f_in.read(), ctype='image/{0}'.format(mime)) @server.text_get(prefix + '/', 0) def text_index(req, args): return post_index(req, args) @server.text_post(prefix + '/', 0) def post_index(req, args): if "token" not in args["query"]: with token_lock: url = "{0}/?pix=0&token={1}".format(prefix, server.create_token()) req.send_response(307, "Ready to go!") req.send_header("Location", url) req.end_headers() raise PreventDefaultResponse() token = args["query"]["token"] with token_lock: tobj = token_obj(token) if "post" in args: def set_value(o, k, v): if "/" not in k: o[k] = v return kix = k.index("/") tmp = k[:kix] if tmp not in o: o[tmp] = {} set_value(o[tmp], k[kix + 1:], v) pobj = args["post"] pid = pobj["_pid"] for (k, v) in pobj.items(): if k == "_pid": continue if k.startswith("_nop"): continue set_value(tobj, "{0}/{1}".format(pid, k), v) write_token(token, tobj) pix = int(args["query"]["pix"]) url = "?pix={0}&token={1}".format(pix + 1, token) res, _pid, last_page = create_page(spec, pix, url, token, tobj, title) if last_page: msg("{0} finished!", token) return Response(res, ctype="text/html") dry_run(spec) return server, prefix