Views
No views yet
1from sentence_transformers import CrossEncoder
2model = CrossEncoder('akiFQC/bert-base-japanese-v3_nli-jsnli')
3scores = model.predict([('男はピザを食べています', '男は何かを食べています'), ('黒いレーシングカーが観衆の前から発車します。', '男は誰もいない道を運転しています。')])
4
5#Convert scores to labels
6label_mapping = ['entailment', 'neutral', 'contradiction',]
7labels = [label_mapping[score_max] for score_max in scores.argmax(axis=1)]1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/nli-deberta-v3-base')
5tokenizer = AutoTokenizer.from_pretrained('cross-encoder/nli-deberta-v3-base')
6
7features = tokenizer(['男はピザを食べています', '黒いレーシングカーが観衆の前から発車します。'], ['男は何かを食べています', '男は誰もいない道を運転しています。'], padding=True, truncation=True, return_tensors="pt")
8
9model.eval()
10with torch.no_grad():
11 scores = model(**features).logits
12 label_mapping = ['contradiction', 'entailment', 'neutral']
13 labels = [label_mapping[score_max] for score_max in scores.argmax(dim=1)]
14 print(labels)1from transformers import pipeline
2
3classifier = pipeline("zero-shot-classification", model='akiFQC/bert-base-japanese-v3_nli-jsnli')
4
5sent = "Appleは先程、iPhoneの最新機種について発表しました。"
6candidate_labels = ["技術", "スポーツ", "政治"]
7res = classifier(sent, candidate_labels)
8print(res)