File size: 7,164 Bytes
f9a6349
 
 
 
 
 
 
 
 
f71f431
f9a6349
 
 
 
 
f71f431
 
 
 
 
f9a6349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Script to push VINE model to HuggingFace Hub

This script helps you push your trained VINE model to the HuggingFace Hub
for easy sharing and distribution.
"""

import os
import sys
from pathlib import Path
import torch
import argparse
from huggingface_hub import notebook_login
from transformers.pipelines import PIPELINE_REGISTRY

# Add src/ to sys.path so LASER, video-sam2, GroundingDINO are importable
current_dir = Path(__file__).resolve().parent
src_dir = current_dir.parent / "src"
if src_dir.is_dir() and str(src_dir) not in sys.path:
    sys.path.insert(0, str(src_dir))

os.environ['OPENAI_API_KEY'] = "dummy-key"
from vine_hf import VineConfig, VineModel, VinePipeline


def push_vine_to_hub(
    model_weights_path: str,
    repo_name: str,
    model_name: str = "openai/clip-vit-base-patch32",
    segmentation_method: str = "grounding_dino_sam2",
    commit_message: str = "Upload VINE model",
    private: bool = False
):
    """
    Push VINE model to HuggingFace Hub.
    
    Args:
        model_weights_path: Path to the trained model weights (.pth file)
        repo_name: Name for the repository (e.g., "username/vine-model")
        model_name: CLIP model backbone name
        segmentation_method: Segmentation method used
        commit_message: Commit message for the push
        private: Whether to create a private repository
    """
    
    print("=== Pushing VINE Model to HuggingFace Hub ===")
    
    # 1. Create configuration
    print(f"Creating configuration with backbone: {model_name}")
    config = VineConfig(
        model_name=model_name,
        segmentation_method=segmentation_method
    )
    
    # 2. Initialize model
    print("Initializing model...")
    model = VineModel(config)
    
    # 3. Load trained weights
    if os.path.exists(model_weights_path):
        print(f"Loading weights from: {model_weights_path}")
        try:
            # Try loading with weights_only=False for compatibility
            weights = torch.load(model_weights_path, map_location='cpu', weights_only=False)
            
            # Handle different weight formats
            if isinstance(weights, dict):
                if 'state_dict' in weights:
                    model.load_state_dict(weights['state_dict'])
                elif 'model' in weights:
                    model.load_state_dict(weights['model'])
                else:
                    model.load_state_dict(weights)
            else:
                # Assume it's the model directly
                model = weights
                
            print("βœ“ Weights loaded successfully")
        except Exception as e:
            print(f"βœ— Error loading weights: {e}")
            print("Please check your weights file format")
            return False
    else:
        print(f"βœ— Weights file not found: {model_weights_path}")
        return False
    
    # 4. Register for auto classes
    print("Registering for auto classes...")
    config.register_for_auto_class()
    model.register_for_auto_class("AutoModel")
    
    # 5. Register pipeline
    print("Registering pipeline...")
    PIPELINE_REGISTRY.register_pipeline(
        "vine-video-understanding",
        pipeline_class=VinePipeline,
        pt_model=VineModel,
        type="multimodal",
    )
    
    # 6. Create pipeline instance
    print("Creating pipeline...")
    vine_pipeline = VinePipeline(model=model, tokenizer=None)
    
    try:
        # 7. Push configuration to hub
        print(f"Pushing configuration to {repo_name}...")
        config.push_to_hub(
            repo_name,
            commit_message=f"{commit_message} - config",
            private=private
        )
        print("βœ“ Configuration pushed successfully")
        
        # 8. Push model to hub
        print(f"Pushing model to {repo_name}...")
        model.push_to_hub(
            repo_name,
            commit_message=f"{commit_message} - model",
            private=private
        )
        print("βœ“ Model pushed successfully")
        
        # 9. Push pipeline to hub
        print(f"Pushing pipeline to {repo_name}...")
        vine_pipeline.push_to_hub(
            repo_name,
            commit_message=f"{commit_message} - pipeline",
            private=private
        )
        print("βœ“ Pipeline pushed successfully")
        
        print(f"\nπŸŽ‰ Successfully pushed VINE model to: https://huggingface.co/{repo_name}")
        print(f"\nTo use your model:")
        print(f"```python")
        print(f"from transformers import pipeline")
        print(f"")
        print(f"vine_pipeline = pipeline(")
        print(f"    'vine-video-understanding',")
        print(f"    model='{repo_name}',")
        print(f"    trust_remote_code=True")
        print(f")")
        print(f"")
        print(f"results = vine_pipeline(")
        print(f"    'path/to/video.mp4',")
        print(f"    categorical_keywords=['human', 'dog', 'frisbee'],")
        print(f"    unary_keywords=['running', 'jumping'],")
        print(f"    binary_keywords=['chasing', 'behind']")
        print(f")")
        print(f"```")
        
        return True
        
    except Exception as e:
        print(f"βœ— Error pushing to hub: {e}")
        print("Please check your HuggingFace credentials and repository permissions")
        return False


def main():
    parser = argparse.ArgumentParser(description="Push VINE model to HuggingFace Hub")
    
    parser.add_argument(
        "--weights", 
        type=str, 
        required=True,
        help="Path to the trained model weights (.pth file)"
    )
    
    parser.add_argument(
        "--repo", 
        type=str, 
        required=True,
        help="Repository name (e.g., 'username/vine-model')"
    )
    
    parser.add_argument(
        "--model-name",
        type=str,
        default="openai/clip-vit-base-patch32",
        help="CLIP model backbone name"
    )
    
    parser.add_argument(
        "--segmentation",
        type=str,
        default="grounding_dino_sam2",
        choices=["sam2", "grounding_dino_sam2"],
        help="Segmentation method"
    )
    
    parser.add_argument(
        "--message",
        type=str,
        default="Upload VINE model",
        help="Commit message"
    )
    
    parser.add_argument(
        "--private",
        action="store_true",
        help="Create private repository"
    )
    
    parser.add_argument(
        "--login",
        action="store_true",
        help="Login to HuggingFace Hub first"
    )
    
    args = parser.parse_args()
    
    # Login if requested
    if args.login:
        print("Logging in to HuggingFace Hub...")
        notebook_login()
    
    # Push model
    success = push_vine_to_hub(
        model_weights_path=args.weights,
        repo_name=args.repo,
        model_name=args.model_name,
        segmentation_method=args.segmentation,
        commit_message=args.message,
        private=args.private
    )
    
    if success:
        print("\nβœ… Model successfully pushed to HuggingFace Hub!")
    else:
        print("\n❌ Failed to push model to HuggingFace Hub")
        sys.exit(1)


if __name__ == "__main__":
    main()