File size: 1,759 Bytes
3dacdd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")