distilgpt2-werther-finetuned / upload_model.py
ajsbsd's picture
Upload 4 files
3dacdd0 verified
import os
from transformers import AutoModelForCausalLM, AutoTokenizer
# --- 1. Define Paths ---
# Path to your fine-tuned model directory
local_model_path = os.path.join(os.getcwd(), "fine_tuned_werther_model")
# --- 2. Define Hugging Face Hub Repository ID ---
# Replace 'your-username' with your actual Hugging Face username
# Replace 'distilgpt2-werther-finetuned' with your desired model name on the Hub
repo_id = "ajsbsd/distilgpt2-werther-finetuned"
# --- 3. Load Model and Tokenizer from Local Directory ---
print(f"Loading model and tokenizer from local path: {local_model_path}...")
try:
model = AutoModelForCausalLM.from_pretrained(local_model_path)
tokenizer = AutoTokenizer.from_pretrained(local_model_path)
print("Model and tokenizer loaded successfully.")
except Exception as e:
print(f"Error loading local model/tokenizer: {e}")
print("Please ensure the 'fine_tuned_werther_model' directory exists and contains the necessary files.")
exit()
# --- 4. Push to Hugging Face Hub ---
print(f"\nUploading model and tokenizer to Hugging Face Hub: {repo_id}...")
try:
# Use push_to_hub method for model and tokenizer
# The 'commit_message' will appear in your model's history on the Hub
model.push_to_hub(repo_id, commit_message="Fine-tuned DistilGPT2 on The Sorrows of Young Werther")
tokenizer.push_to_hub(repo_id, commit_message="Tokenizer for Werther fine-tuned model")
print("Model and tokenizer uploaded successfully!")
print(f"You can view your model here: https://huggingface.co/{repo_id}")
except Exception as e:
print(f"An error occurred during upload: {e}")
print("Ensure you are logged in to Hugging Face Hub (`huggingface-cli login`) and have write access to the repository.")