Sentiment Analysis via Huggingface Transformers Pipeline

  • Share this:

Code introduction


The function uses the pipeline feature from the Huggingface Transformers library to perform sentiment analysis on the input text, returning sentiment and confidence scores.


Technology Stack : Huggingface Transformers, pipeline

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def generate_random_sentiment_analysis(text, model_name="distilbert-base-uncased"):
    from transformers import pipeline

    # Load sentiment analysis pipeline
    nlp = pipeline("sentiment-analysis", model=model_name)

    # Perform sentiment analysis on the given text
    result = nlp(text)

    # Return the sentiment and confidence scores
    return result