Pipeline Hugging Face untuk analisis sentimen

Natural Language Processing (NLP) in Python

Fouad Trad

Machine Learning Engineer

Rekap: alur kerja NLP

Diagram alur lengkap yang menyebutkan Bab 1 membahas prapemrosesan dan Bab 2 membahas ekstraksi fitur.

Natural Language Processing (NLP) in Python

Pipeline Hugging Face

Gif yang menunjukkan cara menggulir situs dan memilih tugas yang tepat untuk mendapatkan model yang sesuai.

  • Alur siap pakai yang menangani semua langkah dalam satu pemanggilan fungsi
  • Mendefinisikan pipeline memerlukan:
    • Tugas NLP
    • Model untuk menjalankan tugas
Natural Language Processing (NLP) in Python

Pipeline untuk analisis sentimen

 

 

  • Tugas klasifikasi teks
  • Memprediksi apakah teks bernada positif atau negatif

Gambar menunjukkan wajah senang dengan jempol ke atas untuk sentimen positif, dan wajah sedih dengan jempol ke bawah untuk sentimen negatif.

Natural Language Processing (NLP) in Python

Model untuk klasifikasi teks

Gif yang menunjukkan cara menggulir situs dan memilih tugas yang tepat untuk mendapatkan model yang sesuai.

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

Pipeline dalam kode

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

Analisis sentimen pada sekumpulan teks

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

Menilai model analisis sentimen

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

Ayo berlatih!

Natural Language Processing (NLP) in Python

Preparing Video For Download...