File size: 1,206 Bytes
7632f13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#Python版本的transformers pipeline API
#https://huggingface.co/docs/transformers/main_classes/pipelines

#Java版本的transformers pipeline API
#https://huggingface.co/docs/transformers.js/pipelines#available-tasks

#Python版本示例:https://huggingface.co/docs/transformers/main_classes/pipelines
from transformers import pipeline, AutoModelForTokenClassification, AutoTokenizer

# Sentiment analysis pipeline
analyzer = pipeline("sentiment-analysis")
#sentiment-analysis的default model是distilbert-base-uncased-finetuned-sst-2-english
#https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english?text=I+like+you.+I+love+you
#https://huggingface.co/blog/sentiment-analysis-python

# Question answering pipeline, specifying the checkpoint identifier
oracle = pipeline(
    "question-answering", model="distilbert-base-cased-distilled-squad", tokenizer="bert-base-cased"
)

# Named entity recognition pipeline, passing in a specific model and tokenizer
model = AutoModelForTokenClassification.from_pretrained("dbmdz/bert-large-cased-finetuned-conll03-english")
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
recognizer = pipeline("ner", model=model, tokenizer=tokenizer)