Make SubFox production-ready with parallel translation and UI controls

This commit is contained in:
Eddie Nielsen 2026-03-25 11:24:54 +00:00
parent c40b8bed2b
commit 2b1d05f02c
6046 changed files with 798327 additions and 0 deletions

52
app/cache.py Normal file
View file

@ -0,0 +1,52 @@
import os
import json
import hashlib
import time
from pathlib import Path
CACHE_ROOT = Path("/data/cache")
def _hash_key(source_lang: str, target_lang: str, text: str) -> str:
raw = f"{source_lang}:{target_lang}:{text.strip()}"
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
def _get_path(source_lang: str, target_lang: str, key: str) -> Path:
folder = CACHE_ROOT / f"{source_lang}_{target_lang}"
folder.mkdir(parents=True, exist_ok=True)
return folder / f"{key}.json"
def get_cached(source_lang: str, target_lang: str, text: str):
key = _hash_key(source_lang, target_lang, text)
path = _get_path(source_lang, target_lang, key)
if not path.exists():
return None
try:
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
return data.get("translated")
except Exception:
return None
def set_cache(source_lang: str, target_lang: str, text: str, translated: str, model: str):
key = _hash_key(source_lang, target_lang, text)
path = _get_path(source_lang, target_lang, key)
data = {
"source": text,
"translated": translated,
"model": model,
"created": int(time.time()),
}
tmp_path = path.with_suffix(".tmp")
with open(tmp_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False)
os.replace(tmp_path, path)