import torch


def eval_metrics(preds: torch.Tensor, labels: torch.Tensor) -> dict:
    """Calculate metrics: precision, recall, f1, accuracy"""
    tp = ((preds == 1) & (labels == 1)).cpu().sum().item()
    fp = ((preds == 1) & (labels == 0)).cpu().sum().item()
    fn = ((preds == 0) & (labels == 1)).cpu().sum().item()
    tn = ((preds == 0) & (labels == 0)).cpu().sum().item()

    precision = tp / (tp + fp) if (tp + fp) else 0.0
    recall = tp / (tp + fn) if (tp + fn) else 0.0
    f1 = 2 * precision * recall / (precision + recall) if (precision + recall) else 0.0
    acc = (tp + tn) / (tp + fp + fn + tn) if (tp + fp + fn + tn) else 0.0

    return {"precision": precision, "recall": recall, "f1": f1, "acc": acc}