知识库

国内使用transformers库的方法

2024-06-21 21:29:04 李腾 1 次阅读

使用镜像网站

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)

转载请注明转自www.hylab.cn,原文地址:国内使用transformers库的方法

评论 (0)

登录后发表评论

暂无评论,快来发表第一条评论吧!