File size: 5,499 Bytes
869063e |
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 |
import os
from typing import Optional
from huggingface_hub import HfApi, create_repo
from transformers import AutoConfig, AutoModelForCausalLM, AutoProcessor
class SpecVisionModelRegistration:
"""
Handles the registration and pushing of SpecVision model to Hugging Face Hub.
"""
def __init__(self,
model_path: str,
repo_name: str,
organization: Optional[str] = None,
token: Optional[str] = None):
"""
Initialize the registration handler.
Args:
model_path: Local path to your model files
repo_name: Name for the Hugging Face repository
organization: Optional organization name on Hugging Face
token: Hugging Face API token
"""
self.model_path = model_path
self.repo_name = repo_name
self.organization = organization
self.token = token or os.getenv("HF_TOKEN")
if not self.token:
raise ValueError("Please provide a Hugging Face token either directly or via HF_TOKEN environment variable")
self.api = HfApi()
def register_model_components(self):
"""
Register the SpecVision model architecture components with the transformers library.
"""
# Import your custom model classes
from configuration_spec_vision import SpecVisionConfig
from modeling_spec_vision import SpecVisionForCausalLM
from processing_spec_vision import SpecVisionProcessor
# Register the model architecture
AutoConfig.register("spec_vision", SpecVisionConfig)
AutoModelForCausalLM.register(SpecVisionConfig, SpecVisionForCausalLM)
AutoProcessor.register(SpecVisionConfig, SpecVisionProcessor)
print("✓ Successfully registered SpecVision model architecture")
def create_huggingface_repo(self):
"""
Create a new repository on the Hugging Face Hub.
"""
repo_id = f"{self.organization}/{self.repo_name}" if self.organization else self.repo_name
try:
create_repo(
repo_id,
token=self.token,
private=False,
exist_ok=True
)
print(f"✓ Created/accessed repository: {repo_id}")
return repo_id
except Exception as e:
raise Exception(f"Failed to create repository: {str(e)}")
def update_model_card(self):
"""
Create or update the model card (README.md) with necessary information.
"""
model_card = f"""---
language: en
tags:
- spec-vision
- vision-language-model
- transformers
license: apache-2.0
---
# SpecVision Model
This is the SpecVision model, a vision-language model based on the transformers architecture.
## Model Description
SpecVision is designed for vision-language tasks, combining visual and textual understanding capabilities.
## Usage
```python
from transformers import AutoConfig, AutoModelForCausalLM, AutoProcessor
# Load the model and processor
model = AutoModelForCausalLM.from_pretrained("{self.repo_name}")
processor = AutoProcessor.from_pretrained("{self.repo_name}")
# Process inputs
inputs = processor(images=image, text=text, return_tensors="pt")
outputs = model(**inputs)
```
## Training and Evaluation
[Add your training and evaluation details here]
## Limitations and Biases
[Add any known limitations and biases here]
"""
with open(os.path.join(self.model_path, "README.md"), "w") as f:
f.write(model_card)
print("✓ Created/updated model card")
def push_to_hub(self):
"""
Push the model, configurations, and related files to Hugging Face Hub.
"""
repo_id = self.create_huggingface_repo()
# Update the model card first
self.update_model_card()
# Create a dictionary of files to upload
files_to_upload = {}
for filename in os.listdir(self.model_path):
if filename.endswith(('.json', '.py', '.md', '.txt', '.safetensors')):
filepath = os.path.join(self.model_path, filename)
files_to_upload[filename] = filepath
# Upload all files
for filename, filepath in files_to_upload.items():
self.api.upload_file(
path_or_fileobj=filepath,
path_in_repo=filename,
repo_id=repo_id,
token=self.token
)
print(f"✓ Uploaded {filename}")
print(f"\nModel successfully pushed to https://huggingface.co/{repo_id}")
def main():
"""
Main function to execute the registration and push process.
"""
# You can set your HF_TOKEN as an environment variable or pass it directly
TOKEN = os.getenv("HF_TOKEN") # or "your_token_here"
registration = SpecVisionModelRegistration(
model_path="./", # Assuming you're running from the model directory
repo_name="Spec-4B-Vision-V1", # Change this to your desired repo name
organization="SVECTOR-CORPORATION", # Your organization name
token=TOKEN
)
# Register the model architecture
registration.register_model_components()
# Push everything to the Hub
registration.push_to_hub()
if __name__ == "__main__":
main() |