# %pip install setfitfrom huggingface_hub import notebook_login

notebook_login()# !apt install git-lfs# !git config --global user.email "you@example.com"
# !git config --global user.name "Your Name"dataset_id = "sst2"
model_id = "sentence-transformers/paraphrase-mpnet-base-v2"from datasets import load_dataset

dataset = load_dataset(dataset_id)
datasetfrom setfit import sample_dataset

train_dataset = sample_dataset(dataset["train"])
train_dataseteval_dataset = dataset["validation"] from setfit import SetFitModel

model = SetFitModel.from_pretrained(model_id)from sentence_transformers.losses import CosineSimilarityLoss

from setfit import SetFitTrainer

trainer = SetFitTrainer(
    model=model,
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    loss_class=CosineSimilarityLoss,
    num_iterations=20,
    column_mapping={"sentence": "text", "label": "label"},
)trainer.train()metrics = trainer.evaluate()
metricstrainer.push_to_hub("my-awesome-setfit-model")from setfit import SetFitModel

model = SetFitModel.from_pretrained("lewtun/my-awesome-setfit-model")

# Run inference
preds = model(["i loved the spiderman movie!", "pineapple on pizza is the worst 🤮"])
preds   from setfit import SetFitModel

num_classes = len(train_dataset.unique("label"))
model = SetFitModel.from_pretrained(model_id, use_differentiable_head=True, head_params={"out_features": num_classes})trainer = SetFitTrainer(
    model=model,
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    loss_class=CosineSimilarityLoss,
    num_iterations=20,
    column_mapping={"sentence": "text", "label": "label"},
)trainer.freeze()
trainer.train(body_learning_rate=1e-5, num_epochs=1)trainer.unfreeze(keep_body_frozen=True)
trainer.train(learning_rate=1e-2, num_epochs=50)trainer.evaluate()