<commit_before>import time
import sys
from flask import Flask, render_template, request, redirect
from . import model
import argparse
import yaml
import logging
import json
import socket
import subprocess
import traceback
import six

import google.oauth2.id_token
import google.auth.transport.requests

from .experiment import experiment_from_dict

logging.basicConfig()

app = Flask(__name__)


DB_PROVIDER_EXPIRATION = 1800

_db_provider_timestamp = None
_db_provider = None
_config = None

_tensorboard_dirs = {}
_grequest = google.auth.transport.requests.Request()
_save_auth_cookie = False

logger = None


@app.route('/')
def dashboard():
    return _render('dashboard.html')


@app.route('/projects')
def projects():
    return _render('projects.html')


@app.route('/users')
def users():
    return _render('users.html')


@app.route('/all')
def all_experiments():
    return _render('all_experiments.html')


@app.route('/project/<key>')
def project_details(key):
    return _render('project_details.html', project=key)


@app.route('/user/<key>')
def user_experiments(key):
    return _render("user_details.html", user=key)


@app.route('/experiment/<key>')
def experiment(key):
    return _render("experiment_details.html", experiment=key)


@app.route('/tensorboard_exp/<key>')
def tensorboard_exp(key):
    if get_allow_tensorboard():
        experiment = _db_provider.get_experiment(key, getinfo=False)
        tb_path = _db_provider.get_artifact(experiment.artifacts['tb'])

        return tensorboard(tb_path)
    else:
        return render_template(
            'error.html',
            errormsg="Tensorboard is not allowed in hosted mode yet")


@app.route('/tensorboard_proj/<key>')
def tensorboard_proj(key):
    if get_allow_tensorboard():
        experiments = [get_db().get_experiment(e) for e in
                       get_db().get_project_experiments(key)]

        logdir = ','.join(
            [e.key + ":" + get_db().get_artifact(e.artifacts['tb'])
             for e in experiments])

        return tensorboard(logdir)
    else:
        return render_template(
            'error.html',
            errormsg="TensorBoard is not allowed in hosted mode yet")


def tensorboard(logdir):
    port = _tensorboard_dirs.get(logdir)
    if not port:

        sock = socket.socket(socket.AF_INET)
        sock.bind(('', 0))
        port = sock.getsockname()[1]
        sock.close()

        subprocess.Popen([
            'tensorboard',
            '--logdir=' + logdir,
            '--port=' + str(port)])
        time.sleep(5)  # wait for tensorboard to spin up
        _tensorboard_dirs[logdir] = port

    redirect_url = 'http://{}:{}'.format(
        six.moves.urllib.parse.urlparse(request.url).hostname,
        port)

    logger.debug('Redirecting to ' + redirect_url)
    return redirect(redirect_url)


@app.route('/api/get_experiment', methods=['POST'])
def get_experiment():
    tic = time.time()
    key = request.json['key']
    getlogger().info('Getting experiment {} '.format(key))
    try:
        experiment = get_db().get_experiment(key).__dict__
        artifacts = get_db().get_artifacts(key)
        for art, url in six.iteritems(artifacts):
            experiment['artifacts'][art]['url'] = url

        status = 'ok'
    except BaseException as e:
        experiment = {}
        status = traceback.format_exc()

    retval = json.dumps({'status': status, 'experiment': experiment})

    toc = time.time()<commit_msg>fix flake8 complaints in apiserver.py<commit_after>import time
import sys
from flask import Flask, render_template, request, redirect
from . import model
import argparse
import yaml
import logging
import json
import socket
import subprocess
import traceback
import six

import google.oauth2.id_token
import google.auth.transport.requests

from .experiment import experiment_from_dict

logging.basicConfig()

app = Flask(__name__)


DB_PROVIDER_EXPIRATION = 1800

_db_provider_timestamp = None
_db_provider = None
_config = None

_tensorboard_dirs = {}
_grequest = google.auth.transport.requests.Request()
_save_auth_cookie = False

logger = None


@app.route('/')
def dashboard():
    return _render('dashboard.html')


@app.route('/projects')
def projects():
    return _render('projects.html')


@app.route('/users')
def users():
    return _render('users.html')


@app.route('/all')
def all_experiments():
    return _render('all_experiments.html')


@app.route('/project/<key>')
def project_details(key):
    return _render('project_details.html', project=key)


@app.route('/user/<key>')
def user_experiments(key):
    return _render("user_details.html", user=key)


@app.route('/experiment/<key>')
def experiment(key):
    return _render("experiment_details.html", experiment=key)


@app.route('/tensorboard_exp/<key>')
def tensorboard_exp(key):
    if get_allow_tensorboard():
        experiment = _db_provider.get_experiment(key, getinfo=False)
        tb_path = _db_provider.get_artifact(experiment.artifacts['tb'])

        return tensorboard(tb_path)
    else:
        return render_template(
            'error.html',
            errormsg="Tensorboard is not allowed in hosted mode yet")


@app.route('/tensorboard_proj/<key>')
def tensorboard_proj(key):
    if get_allow_tensorboard():
        experiments = [get_db().get_experiment(e) for e in
                       get_db().get_project_experiments(key)]

        logdir = ','.join(
            [e.key + ":" + get_db().get_artifact(e.artifacts['tb'])
             for e in experiments])

        return tensorboard(logdir)
    else:
        return render_template(
            'error.html',
            errormsg="TensorBoard is not allowed in hosted mode yet")


def tensorboard(logdir):
    port = _tensorboard_dirs.get(logdir)
    if not port:

        sock = socket.socket(socket.AF_INET)
        sock.bind(('', 0))
        port = sock.getsockname()[1]
        sock.close()

        subprocess.Popen([
            'tensorboard',
            '--logdir=' + logdir,
            '--port=' + str(port)])
        time.sleep(5)  # wait for tensorboard to spin up
        _tensorboard_dirs[logdir] = port

    redirect_url = 'http://{}:{}'.format(
        six.moves.urllib.parse.urlparse(request.url).hostname,
        port)

    logger.debug('Redirecting to ' + redirect_url)
    return redirect(redirect_url)


@app.route('/api/get_experiment', methods=['POST'])
def get_experiment():
    tic = time.time()
    key = request.json['key']
    getlogger().info('Getting experiment {} '.format(key))
    try:
        experiment = get_db().get_experiment(key).__dict__
        artifacts = get_db().get_artifacts(key)
        for art, url in six.iteritems(artifacts):
            experiment['artifacts'][art]['url'] = url

        status = 'ok'
    except BaseException:
        experiment = {}
        status = traceback.format_exc()

    retval = json.dumps({'status': status, 'experiment': experiment})

    toc = time.time()
{'fraction_non_alphanumeric': 0.13023456612462225, 'fraction_numerical': 0.0024463951647719097, 'mean_word_length': 3.2378048780487805, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2054899', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2140, 'n_words': 457}