<commit_before>import decimal
import os
import random
import urlparse

import bson
import flask
import pymongo


MONGO_URL = 'mongodb://localhost:27017/neural-noise'
client = pymongo.MongoClient(MONGO_URL)
db = client.get_default_database()

app = flask.Flask(__name__)

def is_debug():
  return os.environ.get('FLASK_ENV') != 'production'

def random_song(checkpoint, temperature):
  # Taken from http://stackoverflow.com/a/21867984/41060
  query = {
    'checkpoint': { '$eq': checkpoint },
    'temperature': { '$eq': temperature },
    'random': { '$lte': random.random() },
  }
  cursor = db.songs.find(query).sort('random', pymongo.DESCENDING)
  try:
    doc = next(cursor)
  except StopIteration:
    del query['random']
    cursor = db.songs.find(query).sort('random', pymongo.DESCENDING);
    try:
      doc = next(cursor)
    except StopIteration:
      return None

  doc['random'] = random.random();
  db.songs.update({ '_id': doc['_id'] }, doc);
  return doc

@app.route('/')
def index():
  checkpoints = [cp['name'] for cp in db.checkpoints.find({})]
  return flask.render_template('index.html', checkpoints=checkpoints)

@app.route('/query')
def query():
  temperature = flask.request.args['temperature']
  checkpoint = flask.request.args['checkpoint']
  song = random_song(checkpoint, temperature)
  if not song:
    return 'Error: no songs at checkpoint=%swith temperature=%s found' % (checkpoint, temperature)
  return flask.redirect(flask.url_for(
    'render', checkpoint=checkpoint, id_=str(song['_id'])))

@app.route('/render/<checkpoint>/<id_>')
def render(checkpoint, id_):
  song = db.songs.find_one({
    '_id': { '$eq': bson.objectid.ObjectId(id_) }
  })

  return flask.render_template('render.html', song=song)

@app.route('/2png/<id_>.png')
def png(id_):
  parts = urlparse.urlparse(flask.request.url)
  new_host = flask.request.headers['Host']
  new_host = new_host if new_host else parts.hostname
  new_path = parts.path.replace('2png', 'png')
  # Replace the /2png/ with just /png/ and remove the port
  new_parts = [parts.scheme, new_host, new_path, '', '']
  new_url = urlparse.urlunsplit(new_parts)
  return flask.redirect(new_url)

if __name__ == '__main__':
  app.run(debug=is_debug(), host='0.0.0.0')<commit_msg>Fix png redirection for dev web app.<commit_after>import decimal
import os
import random
import urlparse

import bson
import flask
import pymongo


MONGO_URL = 'mongodb://localhost:27017/neural-noise'
client = pymongo.MongoClient(MONGO_URL)
db = client.get_default_database()

app = flask.Flask(__name__)

def is_debug():
  return os.environ.get('FLASK_ENV') != 'production'

def random_song(checkpoint, temperature):
  # Taken from http://stackoverflow.com/a/21867984/41060
  query = {
    'checkpoint': { '$eq': checkpoint },
    'temperature': { '$eq': temperature },
    'random': { '$lte': random.random() },
  }
  cursor = db.songs.find(query).sort('random', pymongo.DESCENDING)
  try:
    doc = next(cursor)
  except StopIteration:
    del query['random']
    cursor = db.songs.find(query).sort('random', pymongo.DESCENDING);
    try:
      doc = next(cursor)
    except StopIteration:
      return None

  doc['random'] = random.random();
  db.songs.update({ '_id': doc['_id'] }, doc);
  return doc

@app.route('/')
def index():
  checkpoints = [cp['name'] for cp in db.checkpoints.find({})]
  return flask.render_template('index.html', checkpoints=checkpoints)

@app.route('/query')
def query():
  temperature = flask.request.args['temperature']
  checkpoint = flask.request.args['checkpoint']
  song = random_song(checkpoint, temperature)
  if not song:
    return 'Error: no songs at checkpoint=%swith temperature=%s found' % (checkpoint, temperature)
  return flask.redirect(flask.url_for(
    'render', checkpoint=checkpoint, id_=str(song['_id'])))

@app.route('/render/<checkpoint>/<id_>')
def render(checkpoint, id_):
  song = db.songs.find_one({
    '_id': { '$eq': bson.objectid.ObjectId(id_) }
  })

  return flask.render_template('render.html', song=song)

@app.route('/2png/<id_>.png')
def png(id_):
  parts = urlparse.urlparse(flask.request.url)
  new_host = flask.request.headers['Host']
  new_host = new_host[:new_host.find(':')]  # Strip out port
  new_path = parts.path.replace('2png', 'png')
  # Replace the /2png/ with just /png/.
  new_parts = [parts.scheme, new_host, new_path, '', '']
  new_url = urlparse.urlunsplit(new_parts)
  return flask.redirect(new_url)

if __name__ == '__main__':
  app.run(debug=is_debug(), host='0.0.0.0')
{'fraction_non_alphanumeric': 0.16330600487480612, 'fraction_numerical': 0.011079104808331486, 'mean_word_length': 4.57283950617284, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1483908', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1561, 'n_words': 324}