Upload push-model.py with huggingface_hub
Browse files- push-model.py +169 -0
push-model.py
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from typing import Optional
|
3 |
+
|
4 |
+
from huggingface_hub import HfApi, create_repo
|
5 |
+
from transformers import AutoConfig, AutoModelForCausalLM, AutoProcessor
|
6 |
+
|
7 |
+
|
8 |
+
class SpecVisionModelRegistration:
|
9 |
+
"""
|
10 |
+
Handles the registration and pushing of SpecVision model to Hugging Face Hub.
|
11 |
+
"""
|
12 |
+
|
13 |
+
def __init__(self,
|
14 |
+
model_path: str,
|
15 |
+
repo_name: str,
|
16 |
+
organization: Optional[str] = None,
|
17 |
+
token: Optional[str] = None):
|
18 |
+
"""
|
19 |
+
Initialize the registration handler.
|
20 |
+
|
21 |
+
Args:
|
22 |
+
model_path: Local path to your model files
|
23 |
+
repo_name: Name for the Hugging Face repository
|
24 |
+
organization: Optional organization name on Hugging Face
|
25 |
+
token: Hugging Face API token
|
26 |
+
"""
|
27 |
+
self.model_path = model_path
|
28 |
+
self.repo_name = repo_name
|
29 |
+
self.organization = organization
|
30 |
+
self.token = token or os.getenv("HF_TOKEN")
|
31 |
+
|
32 |
+
if not self.token:
|
33 |
+
raise ValueError("Please provide a Hugging Face token either directly or via HF_TOKEN environment variable")
|
34 |
+
|
35 |
+
self.api = HfApi()
|
36 |
+
|
37 |
+
def register_model_components(self):
|
38 |
+
"""
|
39 |
+
Register the SpecVision model architecture components with the transformers library.
|
40 |
+
"""
|
41 |
+
# Import your custom model classes
|
42 |
+
from configuration_spec_vision import SpecVisionConfig
|
43 |
+
from modeling_spec_vision import SpecVisionForCausalLM
|
44 |
+
from processing_spec_vision import SpecVisionProcessor
|
45 |
+
|
46 |
+
# Register the model architecture
|
47 |
+
AutoConfig.register("spec_vision", SpecVisionConfig)
|
48 |
+
AutoModelForCausalLM.register(SpecVisionConfig, SpecVisionForCausalLM)
|
49 |
+
AutoProcessor.register(SpecVisionConfig, SpecVisionProcessor)
|
50 |
+
|
51 |
+
print("✓ Successfully registered SpecVision model architecture")
|
52 |
+
|
53 |
+
def create_huggingface_repo(self):
|
54 |
+
"""
|
55 |
+
Create a new repository on the Hugging Face Hub.
|
56 |
+
"""
|
57 |
+
repo_id = f"{self.organization}/{self.repo_name}" if self.organization else self.repo_name
|
58 |
+
|
59 |
+
try:
|
60 |
+
create_repo(
|
61 |
+
repo_id,
|
62 |
+
token=self.token,
|
63 |
+
private=False,
|
64 |
+
exist_ok=True
|
65 |
+
)
|
66 |
+
print(f"✓ Created/accessed repository: {repo_id}")
|
67 |
+
return repo_id
|
68 |
+
except Exception as e:
|
69 |
+
raise Exception(f"Failed to create repository: {str(e)}")
|
70 |
+
|
71 |
+
def update_model_card(self):
|
72 |
+
"""
|
73 |
+
Create or update the model card (README.md) with necessary information.
|
74 |
+
"""
|
75 |
+
model_card = f"""---
|
76 |
+
language: en
|
77 |
+
tags:
|
78 |
+
- spec-vision
|
79 |
+
- vision-language-model
|
80 |
+
- transformers
|
81 |
+
license: apache-2.0
|
82 |
+
---
|
83 |
+
|
84 |
+
# SpecVision Model
|
85 |
+
|
86 |
+
This is the SpecVision model, a vision-language model based on the transformers architecture.
|
87 |
+
|
88 |
+
## Model Description
|
89 |
+
|
90 |
+
SpecVision is designed for vision-language tasks, combining visual and textual understanding capabilities.
|
91 |
+
|
92 |
+
## Usage
|
93 |
+
|
94 |
+
```python
|
95 |
+
from transformers import AutoConfig, AutoModelForCausalLM, AutoProcessor
|
96 |
+
|
97 |
+
# Load the model and processor
|
98 |
+
model = AutoModelForCausalLM.from_pretrained("{self.repo_name}")
|
99 |
+
processor = AutoProcessor.from_pretrained("{self.repo_name}")
|
100 |
+
|
101 |
+
# Process inputs
|
102 |
+
inputs = processor(images=image, text=text, return_tensors="pt")
|
103 |
+
outputs = model(**inputs)
|
104 |
+
```
|
105 |
+
|
106 |
+
## Training and Evaluation
|
107 |
+
|
108 |
+
[Add your training and evaluation details here]
|
109 |
+
|
110 |
+
## Limitations and Biases
|
111 |
+
|
112 |
+
[Add any known limitations and biases here]
|
113 |
+
"""
|
114 |
+
|
115 |
+
with open(os.path.join(self.model_path, "README.md"), "w") as f:
|
116 |
+
f.write(model_card)
|
117 |
+
|
118 |
+
print("✓ Created/updated model card")
|
119 |
+
|
120 |
+
def push_to_hub(self):
|
121 |
+
"""
|
122 |
+
Push the model, configurations, and related files to Hugging Face Hub.
|
123 |
+
"""
|
124 |
+
repo_id = self.create_huggingface_repo()
|
125 |
+
|
126 |
+
# Update the model card first
|
127 |
+
self.update_model_card()
|
128 |
+
|
129 |
+
# Create a dictionary of files to upload
|
130 |
+
files_to_upload = {}
|
131 |
+
for filename in os.listdir(self.model_path):
|
132 |
+
if filename.endswith(('.json', '.py', '.md', '.txt', '.safetensors')):
|
133 |
+
filepath = os.path.join(self.model_path, filename)
|
134 |
+
files_to_upload[filename] = filepath
|
135 |
+
|
136 |
+
# Upload all files
|
137 |
+
for filename, filepath in files_to_upload.items():
|
138 |
+
self.api.upload_file(
|
139 |
+
path_or_fileobj=filepath,
|
140 |
+
path_in_repo=filename,
|
141 |
+
repo_id=repo_id,
|
142 |
+
token=self.token
|
143 |
+
)
|
144 |
+
print(f"✓ Uploaded {filename}")
|
145 |
+
|
146 |
+
print(f"\nModel successfully pushed to https://huggingface.co/{repo_id}")
|
147 |
+
|
148 |
+
def main():
|
149 |
+
"""
|
150 |
+
Main function to execute the registration and push process.
|
151 |
+
"""
|
152 |
+
# You can set your HF_TOKEN as an environment variable or pass it directly
|
153 |
+
TOKEN = os.getenv("HF_TOKEN") # or "your_token_here"
|
154 |
+
|
155 |
+
registration = SpecVisionModelRegistration(
|
156 |
+
model_path="./", # Assuming you're running from the model directory
|
157 |
+
repo_name="Spec-4B-Vision-V1", # Change this to your desired repo name
|
158 |
+
organization="SVECTOR-CORPORATION", # Your organization name
|
159 |
+
token=TOKEN
|
160 |
+
)
|
161 |
+
|
162 |
+
# Register the model architecture
|
163 |
+
registration.register_model_components()
|
164 |
+
|
165 |
+
# Push everything to the Hub
|
166 |
+
registration.push_to_hub()
|
167 |
+
|
168 |
+
if __name__ == "__main__":
|
169 |
+
main()
|