File size: 664 Bytes
726f5db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from transformers import pipeline

def load_model():
    return pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis")

def analyze_sentiments(df, text_column, classifier):
    if text_column not in df.columns:
        raise ValueError(f"Column '{text_column}' not found in DataFrame.")
    sentiments = []
    for text in df[text_column]:
        try:
            sentiment = classifier(str(text))[0]['label']
            sentiments.append(sentiment)
        except Exception as e:
            print(f"Error processing text: {text}. Error: {e}")
            sentiments.append('UNKNOWN')
    df['sentiment'] = sentiments
    return df