Sentiment analysis के लिए Hugging Face pipelines

Python में Natural Language Processing (NLP)

Fouad Trad

Machine Learning Engineer

रीकैप: NLP वर्कफ़्लो

पूरा वर्कफ़्लो डायग्राम जो बताता है कि Chapter 1 में preprocessing और Chapter 2 में feature extraction कवर हुआ.

Python में Natural Language Processing (NLP)

Hugging Face pipelines

पूरा वर्कफ़्लो डायग्राम जो बताता है कि Chapters 3 और 4 में hugging face pipelines कवर होंगी जो सभी स्टेप्स को रैप करती हैं: preprocessing, feature extraction, और modeling.

  • रेडी-मेड वर्कफ़्लो जो एक फंक्शन कॉल में सभी स्टेप्स संभालता है
  • एक pipeline परिभाषित करने के लिए चाहिए:
    • NLP टास्क
    • टास्क के लिए मॉडल
Python में Natural Language Processing (NLP)

Sentiment analysis के लिए pipelines

 

 

  • Text classification टास्क
  • प्रेडिक्ट करता है कि टेक्स्ट का भाव positive है या negative

एक इमेज जिसमें positive sentiment के लिए खुश चेहरा और thumbs up, तथा negative sentiment के लिए उदास चेहरा और thumbs down दिखता है.

Python में Natural Language Processing (NLP)

Text classification के लिए मॉडल

एक gif जो दिखाता है कि वेबसाइट पर स्क्रॉल करके सही टास्क चुनें ताकि उपयुक्त मॉडल मिलें.

1 https://huggingface.co/models
Python में Natural Language Processing (NLP)

कोड में pipelines

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}]
Python में Natural Language Processing (NLP)

टेक्स्ट के बैच पर 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!"]

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}]
Python में Natural Language Processing (NLP)

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
Python में Natural Language Processing (NLP)

अभ्यास करते हैं!

Python में Natural Language Processing (NLP)

Preparing Video For Download...