Example #1
0
def unset_fav(_id):
    movies = get_movies()
    for movie in movies:
        if movie['id'] == _id:
            movie['is_fav'] = False
            break
    write_movies('movies.json', movies)
    return redirect('/')
Example #2
0
def unwatch_movie(_id):
    movies = get_movies()
    for movie in movies:
        if movie['id'] == _id:
            movie['is_watch'] = False
            movie['is_current'] = False
            break
    write_movies('movies.json', movies)
    return redirect('/')
Example #3
0
def favorites():
    favorites = []
    for f in get_movies():
        try:
            if f['is_fav']:
                favorites.append(f)
        except KeyError:
            continue
    total_favs = len(favorites)
    if total_favs == 0:
        rows = 1
    else:
        rows = math.ceil(total_favs / 4)
    return render_template("favs.html", movies=favorites, rows=rows)
Example #4
0
def randomizer(len_movies):
    movies = get_movies()
    for movie in movies:
        try:
            if movie['is_current']:
                movie['is_current'] = False
        except KeyError:
            movie['is_current'] = False
    current_movies = movie_selector(1)[0]
    for movie in movies:
        if movie['id'] == current_movies['id']:
            movie['is_current'] = True
            break
    write_movies('movies.json', movies)
    return redirect('/')
Example #5
0
def index():
    current_movies = get_current_movies()
    movies = get_movies()
    total_movies = len(movies)
    if not current_movies:
        current_movies = movie_selector(1)[0]
        for movie in movies:
            if movie['id'] == current_movies['id']:
                movie['is_current'] = True
                break
        write_movies('movies.json', movies)
    else:
        current_movies = current_movies[0]
    return render_template("index.html",
                           current=current_movies,
                           movies=movies,
                           total_movies=total_movies,
                           rows=int(total_movies / 4))
Example #6
0
  </body>
</html>
'''

# A single movie entry html template
movie_tile_content = '''
<div class="col-md-6 col-lg-4 movie-tile text-center" data-trailer-youtube-id="{trailer_youtube_id}" data-toggle="modal" data-target="#trailer">
    <img src="{poster_image_url}" width="220" height="342">
    <h2>{movie_title}</h2>
