The Hugging Face Transformers library helps you use powerful language models easily. It lets you understand and generate text like a human.
Hugging Face Transformers library in NLP
from transformers import pipeline # Create a task pipeline nlp = pipeline('task_name') # Use the pipeline on your text result = nlp('Your input text here')
Replace 'task_name' with tasks like 'sentiment-analysis', 'translation_en_to_fr', 'question-answering', etc.
The pipeline handles loading the model and tokenizer automatically.
from transformers import pipeline # Sentiment analysis pipeline nlp = pipeline('sentiment-analysis') result = nlp('I love learning AI!')
from transformers import pipeline # Translation pipeline translator = pipeline('translation_en_to_fr') result = translator('Hello, how are you?')
from transformers import pipeline # Question answering pipeline qa = pipeline('question-answering') result = qa({'question': 'What is AI?', 'context': 'AI means artificial intelligence.'})
This program uses the Hugging Face Transformers library to check if the sentence is positive or negative.
from transformers import pipeline # Create a sentiment analysis pipeline sentiment = pipeline('sentiment-analysis') # Analyze sentiment of a sentence result = sentiment('I am very happy to learn about Hugging Face!') print(result)
Make sure you have internet connection the first time to download models.
You can specify different models by adding the model name in the pipeline, e.g., pipeline('sentiment-analysis', model='distilbert-base-uncased-finetuned-sst-2-english').
Transformers support many tasks beyond text, like image and audio, but text is the most common.
Hugging Face Transformers library makes using language models easy with simple pipelines.
You can do many tasks like sentiment analysis, translation, and question answering with just a few lines of code.
The library downloads and manages models for you, so you focus on your application.