Upload utils.py with huggingface_hub
Browse files
utils.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from typing import Any, Dict
|
| 2 |
|
| 3 |
|
|
@@ -22,3 +23,22 @@ def flatten_dict(
|
|
| 22 |
items.append((new_key, v))
|
| 23 |
|
| 24 |
return dict(items)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
from typing import Any, Dict
|
| 3 |
|
| 4 |
|
|
|
|
| 23 |
items.append((new_key, v))
|
| 24 |
|
| 25 |
return dict(items)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def load_json(path):
|
| 29 |
+
with open(path) as f:
|
| 30 |
+
try:
|
| 31 |
+
return json.load(f)
|
| 32 |
+
except json.decoder.JSONDecodeError as e:
|
| 33 |
+
with open(path) as f:
|
| 34 |
+
file_content = "\n".join(f.readlines())
|
| 35 |
+
raise RuntimeError(
|
| 36 |
+
f"Failed to decode json file at '{path}' with file content:\n{file_content}"
|
| 37 |
+
) from e
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def save_json(path, data):
|
| 41 |
+
with open(path, "w") as f:
|
| 42 |
+
dumped = json.dumps(data, indent=4, ensure_ascii=False)
|
| 43 |
+
f.write(dumped)
|
| 44 |
+
f.write("\n")
|