Spaces:
Running
on
Zero
Running
on
Zero
File size: 6,038 Bytes
1c0abf0 |
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 |
#!/usr/bin/env python3
"""
Utilitaires pour Step 03 - Lecture de la configuration Step 02
"""
import json
from pathlib import Path
from typing import Dict, Optional
class Step03Config:
"""Gestionnaire de configuration Step 03 basé sur la sortie Step 02."""
def __init__(self, config_file: str = "step03_config.json"):
self.config_file = Path(config_file)
self.config = self.load_config()
def load_config(self) -> Dict:
"""Charge la configuration Step 03."""
if not self.config_file.exists():
raise FileNotFoundError(
f"❌ Configuration Step 03 non trouvée: {self.config_file}\n"
f"💡 Lancez d'abord: python step02_upload_embeddings.py"
)
try:
with open(self.config_file, 'r', encoding='utf-8') as f:
config = json.load(f)
# Vérification de la structure
if not config.get("step02_completed"):
raise ValueError("❌ Step 02 non complété selon la configuration")
required_keys = ["huggingface", "embeddings_info"]
for key in required_keys:
if key not in config:
raise ValueError(f"❌ Clé manquante dans configuration: {key}")
return config
except json.JSONDecodeError as e:
raise ValueError(f"❌ Configuration Step 03 malformée: {e}")
@property
def repo_id(self) -> str:
"""Repository Hugging Face ID."""
return self.config["huggingface"]["repo_id"]
@property
def dataset_name(self) -> str:
"""Nom du dataset."""
return self.config["huggingface"]["dataset_name"]
@property
def embeddings_file(self) -> str:
"""Nom du fichier SafeTensors."""
return self.config["huggingface"]["files"]["embeddings"]
@property
def metadata_file(self) -> str:
"""Nom du fichier métadonnées."""
return self.config["huggingface"]["files"]["metadata"]
@property
def total_vectors(self) -> int:
"""Nombre total de vecteurs."""
return self.config["embeddings_info"]["total_vectors"]
@property
def vector_dimension(self) -> int:
"""Dimension des vecteurs."""
return self.config["embeddings_info"]["vector_dimension"]
@property
def embedding_model(self) -> str:
"""Modèle d'embedding utilisé."""
return self.config["embeddings_info"]["embedding_model"]
@property
def download_command(self) -> str:
"""Commande de téléchargement HF Hub."""
return self.config["usage_examples"]["download_command"]
@property
def load_command(self) -> str:
"""Commande de chargement SafeTensors."""
return self.config["usage_examples"]["load_command"]
def print_summary(self):
"""Affiche un résumé de la configuration."""
print("📋 Configuration Step 03 - Résumé")
print("=" * 40)
print(f"📦 Repository HF: {self.repo_id}")
print(f"📊 Embeddings: {self.total_vectors:,} vecteurs")
print(f"📏 Dimension: {self.vector_dimension}")
print(f"🧠 Modèle: {self.embedding_model}")
print(f"📁 Fichier: {self.embeddings_file}")
print(f"⏰ Complété: {self.config.get('completion_timestamp', 'N/A')}")
print()
print("🚀 Prêt pour la recherche sémantique !")
def get_download_instructions(self) -> Dict[str, str]:
"""Retourne les instructions de téléchargement."""
return {
"python_code": f'''
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
import json
# Télécharger les fichiers
embeddings_file = hf_hub_download(
repo_id="{self.repo_id}",
filename="{self.embeddings_file}"
)
metadata_file = hf_hub_download(
repo_id="{self.repo_id}",
filename="{self.metadata_file}"
)
# Charger les embeddings
tensors = load_file(embeddings_file)
embeddings = tensors["embeddings"] # Shape: [{self.total_vectors}, {self.vector_dimension}]
# Charger les métadonnées
with open(metadata_file, 'r') as f:
metadata = json.load(f)
print(f"✅ Embeddings chargés: {{embeddings.shape}}")
'''.strip(),
"cli_download": f"huggingface-cli download {self.repo_id} --repo-type dataset",
"repo_url": f"https://huggingface.co/datasets/{self.repo_id}"
}
def load_step03_config(config_file: str = "step03_config.json") -> Step03Config:
"""
Fonction utilitaire pour charger la configuration Step 03.
Args:
config_file: Chemin vers le fichier de configuration
Returns:
Instance de Step03Config
Raises:
FileNotFoundError: Si le fichier n'existe pas
ValueError: Si la configuration est invalide
"""
return Step03Config(config_file)
def check_step03_ready() -> bool:
"""
Vérifie si Step 03 peut être lancé (configuration Step 02 disponible).
Returns:
True si prêt, False sinon
"""
try:
config = load_step03_config()
return config.config.get("step02_completed", False)
except (FileNotFoundError, ValueError):
return False
if __name__ == "__main__":
"""Test de la configuration Step 03."""
try:
print("🧪 Test de configuration Step 03")
print("=" * 40)
if check_step03_ready():
config = load_step03_config()
config.print_summary()
print("\n📖 Instructions de téléchargement:")
instructions = config.get_download_instructions()
print(instructions["python_code"])
else:
print("❌ Step 03 non prêt")
print("💡 Lancez d'abord: python step02_upload_embeddings.py")
except Exception as e:
print(f"❌ Erreur: {e}") |