File size: 8,063 Bytes
7762e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""
Comprehensive script to publish model and codebase to Hugging Face Hub
"""
import argparse
import os
import sys
from pathlib import Path
from huggingface_hub import HfApi, create_repo, upload_folder, upload_file
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# Add parent directory to path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))


def publish_to_hub(
    model_path: str,
    repo_id: str,
    private: bool = False,
    upload_code: bool = True,
    upload_model: bool = True
):
    """
    Publish model and codebase to Hugging Face Hub.
    
    Args:
        model_path: Path to the trained model
        repo_id: Full repository ID (e.g., "username/repo-name")
        private: Whether to make the repository private
        upload_code: Whether to upload code files
        upload_model: Whether to upload the model
    """
    print("=" * 70)
    print("Publishing to Hugging Face Hub")
    print("=" * 70)
    print(f"\nRepository: {repo_id}")
    print(f"Private: {private}")
    print(f"Upload Model: {upload_model}")
    print(f"Upload Code: {upload_code}")
    
    api = HfApi()
    
    # Create repository
    print("\n[1/4] Creating/verifying repository...")
    try:
        create_repo(
            repo_id=repo_id,
            repo_type="model",
            exist_ok=True,
            private=private
        )
        print(f"βœ“ Repository ready: {repo_id}")
    except Exception as e:
        print(f"βœ— Error creating repository: {e}")
        print("\nMake sure you're logged in:")
        print("  huggingface-cli login")
        return False
    
    # Upload model and tokenizer
    if upload_model:
        print("\n[2/4] Uploading model and tokenizer...")
        try:
            if not os.path.exists(model_path):
                print(f"βœ— Model path not found: {model_path}")
                print("  Skipping model upload. You can upload it later.")
            else:
                tokenizer = AutoTokenizer.from_pretrained(model_path)
                model = AutoModelForSequenceClassification.from_pretrained(model_path)
                
                model.push_to_hub(repo_id)
                tokenizer.push_to_hub(repo_id)
                print("βœ“ Model and tokenizer uploaded")
        except Exception as e:
            print(f"βœ— Error uploading model: {e}")
            print("  You can upload the model separately later.")
    else:
        print("\n[2/4] Skipping model upload (--no-model flag)")
    
    # Upload code files
    if upload_code:
        print("\n[3/4] Uploading code files...")
        try:
            repo_root = Path(__file__).parent.parent
            
            # Files to upload
            code_files = [
                "train.py",
                "inference.py",
                "config.yaml",
                "requirements.txt",
                "setup.py",
                "README.md",
                "MODEL_CARD.md",
                "LICENSE",
                ".gitignore"
            ]
            
            # Directories to upload
            code_dirs = [
                "src",
                "scripts"
            ]
            
            uploaded_count = 0
            
            # Upload individual files
            for file_name in code_files:
                file_path = repo_root / file_name
                if file_path.exists():
                    try:
                        upload_file(
                            path_or_fileobj=str(file_path),
                            path_in_repo=file_name,
                            repo_id=repo_id,
                            repo_type="model"
                        )
                        print(f"  βœ“ Uploaded {file_name}")
                        uploaded_count += 1
                    except Exception as e:
                        print(f"  ⚠ Could not upload {file_name}: {e}")
            
            # Upload directories
            for dir_name in code_dirs:
                dir_path = repo_root / dir_name
                if dir_path.exists() and dir_path.is_dir():
                    try:
                        upload_folder(
                            folder_path=str(dir_path),
                            path_in_repo=dir_name,
                            repo_id=repo_id,
                            repo_type="model",
                            ignore_patterns=["__pycache__", "*.pyc", ".DS_Store"]
                        )
                        print(f"  βœ“ Uploaded {dir_name}/")
                        uploaded_count += 1
                    except Exception as e:
                        print(f"  ⚠ Could not upload {dir_name}/: {e}")
            
            print(f"\nβœ“ Uploaded {uploaded_count} code files/directories")
            
        except Exception as e:
            print(f"βœ— Error uploading code: {e}")
    else:
        print("\n[3/4] Skipping code upload (--no-code flag)")
    
    # Final summary
    print("\n[4/4] Publishing complete!")
    print("\n" + "=" * 70)
    print("Success! πŸŽ‰")
    print("=" * 70)
    print(f"\nYour model is now available at:")
    print(f"https://huggingface.co/{repo_id}")
    
    if upload_model:
        print("\nTo use your model:")
        print(f"""
from transformers import pipeline

classifier = pipeline("text-classification", model="{repo_id}")

# Classify a comment
result = classifier("This function uses dynamic programming for O(n) time complexity")
print(result)
""")
    
    return True


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Publish model and codebase to Hugging Face Hub",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  # Publish everything (model + code)
  python scripts/publish_to_hub.py --repo-id Snaseem2026/code-comment-classifier
  
  # Publish only code (no model)
  python scripts/publish_to_hub.py --repo-id Snaseem2026/code-comment-classifier --no-model
  
  # Publish only model (no code)
  python scripts/publish_to_hub.py --repo-id Snaseem2026/code-comment-classifier --no-code
  
  # Private repository
  python scripts/publish_to_hub.py --repo-id Snaseem2026/code-comment-classifier --private
        """
    )
    parser.add_argument(
        "--model-path",
        type=str,
        default="./results/final_model",
        help="Path to the trained model"
    )
    parser.add_argument(
        "--repo-id",
        type=str,
        default="Snaseem2026/code-comment-classifier",
        help="Full repository ID (e.g., 'username/repo-name')"
    )
    parser.add_argument(
        "--private",
        action="store_true",
        help="Make the repository private"
    )
    parser.add_argument(
        "--no-code",
        action="store_true",
        help="Skip uploading code files"
    )
    parser.add_argument(
        "--no-model",
        action="store_true",
        help="Skip uploading model files"
    )
    parser.add_argument(
        "--yes",
        action="store_true",
        help="Skip confirmation prompt"
    )
    
    args = parser.parse_args()
    
    print("\n" + "=" * 70)
    print("Hugging Face Hub Publishing")
    print("=" * 70)
    print("\nBefore publishing, make sure you:")
    print("1. Have a Hugging Face account")
    print("2. Are logged in: huggingface-cli login")
    print("3. Have reviewed MODEL_CARD.md and README.md")
    print(f"4. Model path exists: {args.model_path} ({'βœ“' if os.path.exists(args.model_path) else 'βœ—'})")
    
    if not args.yes:
        print("\n" + "=" * 70)
        response = input(f"\nProceed with publishing to {args.repo_id}? (yes/no): ")
        if response.lower() not in ['yes', 'y']:
            print("Publishing cancelled.")
            sys.exit(0)
    
    success = publish_to_hub(
        model_path=args.model_path,
        repo_id=args.repo_id,
        private=args.private,
        upload_code=not args.no_code,
        upload_model=not args.no_model
    )
    
    if not success:
        sys.exit(1)