from __future__ import annotations
import json
import shutil
import subprocess
import tempfile
from datetime import datetime, timedelta
from functools import lru_cache
from pathlib import Path
from huggingface_hub import hf_hub_download
import gradio as gr
from modular_graph_and_candidates import build_graph_json, generate_html, build_timeline_json, generate_timeline_html
HF_MAIN_REPO = "https://github.com/huggingface/transformers"
CACHE_REPO = "Molbap/hf_cached_embeds_log"
def _fetch_from_cache_repo(kind: str, sim_method: str, threshold: float, multimodal: bool):
repo_id = CACHE_REPO
latest_fp = hf_hub_download(repo_id=repo_id, filename="latest.json", repo_type="dataset")
info = json.loads(Path(latest_fp).read_text(encoding="utf-8"))
sha = info.get("sha")
key = f"{sha}/{sim_method}-{threshold:.2f}-m{int(multimodal)}"
html_fp = hf_hub_download(repo_id=repo_id, filename=f"{kind}/{key}.html", repo_type="dataset")
json_fp = hf_hub_download(repo_id=repo_id, filename=f"{kind}/{key}.json", repo_type="dataset")
raw_html = Path(html_fp).read_text(encoding="utf-8")
json_text = Path(json_fp).read_text(encoding="utf-8")
iframe_html = f''
tmp = Path(tempfile.mkstemp(suffix=("_timeline.json" if kind == "timeline" else ".json"))[1])
tmp.write_text(json_text, encoding="utf-8")
return iframe_html, str(tmp)
def _escape_srcdoc(text: str) -> str:
"""Escape for inclusion inside an