File size: 2,872 Bytes
b47e2cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from huggingface_hub import HfApi, create_repo
from pathlib import Path
import sys


def check_files_exist(model_path):
    """Check if all necessary files exist in the model directory."""
    required_files = [
        "config.json",
        "model.safetensors",
        "special_tokens_map.json",
        "tokenizer.json",
        "tokenizer_config.json",
        "training_args.bin",
        "vocab.txt",
        "README.md"
    ]

    missing_files = []
    for file in required_files:
        if not os.path.exists(os.path.join(model_path, file)):
            missing_files.append(file)

    return missing_files


def upload_model_to_hf(model_path, repo_name, organization=None):
    """
    Upload a model to Hugging Face Hub.

    Args:
        model_path (str): Path to the model directory
        repo_name (str): Name for the repository on Hugging Face
        organization (str, optional): Organization name if uploading to an organization
    """
    try:
        # Initialize the API
        api = HfApi()

        # Check if all required files exist
        missing_files = check_files_exist(model_path)
        if missing_files:
            print(f"Error: Missing required files: {', '.join(missing_files)}")
            return False

        # Create full repository name
        if organization:
            full_repo_name = f"{organization}/{repo_name}"
        else:
            full_repo_name = f"{api.whoami()['name']}/{repo_name}"

        print(f"Creating repository: {full_repo_name}")

        # Create the repository
        try:
            create_repo(
                repo_id=full_repo_name,
                private=False,
                exist_ok=True
            )
        except Exception as e:
            print(f"Error creating repository: {str(e)}")
            return False

        print("Repository created successfully!")

        # Upload the model files
        print(f"Uploading files from {model_path}")
        api.upload_folder(
            folder_path=model_path,
            repo_id=full_repo_name,
            repo_type="model"
        )

        print("Upload completed successfully!")
        print(f"Your model is now available at: https://huggingface.co/{full_repo_name}")
        return True

    except Exception as e:
        print(f"An error occurred: {str(e)}")
        return False


if __name__ == "__main__":
    # Configurazione
    MODEL_PATH = "/Users/erikbranmarino/BERT-PRCT-fine-tuning/ct-bert-finetuned-20250131_120923"  # Il path al tuo modello
    REPO_NAME = "CT-BERT-PRCT"  # Il nome che vuoi dare al repository

    # Verifica che il path esista
    if not os.path.exists(MODEL_PATH):
        print(f"Error: Model path {MODEL_PATH} does not exist!")
        sys.exit(1)

    # Esegui l'upload
    success = upload_model_to_hf(MODEL_PATH, REPO_NAME)

    if not success:
        sys.exit(1)