Upload folder using huggingface_hub
Browse files- upload_model.py +41 -0
upload_model.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# upload_model.py
|
| 3 |
+
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from transformers import TFAutoModelForSequenceClassification, AutoTokenizer
|
| 6 |
+
|
| 7 |
+
# --- 1. EDIT THESE VARIABLES ---
|
| 8 |
+
# The base model architecture you used, e.g., 'bert-base-uncased', 'distilbert-base-cased'
|
| 9 |
+
MODEL_ARCH = 'bert-base-uncased'
|
| 10 |
+
# Path to your saved .h5 file
|
| 11 |
+
H5_WEIGHTS_PATH = './my_model.h5'
|
| 12 |
+
# The name for your new repository on the Hugging Face Hub
|
| 13 |
+
HUB_REPO_ID = "your-hf-username/your-model-name"
|
| 14 |
+
# The number of labels your model was trained to predict
|
| 15 |
+
NUM_LABELS = 2 # Example for binary classification
|
| 16 |
+
|
| 17 |
+
# --- 2. LOAD THE TRANSFORMERS MODEL AND TOKENIZER ---
|
| 18 |
+
print("Loading base tokenizer and model architecture...")
|
| 19 |
+
# Load the tokenizer that corresponds to your model architecture
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ARCH)
|
| 21 |
+
|
| 22 |
+
# Load the model architecture, specifying it's a TensorFlow model and the number of classes
|
| 23 |
+
model = TFAutoModelForSequenceClassification.from_pretrained(MODEL_ARCH, num_labels=NUM_LABELS, from_pt=True)
|
| 24 |
+
|
| 25 |
+
# --- 3. LOAD WEIGHTS FROM YOUR .H5 FILE ---
|
| 26 |
+
# NOTE: The model must be "built" before loading weights.
|
| 27 |
+
# A simple way to do this is to pass a dummy input through it.
|
| 28 |
+
dummy_input = tokenizer("This is a dummy sentence.", return_tensors="tf")
|
| 29 |
+
_ = model(dummy_input) # The output of this call is not needed
|
| 30 |
+
|
| 31 |
+
print(f"Loading weights from {H5_WEIGHTS_PATH}...")
|
| 32 |
+
model.load_weights(H5_WEIGHTS_PATH)
|
| 33 |
+
print("Weights loaded successfully.")
|
| 34 |
+
|
| 35 |
+
# --- 4. PUSH THE MODEL AND TOKENIZER TO THE HUB ---
|
| 36 |
+
print(f"Uploading model and tokenizer to {HUB_REPO_ID}...")
|
| 37 |
+
# This command will create the repository if it doesn't exist
|
| 38 |
+
model.push_to_hub(HUB_REPO_ID)
|
| 39 |
+
tokenizer.push_to_hub(HUB_REPO_ID)
|
| 40 |
+
|
| 41 |
+
print("All done! Your model is now on the Hugging Face Hub.")
|