Download data_loader.py from ahn009/Oxinto-AI: direct link, hf CLI and curl.
- Browser
- Download file 2 kB
-
https://huggingface.co/spaces/ahn009/Oxinto-AI/resolve/main/data_loader.py
- Command line
-
hf download hf://spaces/ahn009/Oxinto-AI/data_loader.py
-
curl -L -o data_loader.py https://huggingface.co/spaces/ahn009/Oxinto-AI/resolve/main/data_loader.py
2 kB
| """ | |
| Loads and caches product catalog and questionnaire config from JSON files. | |
| """ | |
| import json | |
| import logging | |
| from pathlib import Path | |
| from typing import List, Dict, Any, Optional | |
| logger = logging.getLogger(__name__) | |
| _products: Optional[List[Dict]] = None | |
| _questionnaire: Optional[Dict] = None | |
| DATA_DIR = Path(__file__).parent / "data" | |
| def get_products() -> List[Dict]: | |
| global _products | |
| if _products is None: | |
| path = DATA_DIR / "products.json" | |
| try: | |
| with open(path, "r", encoding="utf-8") as f: | |
| raw = json.load(f) | |
| # Assign sequential IDs if not present | |
| _products = [ | |
| {**p, "id": p.get("id", i + 1)} | |
| for i, p in enumerate(raw) | |
| ] | |
| logger.info("Loaded %d products from %s", len(_products), path) | |
| except FileNotFoundError: | |
| logger.error("products.json not found at %s", path) | |
| _products = [] | |
| except json.JSONDecodeError as e: | |
| logger.error("Failed to parse products.json: %s", e) | |
| _products = [] | |
| return _products | |
| def get_questionnaire() -> Dict[str, Any]: | |
| global _questionnaire | |
| if _questionnaire is None: | |
| path = DATA_DIR / "questionnaire.json" | |
| try: | |
| with open(path, "r", encoding="utf-8") as f: | |
| _questionnaire = json.load(f) | |
| logger.info("Loaded questionnaire from %s", path) | |
| except FileNotFoundError: | |
| logger.warning("questionnaire.json not found — using empty config") | |
| _questionnaire = {"questions": []} | |
| except json.JSONDecodeError as e: | |
| logger.error("Failed to parse questionnaire.json: %s", e) | |
| _questionnaire = {"questions": []} | |
| return _questionnaire | |
| def reload_data() -> None: | |
| """Force reload of all cached data (useful in tests).""" | |
| global _products, _questionnaire | |
| _products = None | |
| _questionnaire = None | |
| get_products() | |
| get_questionnaire() | |