import torch


def test_dfd(dataloader, model, criterion, device):
    """ Test deep fake detector
    Parameters
    ----------
    dataloader : torch.utils.data.DataLoader
        Dataloader used for evaluation
    model : torch.Module
        Pytorch module to evaluate
    crriterion : torch.nn.Module
        Objective function used in training
    device : str
        Device to run eval
    Returns
    -------
    accuracy : float
        Model accuracy over evaluation set
    """
    device = torch.device(device)
    model.to(device)
    model.eval()
    correct = 0
    loss = 0
    total = 0
    print('------------ Validating performance')
    for idx, batch in enumerate(dataloader):
        frames, lbls = batch
        frames, lbls = frames.to(device), lbls.float().to(device)
        with torch.no_grad():
            model.lstm.reset_hidden_state()
            predictions = model(frames)
            correct += (torch.round(predictions).detach()
                        == lbls).sum().cpu().numpy()
        total += frames.shape[0]
        loss += (lbls.shape[0]) * \
            (criterion(predictions, lbls).detach().cpu().item())
    return 100.*correct/total, loss/total