| | from dotenv import load_dotenv |
| | import os |
| | from huggingface_hub import login, HfApi |
| | import sys |
| |
|
| | def setup_huggingface(): |
| | """Configure l'authentification Hugging Face.""" |
| | try: |
| | print("=== Début de la configuration Hugging Face ===") |
| | print(f"Python version: {sys.version}") |
| | print(f"Working directory: {os.getcwd()}") |
| | |
| | |
| | load_dotenv(verbose=True, override=True) |
| | print("Variables d'environnement chargées depuis .env") |
| | |
| | |
| | print("\nRecherche du token...") |
| | |
| | |
| | token_names = ["HF_TOKEN", "HUGGINGFACE_API_TOKEN", "HUGGINGFACE_TOKEN"] |
| | |
| | |
| | env_vars = {k: v if k != "HF_TOKEN" else f"{v[:4]}...{v[-4:]}" for k, v in os.environ.items() |
| | if k.startswith("HF") or "HUGGING" in k} |
| | print("\nVariables d'environnement disponibles :") |
| | for k, v in env_vars.items(): |
| | print(f"- {k}: {v}") |
| | |
| | |
| | hf_token = None |
| | for name in token_names: |
| | value = os.environ.get(name) |
| | if value: |
| | print(f"\nToken trouvé dans {name}") |
| | if len(value.strip()) > 0: |
| | hf_token = value.strip() |
| | break |
| | else: |
| | print(f"Token trouvé dans {name} mais vide") |
| | else: |
| | print(f"Token non trouvé dans {name}") |
| | |
| | if not hf_token: |
| | print("\nErreur : Token Hugging Face non trouvé ou vide dans les variables d'environnement") |
| | return False |
| | |
| | print(f"\nToken trouvé (longueur: {len(hf_token)})") |
| | print(f"Début du token : {hf_token[:4]}...") |
| | print(f"Fin du token : ...{hf_token[-4:]}") |
| | |
| | |
| | os.environ["HF_TOKEN"] = hf_token |
| | print("Token configuré comme HF_TOKEN") |
| | |
| | |
| | print("\nTentative de connexion à Hugging Face...") |
| | try: |
| | login(token=hf_token, add_to_git_credential=True) |
| | print("Login réussi") |
| | except Exception as e: |
| | print(f"Erreur lors du login : {str(e)}") |
| | raise |
| | |
| | |
| | print("\nVérification de la connexion...") |
| | try: |
| | api = HfApi() |
| | user_info = api.whoami() |
| | print(f"Connecté avec succès en tant que : {user_info['name']}") |
| | print(f"Type de compte : {user_info.get('account_type', 'inconnu')}") |
| | print(f"Organisations : {', '.join(user_info.get('orgs', []))}") |
| | except Exception as e: |
| | print(f"Erreur lors de la vérification de la connexion : {str(e)}") |
| | raise |
| | |
| | print("\n=== Configuration Hugging Face terminée avec succès ===") |
| | return True |
| | |
| | except Exception as e: |
| | print(f"\nErreur lors de la configuration Hugging Face : {str(e)}") |
| | import traceback |
| | print("\nTrace complète :") |
| | print(traceback.format_exc()) |
| | return False |
| |
|
| | if __name__ == "__main__": |
| | setup_huggingface() |
| |
|