Pipeline di Hugging Face per l'analisi del sentiment

Natural Language Processing (NLP) in Python

Fouad Trad

Machine Learning Engineer

Ricapitolo: workflow NLP

Il workflow completo: il Capitolo 1 ha coperto il preprocessing e il Capitolo 2 l'estrazione di feature.

Natural Language Processing (NLP) in Python

Pipeline di Hugging Face

Il workflow completo: i Capitoli 3 e 4 tratteranno le pipeline di Hugging Face che racchiudono tutti i passaggi: preprocessing, estrazione di feature e modellazione.

  • Workflow pronto all'uso che gestisce tutto in una funzione
  • Per definire una pipeline servono:
    • Task NLP
    • Modello per il task
Natural Language Processing (NLP) in Python

Pipeline per l'analisi del sentiment

 

 

  • Task di classificazione del testo
  • Predice se il testo esprime emozione positiva o negativa

Immagine con una faccina felice con pollice in su per sentiment positivo e una faccina triste con pollice in giù per sentiment negativo.

Natural Language Processing (NLP) in Python

Modelli per la classificazione del testo

Una gif che mostra come scorrere il sito e selezionare il task giusto per trovare modelli adatti.

1 https://huggingface.co/models
Natural Language Processing (NLP) in Python

Pipeline nel codice

from transformers import pipeline

classification_pipeline = pipeline(
task="sentiment-analysis", # or text-classification
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english" )
result = classification_pipeline("I really liked the movie!!")
print(result)
[{'label': 'POSITIVE', 'score': 0.9998093247413635}]
Natural Language Processing (NLP) in Python

Analisi del sentiment su un batch di testi

texts = ["I really liked the movie!!",
         "Great job ruining my day.",
         "This product exceeded my expectations.",
         "Wow, just what I needed... another problem.", 
         "Absolutely fantastic experience!"]

results = classification_pipeline(texts)
print(results)
[{'label': 'POSITIVE', 'score': 0.9998093247413635}, 
 {'label': 'NEGATIVE', 'score': 0.8666700124740601}, 
 {'label': 'POSITIVE', 'score': 0.998874843120575}, 
 {'label': 'POSITIVE', 'score': 0.98626708984375}, 
 {'label': 'POSITIVE', 'score': 0.9998812675476074}]
Natural Language Processing (NLP) in Python

Valutare i modelli di sentiment analysis

texts = ["I really liked the movie!!",
         "Great job ruining my day.",
         "This product exceeded my expectations.",
         "Wow, just what I needed... another problem.", 
         "Absolutely fantastic experience!"]

true_labels = ["POSITIVE", "NEGATIVE", "POSITIVE", "NEGATIVE", "POSITIVE"]
results = classification_pipeline(texts)
predicted_labels = [result['label'] for result in results]
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(true_labels, predicted_labels)

print(f"Accuracy: {accuracy}")
Accuracy: 0.80
Natural Language Processing (NLP) in Python

Passons à la pratique !

Natural Language Processing (NLP) in Python

Preparing Video For Download...