|

AI Project: Build a Sentiment Analyzer with Hugging Face in 5 Lines

3D illustration of a Hugging Face device instantly converting text input into a positive sentiment smiley face output.

This is a perfect first project to show the power of our Hugging Face Hub. We will use a pre-trained AI model to instantly determine if a sentence is positive or negative.

Step 1: Installation

pip install transformers

(You may also need pip install torch if you don’t have it).

Step 2: The Code

from transformers import pipeline

# 1. Load the pipeline (This downloads a pre-trained model for you)
classifier = pipeline("sentiment-analysis")

# 2. Define your text
text = "I love this product! It's the best thing I've ever bought."

# 3. Classify!
result = classifier(text)

# 4. Print the result
print(result)

Step 3: Understanding the Output

The output will look like this: [{'label': 'POSITIVE', 'score': 0.999874...}]

The model is 99.98% sure that this text is POSITIVE.

Let’s try a negative one:

result = classifier("This movie was terrible. I fell asleep.")
print(result)
# Output: [{'label': 'NEGATIVE', 'score': 0.9997...}]

How to Use This

You can now build powerful apps!

  • Loop through a CSV of customer reviews to find all the unhappy ones.
  • Analyze live tweets about your brand.
  • Auto-sort feedback emails.

Similar Posts

Leave a Reply