Download utils/cache_manager.py from Basel0/qshub: direct link, hf CLI and curl.
- Browser
- Download file 2.45 kB
-
https://huggingface.co/spaces/Basel0/qshub/resolve/main/utils/cache_manager.py
- Command line
-
hf download hf://spaces/Basel0/qshub/utils/cache_manager.py
-
curl -L -o cache_manager.py https://huggingface.co/spaces/Basel0/qshub/resolve/main/utils/cache_manager.py
2.45 kB
| """ | |
| مدير التخزين المؤقت لاستجابات الذكاء الاصطناعي (AI Response Caching) | |
| يحفظ استجابات AI سحابياً/محلياً بصيغة JSON باستخدام هاش الملف لتجنب الاستدعاء المتكرر وتوفير التكلفة. | |
| """ | |
| import os | |
| import hashlib | |
| import json | |
| import numpy as np | |
| CACHE_DIR = ".qto_cache" | |
| def get_file_hash(data) -> str: | |
| """Computes SHA256 of files, bytes, or numpy arrays to index the cache.""" | |
| hasher = hashlib.sha256() | |
| if isinstance(data, str): | |
| # File path | |
| if not os.path.exists(data): | |
| return "" | |
| try: | |
| with open(data, "rb") as f: | |
| for chunk in iter(lambda: f.read(65536), b""): | |
| hasher.update(chunk) | |
| except Exception: | |
| return "" | |
| elif isinstance(data, bytes): | |
| hasher.update(data) | |
| elif isinstance(data, np.ndarray): | |
| hasher.update(data.tobytes()) | |
| else: | |
| # Fallback to string representation | |
| hasher.update(str(data).encode("utf-8")) | |
| return hasher.hexdigest() | |
| def get_cached_response(file_hash: str, prompt_key: str) -> str: | |
| """Reads response from local JSON cache if exists.""" | |
| if not file_hash or not prompt_key: | |
| return None | |
| cache_file = os.path.join(CACHE_DIR, f"{file_hash}.json") | |
| if os.path.exists(cache_file): | |
| try: | |
| with open(cache_file, "r", encoding="utf-8") as f: | |
| cache_data = json.load(f) | |
| return cache_data.get(prompt_key) | |
| except Exception: | |
| pass | |
| return None | |
| def save_cached_response(file_hash: str, prompt_key: str, response_text: str): | |
| """Saves AI response in the local JSON cache.""" | |
| if not file_hash or not prompt_key or not response_text: | |
| return | |
| os.makedirs(CACHE_DIR, exist_ok=True) | |
| cache_file = os.path.join(CACHE_DIR, f"{file_hash}.json") | |
| cache_data = {} | |
| if os.path.exists(cache_file): | |
| try: | |
| with open(cache_file, "r", encoding="utf-8") as f: | |
| cache_data = json.load(f) | |
| except Exception: | |
| pass | |
| cache_data[prompt_key] = response_text | |
| try: | |
| with open(cache_file, "w", encoding="utf-8") as f: | |
| json.dump(cache_data, f, ensure_ascii=False, indent=2) | |
| except Exception as e: | |
| print(f"[Cache Write Error] {e}") | |