Hugging Face-pipelines för sentimentanalys

Natural Language Processing (NLP) i Python

Fouad Trad

Machine Learning Engineer

Repetition: NLP-arbetsflöde

Det fullständiga arbetsflödesdiagrammet som visar att kapitel 1 behandlade förbehandling och kapitel 2 behandlade särdragsextraktion.

Natural Language Processing (NLP) i Python

Hugging Face-pipelines

Det fullständiga arbetsflödesdiagrammet som visar att kapitel 3 och 4 tar upp Hugging Face-pipelines som hanterar alla steg: förbehandling, särdragsextraktion och modellering.

  • Färdigt arbetsflöde som hanterar alla steg i ett funktionsanrop
  • Att definiera en pipeline kräver:
    • NLP-uppgift
    • Modell för uppgiften
Natural Language Processing (NLP) i Python

Pipelines för sentimentanalys

 

 

  • Textklassificeringsuppgift
  • Förutsäger om text uttrycker positiv eller negativ känsla

Bild som visar ett glatt ansikte med tummen upp för positivt sentiment och ett ledset ansikte med tummen ned för negativt sentiment.

Natural Language Processing (NLP) i Python

Modeller för textklassificering

En gif som visar hur man bläddrar på webbplatsen och väljer rätt uppgift för att hitta lämpliga modeller.

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

Pipelines i kod

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) i Python

Sentimentanalys på en batch av texter

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) i Python

Utvärdera sentimentanalysmodeller

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) i Python

Nu kör vi en övning!

Natural Language Processing (NLP) i Python

Preparing Video For Download...