| |
| |
| """ |
| upload_to_hf.py — Script de sauvegarde et publication des données, datasets et graphiques sur Hugging Face (Dataset Hub). |
| |
| Ce script permet d'uploader les données brutes, les datasets de caractéristiques extraites, |
| les rapports d'évaluation, les graphiques générés (PNG/HTML) et le code du pipeline |
| sur le Hugging Face Dataset Hub. Il permet de reprendre l'entraînement ultérieurement |
| ou de partager les résultats de l'étude. |
| |
| Usage: |
| python scripts/upload_to_hf.py --repo_id "votre-username/nom-du-dataset" --token "HF_TOKEN" |
| """ |
|
|
| import os |
| import sys |
| import argparse |
| import yaml |
| from huggingface_hub import HfApi |
|
|
| def load_config(config_path="configs/config.yaml"): |
| if os.path.exists(config_path): |
| with open(config_path, "r", encoding="utf-8") as f: |
| return yaml.safe_load(f) |
| return {} |
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="Upload datasets, plots, reports, and code to Hugging Face Datasets.") |
| parser.add_argument("--repo_id", required=True, help="HF Dataset Repository ID (e.g., 'username/detecteur-ia-parlement-dataset')") |
| parser.add_argument("--token", help="Hugging Face Write Token (or set HF_TOKEN env var)") |
| parser.add_argument("--repo_type", default="dataset", choices=["dataset", "space"], help="HF Repository Type") |
| parser.add_argument("--space_sdk", default="gradio", help="SDK if uploading as a Space (defaults to gradio)") |
| parser.add_argument("--exclude_models", type=bool, default=True, help="Exclude binary model checkpoints (.pkl files in models/)") |
| parser.add_argument("--config", default="configs/config.yaml", help="Path to config file") |
| args = parser.parse_args() |
| |
| repo_id = args.repo_id.strip() if args.repo_id else "" |
| token = args.token or os.environ.get("HF_TOKEN") |
| if token: |
| token = token.strip() |
| else: |
| print("Error: Hugging Face API token is required. Use --token or set the HF_TOKEN environment variable.") |
| sys.exit(1) |
| |
| api = HfApi(token=token) |
| |
| |
| print(f"Checking/Creating Hugging Face repository '{repo_id}' (Type: {args.repo_type})...") |
| try: |
| api.create_repo( |
| repo_id=repo_id, |
| repo_type=args.repo_type, |
| space_sdk=args.space_sdk if args.repo_type == "space" else None, |
| exist_ok=True |
| ) |
| print("Repository is ready.") |
| except Exception as e: |
| print(f"Error creating/retrieving repository: {e}") |
| sys.exit(1) |
| |
| |
| workspace_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| print(f"Uploading files from local workspace '{workspace_dir}' to HF repo...") |
| |
| |
| ignore_patterns = [ |
| "**/.git/**", |
| "**/__pycache__/**", |
| "**/*.pyc", |
| "**/.ipynb_checkpoints/**", |
| "**/.gradio/**", |
| "models_upload_temp/**", |
| "hf_model_upload_*/**" |
| ] |
| |
| if args.exclude_models: |
| print("Excluding heavy binary models (.pkl files in models/) to keep dataset repo clean...") |
| ignore_patterns.append("models/**/*.pkl") |
| ignore_patterns.append("models/*.pkl") |
| else: |
| print("Including binary models in the upload...") |
| |
| |
| |
| |
| |
| try: |
| api.upload_folder( |
| folder_path=workspace_dir, |
| repo_id=repo_id, |
| repo_type=args.repo_type, |
| ignore_patterns=ignore_patterns |
| ) |
| print("\n🎉 Success! All data files, datasets, reports, plots, and scripts have been uploaded to Hugging Face.") |
| |
| if args.repo_type == "dataset": |
| print(f"Your dataset repository is live at: https://huggingface.co/datasets/{repo_id}") |
| print("\nTo resume training or run the pipeline elsewhere, you can clone this dataset repository:") |
| print(f" git clone https://huggingface.co/datasets/{repo_id}") |
| elif args.repo_type == "space": |
| print(f"Your application space is live at: https://huggingface.co/spaces/{repo_id}") |
| |
| except Exception as e: |
| print(f"Error uploading files: {e}") |
| sys.exit(1) |
|
|
| if __name__ == "__main__": |
| main() |
|
|