Spaces:
Sleeping
Sleeping
Download answer_cache.py from jwlee-ai/Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 4.19 kB
-
https://huggingface.co/spaces/jwlee-ai/Final_Assignment_Template/resolve/main/answer_cache.py
- Command line
-
hf download hf://spaces/jwlee-ai/Final_Assignment_Template/answer_cache.py
-
curl -L -o answer_cache.py https://huggingface.co/spaces/jwlee-ai/Final_Assignment_Template/resolve/main/answer_cache.py
4.19 kB
| """Per-task λ΅λ³ μΊμ. | |
| μ±μ μλ²μ λ΅μ ν λ² μ μ‘νλλΌλ μ μ νμΈΒ·λλ²κΉ μ μν΄ λ€μ λλ €μΌ ν λκ° | |
| μλ€. ν λ¬Έμ λ§λ€ LLM νΈμΆμ΄ λΉμΈλ―λ‘ task_id λ¨μλ‘ λ΅μ λμ€ν¬μ 보κ΄ν΄ | |
| μ¬μ€ν μ μΊμλ λ¬Έμ λ μ€ν΅νλ€. ν λ¬Έμ μμ μ£½μΌλ©΄ μ μ²΄κ° λ©μΆλ λΆμ λ¬Έμ λ | |
| ν¨κ» μν β λ€μ μ€ν λ κ·Έ λ¬Έμ λ§ λ€μ νλ©΄ λλ€. | |
| μ μ₯ μμΉ: .cache/answers.json (μ΄λ―Έ λλ ν°λ¦¬ μ‘΄μ¬). | |
| νμ: {task_id: {"question": str, "answer": str}} | |
| μμμ μ°κΈ°: μμ νμΌ β os.replace λ‘ λμμ°κΈ°/ν¬λμ μμ . | |
| μΊμ 무ν¨νλ νμΌ μ§μ μμ (rm .cache/answers.json) λλ clear_cache() μ¬μ©. | |
| """ | |
| import json | |
| import os | |
| import tempfile | |
| from pathlib import Path | |
| _CACHE_PATH = Path(".cache") / "answers.json" | |
| def load_cache() -> dict: | |
| """λμ€ν¬μμ μΊμ λ‘λ. νμΌ μκ±°λ κΉ¨μ§λ©΄ λΉ dict.""" | |
| if not _CACHE_PATH.exists(): | |
| return {} | |
| try: | |
| return json.loads(_CACHE_PATH.read_text(encoding="utf-8")) | |
| except Exception as e: | |
| print(f"Warning: cache load failed ({e}); starting empty.") | |
| return {} | |
| def save_answer(task_id: str, question: str, answer: str) -> None: | |
| """task_id λ΅μ μΊμμ μΆκ°νκ³ μμμ μΌλ‘ μ μ₯. | |
| AGENT_ERROR κ²°κ³Όλ μΊμ μ ν¨ β μ¬μ€ν μ λ€μ μλν΄μΌ νλ νλͺ©μ΄λΌ.""" | |
| if not task_id or is_retryable_answer(answer): | |
| return | |
| cache = load_cache() | |
| cache[task_id] = {"question": question, "answer": answer} | |
| _CACHE_PATH.parent.mkdir(parents=True, exist_ok=True) | |
| # κ°μ λλ ν°λ¦¬μ μμ νμΌ β os.replace λ‘ OS-level atomic rename. | |
| fd, tmp = tempfile.mkstemp(prefix="answers.", suffix=".tmp", dir=str(_CACHE_PATH.parent)) | |
| try: | |
| with os.fdopen(fd, "w", encoding="utf-8") as f: | |
| json.dump(cache, f, ensure_ascii=False, indent=2) | |
| os.replace(tmp, _CACHE_PATH) | |
| except Exception: | |
| # μ€ν¨ μ μμ νμΌ μ 리 ν μ¬λ°μ. | |
| try: | |
| os.unlink(tmp) | |
| except OSError: | |
| pass | |
| raise | |
| def get_cached_answer(task_id: str, cache: dict | None = None) -> str | None: | |
| """μΊμμμ task_idμ λ΅μ κΊΌλ΄κ±°λ None. | |
| cache μΈμλ₯Ό μ£Όλ©΄ λ§€λ² λμ€ν¬ μ¬λ‘λ© μ ν¨(루νμμ μ μ©).""" | |
| if cache is None: | |
| cache = load_cache() | |
| entry = cache.get(task_id) | |
| if entry and isinstance(entry, dict): | |
| return entry.get("answer") | |
| return None | |
| def is_retryable_answer(answer: str | None) -> bool: | |
| """λ€μ μ€νμμ λ€μ μλν΄μΌ ν λ΅λ³μΈμ§ νλ³.""" | |
| if answer is None: | |
| return True | |
| a = str(answer).strip() | |
| if not a: | |
| return True | |
| upper = a.upper() | |
| return ( | |
| upper == "UNKNOWN" | |
| or upper == "UNK" | |
| or upper.startswith("AGENT_ERROR:") | |
| or upper.startswith("AGENT ERROR:") | |
| or "CANNOT ANSWER" in upper | |
| or "NO FINAL ANSWER" in upper | |
| ) | |
| def clear_cache() -> None: | |
| """μλ νΈμΆμ©. μλ νΈμΆ μ ν¨.""" | |
| if _CACHE_PATH.exists(): | |
| _CACHE_PATH.unlink() | |
| def invalidate_tasks(task_ids) -> int: | |
| """μ£Όμ΄μ§ task_id λͺ©λ‘λ§ μΊμμμ μ κ±°νκ³ μμμ μΌλ‘ μ μ₯. | |
| κ°μ λ¨κ³λ§λ€ "μ€λ΅μ΄μλ task_idλ§ μ¬μλ" νκΈ° μν ν¬νΌ. | |
| μ λ΅μΌλ‘ μΊμλ νλͺ©μ λ³΄μ‘΄ν΄ ν ν°μ μλΌλ©΄μ, λ³κ²½ ν¨κ³Όλ§ κΉ¨λμ΄ μΈ‘μ . | |
| Returns: | |
| μ€μ λ‘ μ κ±°λ νλͺ© μ. | |
| """ | |
| cache = load_cache() | |
| removed = 0 | |
| for tid in task_ids: | |
| if tid in cache: | |
| del cache[tid] | |
| removed += 1 | |
| if removed == 0: | |
| return 0 | |
| _CACHE_PATH.parent.mkdir(parents=True, exist_ok=True) | |
| fd, tmp = tempfile.mkstemp(prefix="answers.", suffix=".tmp", dir=str(_CACHE_PATH.parent)) | |
| try: | |
| with os.fdopen(fd, "w", encoding="utf-8") as f: | |
| json.dump(cache, f, ensure_ascii=False, indent=2) | |
| os.replace(tmp, _CACHE_PATH) | |
| except Exception: | |
| try: | |
| os.unlink(tmp) | |
| except OSError: | |
| pass | |
| raise | |
| return removed | |