Spaces:
Sleeping
Sleeping
File size: 11,104 Bytes
6276d4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | """
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": "ู
ุณุชุทููุฉุ ููููุง ูุชุฏุฑูุฌ ู
ู ุงูุฃุตูุฑ ุฅูู ุงูููุฑู
ุงูู. ูุดุฑุชูุง ู
ุชู
ุงุณูุฉ ูุดุจู ุฌุงููุฉ.",
},
}
|