truehuman-playground / runtime.py
datamarketinglabs's picture
Deploy Gradio playground and synced assets
34fca4b verified
Raw
History Blame Contribute Delete
3.36 kB
"""Shared runtime for Hugging Face Space scoring."""
from __future__ import annotations
import json
import re
from pathlib import Path
from typing import Any, Dict, List, Mapping, MutableMapping
DATA_DIR = Path(__file__).resolve().parent / "data"
DATASET_PATH = DATA_DIR / "pentadrive-v1.json"
I18N_PATH = DATA_DIR / "markers-i18n.json"
HAS_LATIN = re.compile(r"[a-zA-Z\u00C0-\u024F]")
def _load_json(path: Path) -> Dict[str, Any]:
with path.open("r", encoding="utf-8") as f:
return json.load(f)
DATASET = _load_json(DATASET_PATH)
I18N = _load_json(I18N_PATH)
def match_marker(marker: str, lowered: str) -> int:
m = marker.lower()
if not m:
return 0
if " " not in m and HAS_LATIN.search(m):
escaped = re.escape(m)
return len(re.findall(rf"\b{escaped}\b", lowered, re.IGNORECASE))
count = 0
start = 0
while start <= len(lowered):
idx = lowered.find(m, start)
if idx < 0:
break
count += 1
start = idx + max(1, len(m))
return count
def detect_drives(text: str) -> Dict[str, Any]:
lowered = (text or "").lower()
by_drive: MutableMapping[str, int] = {k: 0 for k in ("S", "K", "A", "M", "G")}
hits: List[Dict[str, str]] = []
phases = DATASET.get("phases") or ()
for node in DATASET.get("nodes") or ():
drive = node.get("drive")
node_phases = node.get("phases")
if not drive or not isinstance(node_phases, Mapping):
continue
for phase in phases:
phase_data = node_phases.get(phase)
markers = phase_data.get("markers") if isinstance(phase_data, Mapping) else None
if not isinstance(markers, list):
continue
for marker in markers:
count = match_marker(str(marker), lowered)
if count == 0:
continue
by_drive[str(drive)] = by_drive.get(str(drive), 0) + count
hits.append(
{
"drive": str(drive),
"node": str(node.get("node", "")),
"phase": str(phase),
"marker": str(marker),
}
)
i18n_drives = I18N.get("drives")
if isinstance(i18n_drives, dict):
for drive_code, langs in i18n_drives.items():
if not isinstance(langs, dict):
continue
for _, markers in langs.items():
if not isinstance(markers, list):
continue
for marker in markers:
count = match_marker(str(marker), lowered)
if count == 0:
continue
by_drive[str(drive_code)] = by_drive.get(str(drive_code), 0) + count
hits.append(
{
"drive": str(drive_code),
"node": "i18n",
"phase": "all",
"marker": str(marker),
}
)
ranked = [
{"drive": d, "score": s}
for d, s in sorted(by_drive.items(), key=lambda x: (-x[1], x[0]))
if s > 0
]
return {"ranked": ranked, "by_drive": dict(by_drive), "matches": hits}