</div>
'''

content = ""
# For each movie...
try:
    movies = utils.get_movies()
    for m in movies:
        # Extract the youtube ID from the url
        youtube_id_match = re.search(r'(?<=v=)[^&#]+', m.trailer_youtube_url)
        youtube_id_match = youtube_id_match or re.search(
            r'(?<=be/)[^&#]+', m.trailer_youtube_url)
        trailer_youtube_id = (youtube_id_match.group(0)
                              if youtube_id_match else None)

        # Append the tile for the movie with its content filled in
        content += movie_tile_content.format(
            movie_title=m.title,
            poster_image_url=m.poster_image_url,
            trailer_youtube_id=trailer_youtube_id)
except ValueError:  # Just in case if there is an Invalid Rating
    content = "<h2>Oops! Something went wrong.  Please hang on we will fix this at the earliest.</h2>"
Example #7
0
def train(args):
  word_dict = utils.get_lm_dict(args.train_file)

  train_data = utils.noisy_data(args.train_file, word_dict, count=100000, dict_type='word')
  dev_data = utils.clean_data(args.dev_file, word_dict, dict_type='word')

  x1_train, x2_train, c_train, y_train = train_data
  x_train = [x1+x2 for x1,x2 in zip(x1_train, x2_train)]
  x_dev, c_train, y_dev = test_data
  if dataset == 'rotten':
    m_dev = utils.get_movies(file_dir + '/test.json')

  rev_word_dict = {value:key for key, value in word_dict.items()}

  model = Mustard(args.word_size,
                  args.word_dim,
                  args.hidden_dim,
                  args.disc_size)
  model.cuda()

  optimizer = torch.optim.Adam(model.parameters())
  best_bleu = 0
  best_loss = 1000

  if os.parh.exists(args.pretrain_file):
    best_point = torch.load(args.pretrain_file)
    model.load_partial_state_dict(best_point['state_dict'])
  if os.path.exists(args.model_file):
    best_point = torch.load(args.model_file)
    model.load_state_dict(best_point['state_dict'])
    optimizer.load_state_dict(best_point['optimizer'])
    best_bleu = best_point['dev_bleu']

  eval_count = args.eval_every
  stop_count = args.stop_after

  for epoch in range(args.num_epoch):
    if stop_count <= 0:
      break

    shuffle_indices = np.random.permutation(np.arange(len(x_train)))
    x_shuffle = np.array(x_train)[shuffle_indices]
    y_shuffle = np.array(y_train)[shuffle_indices]
    c_shuffle = np.array(c_train)[shuffle_indices]

    losses = []
    train_iterator = tqdm(range(0, len(x_shuffle), args.batch_size))
    for i in train_iterator:
      if stop_count <= 0:
        train_iterator.close()
        break

      model.train()

      x_batch = x_shuffle[i:i+batch_size]
      y_batch = y_shuffle[i:i+batch_size]
      c_batch = c_shuffle[i:i+batch_size]

      x_pad = utils.hier_pad(x_batch, 
                             att_end=args.no_instance//2, 
                             pnt_start=args.no_instance//2,
                             max_tok_length=200)
      x_batch, doc_att_mask, doc_pnt_mask, tok_att_mask, tok_pnt_mask = x_pad

      y_batch, y_mask = utils.pad(y_batch)

      x_batch = torch.tensor(x_batch).cuda()
      doc_att_mask = torch.tensor(doc_att_mask).float().cuda()
      doc_pnt_mask = torch.tensor(doc_pnt_mask).float().cuda()
      tok_att_mask = torch.tensor(tok_att_mask).float().cuda()
      tok_pnt_mask = torch.tensor(tok_pnt_mask).float().cuda()
      y_batch = torch.tensor(y_batch).cuda()
      y_mask = torch.tensor(y_mask).float().cuda()
      c_batch = torch.tensor(c_batch).float().cuda()

      p_batch, loss = model(x_batch
                            doc_att_mask,
                            doc_pnt_mask,
                            tok_att_mask,
                            tok_pnt_mask,
                            y_batch,
                            y_mask,
                            category=c_batch,
                            coverage_rate=args.coverage_rate)

      losses.append(loss.item())

      loss.backward()
      nn.utils.clip_grad_norm_(model.parameters(), 3)
      nan_check = False:
      for param in model.parameters():
        if param.grad is not None:
          if torch.isnan(param.grad.sum()):
            nan_check = True
            break
      if not nan_check:
        optimizer.step()
        optimizer.zero_grad()

      eval_count -= len(x_batch)
      if eval_count <= 0:
        with torch.no_grad():
          train_loss = np.mean(losses)

          pred_sums = []
          gold_sums = []
          dev_loss = []
          printing = 5
          for j in tqdm(range(0, len(x_dev), 1)):
            model.eval()

            x_batch = x_dev[j:j+1]
            y_batch = y_dev[j:j+1]
            if args.dataset == 'rotten':
              m_batch = m_dev[j:j+1]
            else:
              m_batch = ['MOV']

            x_pad = utils.hier_pad(x_batch,
                                   att_end=args.no_instance//2,
                                   pnt_start=args.no_instance//2,
                                   max_tok_length=200)
            x_batch, doc_att_mask, doc_pnt_mask, tok_att_mask, tok_pnt_mask = x_pad

            y_batch, y_mask = utils.pad(y_batch)

            x_batch = torch.tensor(x_batch).cuda()
            doc_att_mask = torch.tensor(doc_att_mask).float().cuda()
            doc_pnt_mask = torch.tensor(doc_pnt_mask).float().cuda()
            tok_att_mask = torch.tensor(tok_att_mask).float().cuda()
            tok_pnt_mask = torch.tensor(tok_pnt_mask).float().cuda()
            y_batch = torch.tensor(y_batch).cuda()
            y_mask = torch.tensor(y_mask).float().cuda()
            c_batch = None

            p_batch, loss = model(x_batch,
                                  doc_att_mask,
                                  doc_pnt_mask,
                                  tok_att_mask,
                                  tok_pnt_mask,
                                  y_batch,
                                  y_mask,
                                  category=c_batch,
                                  coverage_rate=args.coverage_rate)

            dev_loss.append(loss.item())

            y_batch = y_batch.cpu().detach().numpy()
            p_batch = p_batch.argmax(-1).cpu().detach().numpy()
            for y, p, m in zip(y_batch, p_batch, m_batch):
              y = list([int(yy) for yy in y])
              p = list([int(pp) for pp in p])
              try:
                y = y[1:y.index(eos)]
              except:
                pass
              try:
                p = p[:p.index(eos)]
              except:
                pass
              y_text = ' '.join([rev_word_dict[yy] for yy in y])
              p_text = ' '.join([rev_word_dict[pp] for pp in p])
              pred_sums.append(p_text)
              gold_sums.append(y_text)
              if printing:
                printing -= 1
                tqdm.write('gold: %s' % y_text)
                tqdm.write('pred: %s' % p_text)
                tqdm.write('-----------------------------------------')

        gold_sums = [[gold] for gold in gold_sums]
        dev_bleu = corpus_bleu(gold_sums, pred_sums)
        dev_loss = np.mean(dev_loss)

        if dev_bleu >= best_bleu:
          tqdm.write('updating model...')
          best_loss = dev_loss
          best_bleu = dev_bleu
          stop_count = stop_after
          torch.save({
            'state_dict': model.state_dict(),
            'optimizer': optimizer.state_dict(),
            'dev_bleu': dev_bleu,
            'dev_loss': dev_loss
          }, args.model_file)
        else:
          stop_count -= 1

        tqdm.write("Epoch: %d, Batch: %d, Train Loss: %.4f, Dev Loss: %.4f, Dev BLEU: %.4f"
                   % (epoch, i, train_loss, dev_loss, dev_bleu))
        losses = []
        eval_count = args.eval_every