Spaces:
Running
Running
File size: 2,252 Bytes
0fa39da | 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 77 78 79 80 81 82 83 | import json
from pathlib import Path
# If Hugging Face persistent storage is attached, /data is the preferred location.
# If /data does not exist, this falls back to the app folder, but that is not guaranteed
# to survive restarts/rebuilds.
DATA_DIR = Path("/data") if Path("/data").exists() else Path(".")
MEMORY_FILE = DATA_DIR / "replacement_memory.json"
def get_memory_file_path():
return str(MEMORY_FILE)
def load_remembered_replacements():
if not MEMORY_FILE.exists():
return []
try:
with MEMORY_FILE.open("r", encoding="utf-8") as f:
data = json.load(f)
if not isinstance(data, list):
return []
cleaned = []
for row in data:
find_text = str(row.get("find", "")).strip()
replace_with = str(row.get("replace_with", "")).strip()
entity_type = str(row.get("entity_type", "REMEMBERED")).strip() or "REMEMBERED"
if not find_text or not replace_with:
continue
cleaned.append(
{
"include": True,
"remember": True,
"find": find_text,
"replace_with": replace_with,
"entity_type": entity_type,
"score": "",
}
)
return cleaned
except Exception:
return []
def save_remembered_replacements(rows):
MEMORY_FILE.parent.mkdir(parents=True, exist_ok=True)
deduped = {}
for row in rows:
find_text = str(row.get("find", "")).strip()
replace_with = str(row.get("replace_with", "")).strip()
entity_type = str(row.get("entity_type", "REMEMBERED")).strip() or "REMEMBERED"
if not find_text or not replace_with:
continue
deduped[find_text] = {
"find": find_text,
"replace_with": replace_with,
"entity_type": entity_type,
}
final_rows = list(deduped.values())
with MEMORY_FILE.open("w", encoding="utf-8") as f:
json.dump(final_rows, f, ensure_ascii=False, indent=2)
return len(final_rows)
def clear_remembered_replacements():
if MEMORY_FILE.exists():
MEMORY_FILE.unlink() |