hf-pipeline-API / app.py
iShare's picture
Create app.py
7632f13
raw
history blame
1.21 kB
#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)