|
|
|
|
|
|
|
!pip install transformers datasets huggingface_hub |
|
|
|
from datasets import load_dataset |
|
from transformers import DistilBERTForSequenceClassification, DistilBERTTokenizer, Trainer, TrainingArguments |
|
from huggingface_hub import HfApi |
|
|
|
|
|
dataset = load_dataset('imdb') |
|
|
|
|
|
tokenizer = DistilBERTTokenizer.from_pretrained('distilbert-base-uncased') |
|
model = DistilBERTForSequenceClassification.from_pretrained('distilbert-base-uncased') |
|
|
|
|
|
def tokenize_function(examples): |
|
return tokenizer(examples['text'], padding='max_length', truncation=True) |
|
|
|
tokenized_datasets = dataset.map(tokenize_function, batched=True) |
|
tokenized_datasets = tokenized_datasets.remove_columns(['text']) |
|
tokenized_datasets.set_format('torch') |
|
|
|
|
|
training_args = TrainingArguments( |
|
output_dir='./results', |
|
evaluation_strategy='epoch', |
|
learning_rate=2e-5, |
|
per_device_train_batch_size=16, |
|
per_device_eval_batch_size=16, |
|
num_train_epochs=3, |
|
weight_decay=0.01, |
|
) |
|
|
|
|
|
trainer = Trainer( |
|
model=model, |
|
args=training_args, |
|
train_dataset=tokenized_datasets['train'], |
|
eval_dataset=tokenized_datasets['test'], |
|
) |
|
|
|
|
|
trainer.train() |
|
|
|
|
|
model.save_pretrained("imdb-distilbert") |
|
tokenizer.save_pretrained("imdb-distilbert") |
|
|
|
|
|
!huggingface-cli login --token seu-token |
|
|
|
|
|
api = HfApi() |
|
api.upload_folder( |
|
folder_path="imdb-distilbert", |
|
path_in_repo="", |
|
repo_id="seu-username/imdb-distilbert", |
|
repo_type="model" |
|
) |
|
|
|
print("Deploy completo! Acesse seu modelo no Hugging Face para mais detalhes.") |
|
|