Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials AI Hugging Face & Transformers
AI Intermediate FREE

Hugging Face & Transformers

Lesson 2 of 17 Intermediate Interactive

Hugging Face provides pre-trained models and tools for NLP tasks.

Syntax

AI
from transformers import pipeline

# Sentiment Analysis
classifier = pipeline("sentiment-analysis")
result = classifier("I love this!")

# Text Generation
generator = pipeline("text-generation", model="gpt2")
Hugging Face Transformers
PYTHON
# Hugging Face concept (conceptual)
# from transformers import pipeline

# classifier = pipeline("sentiment-analysis")

def simple_sentiment(text):
    positive = ["good", "great", "love", "excellent", "amazing"]
    negative = ["bad", "terrible", "hate", "awful", "worst"]

    pos_count = sum(1 for w in positive if w in text.lower())
    neg_count = sum(1 for w in negative if w in text.lower())

    if pos_count > neg_count:
        return {"label": "POSITIVE", "score": 0.95}
    elif neg_count > pos_count:
        return {"label": "NEGATIVE", "score": 0.88}
    return {"label": "NEUTRAL", "score": 0.5}

print(simple_sentiment("This is great!"))
print(simple_sentiment("This is terrible!"))