使用镜像网站
import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
from transformers import pipeline
classifier = pipeline("sentiment-analysis", framework="pt", trust_remote_code=True)
result = classifier(
"We are very happy to introduce pipeline to the transformers repository."
)
print(result)
使用本地模型
from transformers import (
pipeline,
DistilBertTokenizer,
DistilBertForSequenceClassification,
)
path = "E:\\temp\\models\\models--distilbert--distilbert-base-uncased-finetuned-sst-2-english\\snapshots\\714eb0fa89d2f80546fda750413ed43d93601a13"
tokenizer = DistilBertTokenizer.from_pretrained(path)
model = DistilBertForSequenceClassification.from_pretrained(path)
classifier = pipeline(
"sentiment-analysis", model=model, tokenizer=tokenizer, framework="pt"
)
result = classifier(
"We are very happy to introduce pipeline to the transformers repository."
)
print(result)