Spaces:
Running
Running
| """ | |
| Utility helpers: seeding, device selection, logging, config loading. | |
| """ | |
| import os | |
| import random | |
| import logging | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| import yaml | |
| def seed_everything(seed: int = 42) -> None: | |
| """Set seed for reproducibility across all libraries.""" | |
| random.seed(seed) | |
| np.random.seed(seed) | |
| torch.manual_seed(seed) | |
| torch.cuda.manual_seed_all(seed) | |
| os.environ["PYTHONHASHSEED"] = str(seed) | |
| torch.backends.cudnn.deterministic = True | |
| torch.backends.cudnn.benchmark = False | |
| def get_device() -> torch.device: | |
| """Return best available device (CUDA > MPS > CPU).""" | |
| if torch.cuda.is_available(): | |
| return torch.device("cuda") | |
| elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available(): | |
| return torch.device("mps") | |
| return torch.device("cpu") | |
| def load_config(config_path: str = "configs/default.yaml") -> dict: | |
| """Load YAML configuration file.""" | |
| with open(config_path, "r") as f: | |
| return yaml.safe_load(f) | |
| def setup_logging(log_dir: str | None = None, level: int = logging.INFO) -> logging.Logger: | |
| """Configure logging with console and optional file output.""" | |
| logger = logging.getLogger("date-fruit") | |
| logger.setLevel(level) | |
| # Avoid duplicate handlers | |
| if logger.handlers: | |
| return logger | |
| formatter = logging.Formatter( | |
| "[%(asctime)s] %(levelname)s - %(message)s", | |
| datefmt="%Y-%m-%d %H:%M:%S", | |
| ) | |
| # Console handler | |
| ch = logging.StreamHandler() | |
| ch.setFormatter(formatter) | |
| logger.addHandler(ch) | |
| # File handler (optional) | |
| if log_dir: | |
| Path(log_dir).mkdir(parents=True, exist_ok=True) | |
| fh = logging.FileHandler(Path(log_dir) / "train.log") | |
| fh.setFormatter(formatter) | |
| logger.addHandler(fh) | |
| return logger | |
| # Heritage knowledge base for Saudi date varieties. | |
| # season_start / season_end are month numbers (1..12). peak_month is the strongest month. | |
| # distinguish_* describes visual cues to identify the variety at a glance. | |
| HERITAGE_INFO = { | |
| "Ajwa": { | |
| "arabic": "ุนุฌูุฉ", | |
| "region": "ุงูู ุฏููุฉ ุงูู ููุฑุฉ (Madinah)", | |
| "description": "One of the most prized dates in Saudi Arabia, known for its dark color and soft texture.", | |
| "description_ar": "ู ู ุฃุซู ู ุชู ูุฑ ุงูู ู ููุฉุ ูุชู ููุฒ ุจูููู ุงูุฏุงูู ูููุงู ู ุงููููู.", | |
| "significance": "Mentioned in Hadith. Considered sacred and highly valued across the Muslim world.", | |
| "significance_ar": "ูุฑุฏ ุฐูุฑู ูู ุงูุญุฏูุซ ุงูุดุฑููุ ููุญุธู ุจู ูุงูุฉ ู ูุฏูุณุฉ ุนูุฏ ุงูู ุณูู ูู.", | |
| "flavor": "Rich, sweet with subtle caramel and prune notes.", | |
| "flavor_ar": "ุญูุงูุฉ ุบูููุฉ ู ุน ูู ุญุฉ ูุฑุงู ูู ูุจุฑููู.", | |
| "season_start": 8, "season_end": 10, "peak_month": 9, | |
| "distinguish": "Small, matte black-brown surface with fine wrinkles. Very soft.", | |
| "distinguish_ar": "ุตุบูุฑุฉุ ุณุทุญูุง ุฃุณูุฏ ู ุงุฆู ููุจููู ู ุน ุชุฌุงุนูุฏ ูุงุนู ุฉ. ููููุฉ ุฌุฏูุง.", | |
| }, | |
| "Galaxy": { | |
| "arabic": "ููุงูุณู", | |
| "region": "ุงูู ู ููุฉ ุงูุนุฑุจูุฉ ุงูุณุนูุฏูุฉ (Saudi Arabia)", | |
| "description": "A premium commercial variety known for its large size and golden-brown color.", | |
| "description_ar": "ุตูู ุชุฌุงุฑู ู ุชู ููุฒ ุจุญุฌู ู ุงููุจูุฑ ููููู ุงูุฐูุจู ุงูุจููู.", | |
| "significance": "Popular modern variety widely exported from Saudi Arabia.", | |
| "significance_ar": "ุตูู ุญุฏูุซ ู ุดููุฑ ููุตุฏููุฑ ู ู ุงูุณุนูุฏูุฉ ุจูุซุฑุฉ.", | |
| "flavor": "Sweet, chewy with honey-like undertones.", | |
| "flavor_ar": "ุญูุงูุฉ ู ุทุงุทููุฉ ุจูู ุณุฉ ุนุณู.", | |
| "season_start": 8, "season_end": 10, "peak_month": 9, | |
| "distinguish": "Large, glossy, amber-golden skin. Elongated with a pointed tip.", | |
| "distinguish_ar": "ูุจูุฑุฉุ ูุดุฑุชูุง ุฐูุจููุฉ ูุงู ุนุฉุ ู ุณุชุทููุฉ ู ุฏุจูุจุฉ ุงูุฃุทุฑุงู.", | |
| }, | |
| "Medjool": { | |
| "arabic": "ู ุฌุฏูู", | |
| "region": "ุงูู ุฏููุฉ ุงูู ููุฑุฉ (Madinah) / ุงูุฑูุงุถ (Riyadh)", | |
| "description": "Known as the 'King of Dates' for its large size and rich taste.", | |
| "description_ar": "ููููููุจ ุจูยซู ูู ุงูุชู ูุฑยป ูุญุฌู ู ุงููุจูุฑ ูู ุฐุงูู ุงููุงุฎุฑ.", | |
| "significance": "One of the most recognized date varieties worldwide. Historically reserved for royalty.", | |
| "significance_ar": "ู ู ุฃุดูุฑ ุงูุชู ูุฑ ุนุงูู ููุงุ ููุงู ูุฏูู ูุง ุญูุฑูุง ุนูู ุงูู ููู.", | |
| "flavor": "Caramel-like sweetness, soft and creamy texture.", | |
| "flavor_ar": "ุญูุงูุฉ ุงููุฑุงู ููุ ุจููุงู ูููู ูุฑูู ู.", | |
| "season_start": 9, "season_end": 11, "peak_month": 10, | |
| "distinguish": "Very large, plump, wrinkled amber-brown skin. Tender and moist.", | |
| "distinguish_ar": "ูุจูุฑุฉ ุฌุฏูุงุ ู ู ุชูุฆุฉุ ูุดุฑุชูุง ุจูููุฉ ููุฑู ุงูููุฉ ู ุชุฌุนูุฏุฉ. ุฑุทุจุฉ ุทุฑููุฉ.", | |
| }, | |
| "Meneifi": { | |
| "arabic": "ู ูููู", | |
| "region": "ุงููุตูู (Qassim) / ุงูู ุฏููุฉ ุงูู ููุฑุฉ (Madinah)", | |
| "description": "A traditional Saudi variety with elongated shape and amber color.", | |
| "description_ar": "ุตูู ุณุนูุฏู ุฃุตููุ ุดููู ู ุณุชุทูู ููููู ููุฑู ุงูู.", | |
| "significance": "Well-known in local Saudi markets, a household favorite.", | |
| "significance_ar": "ู ุนุฑูู ูู ุงูุฃุณูุงู ุงูุณุนูุฏูุฉุ ูู ูุถููู ูู ุงูุจููุช.", | |
| "flavor": "Moderately sweet with a firm, slightly chewy texture.", | |
| "flavor_ar": "ุญูุงูุชู ู ุชูุณูุทุฉุ ููุงู ู ู ุชู ุงุณู ู ุงุฆู ููู ุถุบ.", | |
| "season_start": 8, "season_end": 10, "peak_month": 9, | |
| "distinguish": "Elongated, narrow, light amber body. Firmer than Medjool.", | |
| "distinguish_ar": "ู ุณุชุทููุฉ ูุญููุฉุ ููููุง ููุฑู ุงูู ูุงุชุญ. ุฃูุซุฑ ุตูุงุจุฉ ู ู ุงูู ุฌุฏูู.", | |
| }, | |
| "Nabtat Ali": { | |
| "arabic": "ูุจุชุฉ ุนูู", | |
| "region": "ุงูู ุฏููุฉ ุงูู ููุฑุฉ (Madinah)", | |
| "description": "A Madinah variety with distinctive reddish-brown color.", | |
| "description_ar": "ุตูู ู ุฏูู ูุชู ููุฒ ุจูููู ุงูุจููู ุงูู ุงุฆู ููุงุญู ุฑุงุฑ.", | |
| "significance": "Named variety from the date palm farms of Madinah.", | |
| "significance_ar": "ู ู ุฃุดูุฑ ุฃุตูุงู ูุฎูู ุงูู ุฏููุฉ ุงูู ูููุฑุฉ.", | |
| "flavor": "Sweet and tender with a moist, soft flesh.", | |
| "flavor_ar": "ุญููุฉ ุทุฑููุฉุ ูุญู ูุง ุฑุทุจ ูููู.", | |
| "season_start": 9, "season_end": 10, "peak_month": 9, | |
| "distinguish": "Medium size, reddish-brown tone, slightly wrinkled.", | |
| "distinguish_ar": "ู ุชูุณูุทุฉ ุงูุญุฌู ุ ููููุง ุจููู ู ุญู ุฑูุ ุจุชุฌุงุนูุฏ ุฎูููุฉ.", | |
| }, | |
| "Rutab": { | |
| "arabic": "ุฑุทุจ", | |
| "region": "ุฌู ูุน ู ูุงุทู ุงูู ู ููุฉ (All regions)", | |
| "description": "Refers to the soft, ripe stage of the date fruit before full drying.", | |
| "description_ar": "ุงูู ุฑุญูุฉ ุงูุทุฑููุฉ ุงููุงุถุฌุฉ ู ู ุซู ุฑุฉ ุงููุฎูุฉ ูุจู ุงูุฌูุงู ุงููุงู ู.", | |
| "significance": "Deeply embedded in Saudi culture. Fresh rutab is a seasonal delicacy during harvest.", | |
| "significance_ar": "ุฌุฒุก ุฃุตูู ู ู ุงูุซูุงูุฉ ุงูุณุนูุฏูุฉ. ุงูุฑุทุจ ุงูุทุงุฒุฌ ุฃููุฉ ู ูุณู ูุฉ ูู ุงูุฌูู.", | |
| "flavor": "Extremely soft, juicy, and intensely sweet.", | |
| "flavor_ar": "ููููุฉ ุฌุฏูุงุ ุนุตูุฑููุฉุ ุดุฏูุฏุฉ ุงูุญูุงูุฉ.", | |
| "season_start": 7, "season_end": 9, "peak_month": 8, | |
| "distinguish": "Glossy, half-amber half-brown transition skin. Very moist.", | |
| "distinguish_ar": "ูุดุฑุชูุง ูุงู ุนุฉุ ูุตู ููุฑู ุงูููุฉ ููุตู ุจููููุฉ. ุฑุทุจุฉ ุฌุฏูุง.", | |
| }, | |
| "Shaishe": { | |
| "arabic": "ุดูุดู", | |
| "region": "ุงูู ุฏููุฉ ุงูู ููุฑุฉ (Madinah)", | |
| "description": "A Madinah-specific variety with small to medium size.", | |
| "description_ar": "ุตูู ู ุฏูู ุฎุงุตุ ุตุบูุฑ ุฅูู ู ุชูุณูุท ุงูุญุฌู .", | |
| "significance": "Traditional variety cultivated in Madinah palm groves for generations.", | |
| "significance_ar": "ุตูู ุชูููุฏู ููุฒุฑุน ูู ูุฎูู ุงูู ุฏููุฉ ูุฃุฌูุงู.", | |
| "flavor": "Balanced sweetness with a smooth, soft texture.", | |
| "flavor_ar": "ุญูุงูุฉ ู ุชูุงุฒูุฉ ูููุงู ูุงุนู .", | |
| "season_start": 9, "season_end": 10, "peak_month": 9, | |
| "distinguish": "Small, smooth, dark amber. Uniform shape.", | |
| "distinguish_ar": "ุตุบูุฑุฉ ูุงุนู ุฉ ุงููุดุฑุฉุ ููููุง ููุฑู ุงูู ุฏุงูู. ุดูููุง ู ูุชุธู .", | |
| }, | |
| "Sokari": { | |
| "arabic": "ุณูุฑู", | |
| "region": "ุงููุตูู (Qassim)", | |
| "description": "The most popular date variety in Saudi Arabia, known for its golden color and sugar-like sweetness.", | |
| "description_ar": "ุฃุดูุฑ ุชู ูุฑ ุงูุณุนูุฏูุฉุ ูุชู ููุฒ ุจูููู ุงูุฐูุจู ูุญูุงูุชู ุงูุณูุฑููุฉ.", | |
| "significance": "Named 'Sokari' (sugary) for its exceptional sweetness. A staple in every Saudi home.", | |
| "significance_ar": "ุณูู ููู ยซุณูุฑูยป ูุญูุงูุชู ุงูุงุณุชุซูุงุฆููุฉ. ุญุงุถุฑ ูู ููู ุจูุช ุณุนูุฏู.", | |
| "flavor": "Crisp outer layer with a soft, sweet interior. Tastes like caramel candy.", | |
| "flavor_ar": "ูุดุฑุฉ ู ูุฑู ุดุฉ ููุจู ุทุฑูู ุญููุ ุทุนู ู ูุญููู ุงููุฑุงู ูู.", | |
| "season_start": 7, "season_end": 9, "peak_month": 8, | |
| "distinguish": "Two-tone: golden tip with brown base. Crunchy skin, soft inside.", | |
| "distinguish_ar": "ุจููููู: ุฐูุจู ูู ุงูุทุฑู ูุจููู ูู ุงููุงุนุฏุฉ. ูุดุฑุฉ ู ูุฑู ุดุฉ ููุจู ุทุฑูู.", | |
| }, | |
| "Sugaey": { | |
| "arabic": "ุตูุนู", | |
| "region": "ุงููุตูู (Qassim) / ุงูุฑูุงุถ (Riyadh)", | |
| "description": "A popular variety often eaten at the Khalal (yellow) or Rutab stage.", | |
| "description_ar": "ุตูู ุดููุฑ ูุคูู ุบุงูุจูุง ูู ู ุฑุญูุฉ ุงูุฎูุงู (ุงูุฃุตูุฑ) ุฃู ุงูุฑุทุจ.", | |
| "significance": "Widely consumed during Ramadan. A favorite for stuffing with nuts.", | |
| "significance_ar": "ููุณุชููู ุจูุซุฑุฉ ูู ุฑู ุถุงูุ ูู ูุถููู ููุญุดู ุจุงูู ูุณูุฑุงุช.", | |
| "flavor": "Mildly sweet, firm texture, often enjoyed fresh.", | |
| "flavor_ar": "ุญูุงูุฉ ุฎูููุฉ ูููุงู ู ุชู ุงุณูุ ููุคูู ุทุงุฒุฌูุง ุบุงูุจูุง.", | |
| "season_start": 7, "season_end": 9, "peak_month": 8, | |
| "distinguish": "Oblong, yellow-to-amber gradient. Firm, dry-looking skin.", | |
| "distinguish_ar": "ู ุณุชุทููุฉุ ููููุง ูุชุฏุฑูุฌ ู ู ุงูุฃุตูุฑ ุฅูู ุงูููุฑู ุงูู. ูุดุฑุชูุง ู ุชู ุงุณูุฉ ูุดุจู ุฌุงููุฉ.", | |
| }, | |
| } | |