Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 1,819 Bytes
d745844 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | """Small utility helpers for the Text2SPARQL repair pipeline.
No business logic — just file I/O, hashing, and directory management.
"""
from __future__ import annotations
import hashlib
import json
import os
from datetime import datetime, timezone
from pathlib import Path
def save_json(obj: dict, path: str) -> None:
"""Save a dictionary as formatted JSON.
Creates parent directories if they don't exist.
Args:
obj: Dictionary to serialize.
path: Output file path.
"""
out_path = Path(path)
out_path.parent.mkdir(parents=True, exist_ok=True)
with open(out_path, "w") as f:
json.dump(obj, f, indent=2, default=str, ensure_ascii=False)
def load_json(path: str) -> dict:
"""Load a JSON file and return as dictionary.
Args:
path: Path to the JSON file.
Returns:
Parsed dictionary.
Raises:
FileNotFoundError: If file does not exist.
json.JSONDecodeError: If file is not valid JSON.
"""
with open(path, "r") as f:
return json.load(f)
def make_run_dir(base_dir: str, request_id: str) -> str:
"""Create a directory for storing run artifacts.
Structure: <base_dir>/<request_id>/
Args:
base_dir: Base directory for all runs (usually "runs").
request_id: Unique request identifier.
Returns:
Path to the created run directory.
"""
run_dir = os.path.join(base_dir, request_id)
os.makedirs(run_dir, exist_ok=True)
return run_dir
def short_hash(text: str) -> str:
"""Generate a short deterministic hash from text.
Useful for creating candidate IDs from query strings.
Args:
text: Input text to hash.
Returns:
8-character hex digest.
"""
return hashlib.sha256(text.encode("utf-8")).hexdigest()[:8]
|