File size: 1,557 Bytes
61794f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# File 1: Model Repo Code (train.py)
# This file contains steps 1 to 4

from datasets import load_dataset
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, TrainingArguments, Trainer

# Step 1: Load the Dataset
dataset = load_dataset("squad")

# Step 2: Preprocess the Dataset
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")

def preprocess_function(examples):
    return tokenizer(
        examples["question"],
        examples["context"],
        truncation=True,
        max_length=384,
        stride=128,
        return_overflowing_tokens=True,
        padding="max_length"
    )

tokenized_dataset = dataset.map(preprocess_function, batched=True)

# Step 3: Train the Model
model = AutoModelForQuestionAnswering.from_pretrained("bert-base-uncased")

training_args = TrainingArguments(
    output_dir="./results",
    evaluation_strategy="epoch",
    learning_rate=3e-5,
    per_device_train_batch_size=16,
    num_train_epochs=3,
    weight_decay=0.01,
    push_to_hub=True,  # Automatically push to the Hugging Face Hub
    hub_model_id="username/qa_model_repo"  # Replace with your username and model repo name
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_dataset["train"],
    eval_dataset=tokenized_dataset["validation"],
)

trainer.train()

# Step 4: Push the Model and Tokenizer to Hugging Face Hub
model.push_to_hub("username/qa_model_repo")
tokenizer.push_to_hub("username/qa_model_repo")

print("Model and tokenizer pushed to Hugging Face Hub successfully!")