Download course_code/common/io.py from Evan2027/disaster-tweet-classifier: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/Evan2027/disaster-tweet-classifier/resolve/main/course_code/common/io.py
- Command line
-
hf download hf://spaces/Evan2027/disaster-tweet-classifier/course_code/common/io.py
-
curl -L -o io.py https://huggingface.co/spaces/Evan2027/disaster-tweet-classifier/resolve/main/course_code/common/io.py
1.63 kB
| import hashlib | |
| import json | |
| import random | |
| import sys | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parents[1] | |
| def dump(path, value): | |
| path = Path(path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8") | |
| def load(path): | |
| return json.loads(Path(path).read_text(encoding="utf-8")) | |
| def jsonl(path, rows): | |
| path = Path(path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| with path.open("w", encoding="utf-8") as f: | |
| for row in rows: | |
| f.write(json.dumps(row, ensure_ascii=False) + "\n") | |
| def seed_all(seed=42): | |
| import numpy as np | |
| import torch | |
| random.seed(seed) | |
| np.random.seed(seed) | |
| torch.manual_seed(seed) | |
| torch.cuda.manual_seed_all(seed) | |
| torch.backends.cudnn.deterministic = True | |
| torch.backends.cudnn.benchmark = False | |
| torch.set_num_threads(6) | |
| def environment(): | |
| import importlib.metadata as m | |
| import torch | |
| packages = {} | |
| for name in ["torch", "transformers", "tokenizers", "numpy", "pandas", "scikit-learn", "gradio", "easyjailbreak"]: | |
| try: | |
| packages[name] = m.version(name) | |
| except m.PackageNotFoundError: | |
| packages[name] = None | |
| return dict(python=sys.version, executable=sys.executable, packages=packages, | |
| cuda=torch.version.cuda, gpu=torch.cuda.get_device_name(0) if torch.cuda.is_available() else None) | |
| def sha256(path): | |
| h = hashlib.sha256() | |
| with Path(path).open("rb") as f: | |
| for b in iter(lambda: f.read(2**20), b""): | |
| h.update(b) | |
| return h.hexdigest() | |