国内使用transformers库的方法

创建日期:2024-06-21
更新日期:2024-06-21

使用镜像网站

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)