Uploading Model to Hugging Face Hub
To upload your model to the Hugging Face Hub, you'll need to use the huggingface_hub library. Here's a step-by-step guide:
Step 1: Install huggingface_hub
First, ensure you have the huggingface_hub library installed. If not, install it using pip:
!pip install huggingface_hub
Step 2: Login to Hugging Face Hub
Before uploading your model, you need to log in to the Hugging Face Hub using your write token. You can do this using the huggingface_hub library:
from huggingface_hub import login
# Replace 'your_write_token' with your actual Hugging Face write token
write_token = 'your_write_token'
login(token=write_token)
Step 3: Upload Model to Hugging Face Hub
Now, you can upload your model to the Hugging Face Hub. You'll need to create a repository and upload the necessary files.
from huggingface_hub import HfApi, Repository
import os
# Define your model name and repository name
model_name = "prdev/mini-gte"
repo_name = "your_username/your_repo_name" # Replace with your desired repo name
# Create a new repository
api = HfApi()
api.create_repo(repo_id=repo_name, repo_type="model", exist_ok=True)
# Clone the repository
repo = Repository(
local_dir=repo_name.split("/")[-1],
repo_id=repo_name,
repo_type="model",
)
# Save the ONNX model
onnx_model_path = f"{model_name.split('/')[-1]}.onnx"
onnx_model_name = "model.onnx"
os.replace(onnx_model_path, os.path.join(repo.local_dir, onnx_model_name))
# Save the model configuration
model.config.save_pretrained(repo.local_dir)
# Push the changes to the Hugging Face Hub
repo.push_to_hub(commit_message="Initial commit")
Step 4: Verify the Upload
After uploading, verify that your model is available on the Hugging Face Hub by checking the repository.
Loading the Model on Client-Side
To load the model on a client-side website using transformers, you'll need to use the ORTModelForSequenceClassification class from the optimum.onnxruntime library. Here's an example:
from optimum.onnxruntime import ORTModelForSequenceClassification
from transformers import AutoTokenizer
# Load the model and tokenizer
model_name = "your_username/your_repo_name"
model = ORTModelForSequenceClassification.from_pretrained(model_name, file_name="model.onnx")
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Use the model for inference
inputs = tokenizer("Your input text", return_tensors="pt")
outputs = model(**inputs)
Make sure to replace "your_username/your_repo_name" with your actual repository name.
Full Code
Here's the full code to upload your model to the Hugging Face Hub:
# Install necessary packages
!pip install huggingface_hub
# Import necessary libraries
from huggingface_hub import login, HfApi, Repository
import os
# Login to Hugging Face Hub
write_token = 'your_write_token'
login(token=write_token)
# Define your model name and repository name
model_name = "prdev/mini-gte"
repo_name = "your_username/your_repo_name"
# Create a new repository
api = HfApi()
api.create_repo(repo_id=repo_name, repo_type="model", exist_ok=True)
# Clone the repository
repo = Repository(
local_dir=repo_name.split("/")[-1],
repo_id=repo_name,
repo_type="model",
)
# Save the ONNX model
onnx_model_path = f"{model_name.split('/')[-1]}.onnx"
onnx_model_name = "model.onnx"
os.replace(onnx_model_path, os.path.join(repo.local_dir, onnx_model_name))
# Load the original model
from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Save the model configuration
model.config.save_pretrained(repo.local_dir)
# Push the changes to the Hugging Face Hub
repo.push_to_hub(commit_message="Initial commit")
Replace 'your_write_token', 'your_username', and 'your_repo_name' with your actual Hugging Face write token, username, and desired repository name.