Give me a python program using huggingface's transformers to performan sentiment analysis
- User
Give me a python program using huggingface's transformers to performan sentiment analysis
- Assistant
To perform sentiment analysis using Hugging Face's Transformers library in Python, you first need to install the
transformerspackage. You can do this via pip:pip install transformersOnce installed, you can use the following Python program to perform sentiment analysis. This example uses the
pipelinefunctionality from the Transformers library to easily accomplish this task with just a few lines of code. We'll use a pretrained model that's suitable for sentiment analysis.from transformers import pipeline # Load the sentiment analysis pipeline sentiment_pipeline = pipeline("sentiment-analysis") # Text to analyze text = "I love using Hugging Face's transformers. They make NLP tasks so much easier!" # Perform sentiment analysis results = sentiment_pipeline(text) # Print the results print(results) # If you want to process multiple texts at once texts = ["I love using Hugging Face's transformers. They make NLP tasks so much easier!", "I'm really disappointed with the latest update. It has so many bugs!"] results = sentiment_pipeline(texts) for result, text in zip(results, texts): print(f"Text: {text}\nSentiment: {result['label']}, Score: {round(result['score'], 2)}\n")