kiruluta's picture
Upload folder using huggingface_hub
45eaa9d verified
Raw History Blame Contribute Delete
756 Bytes
"""General helpers."""
from __future__ import annotations
import json
import os
import random
from pathlib import Path
import numpy as np
import torch
def set_seed(seed: int) -> None:
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def ensure_dir(path: str | os.PathLike) -> Path:
p = Path(path)
p.mkdir(parents=True, exist_ok=True)
return p
def save_json(obj, path: str | os.PathLike) -> None:
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
json.dump(obj, f, indent=2)
def load_json(path: str | os.PathLike):
with open(path, "r", encoding="utf-8") as f:
return json.load(f)