File size: 994 Bytes
e708ddc
 
 
 
 
21eb92e
e708ddc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# modules/sentiment_analysis.py

from transformers import pipeline

# Load sentiment pipeline (you can change model to "distilbert-base-uncased-finetuned-sst-2-english")
sentiment_pipeline = pipeline("sentiment-analysis", "distilbert-base-uncased-finetuned-sst-2-english")

def analyze_sentiment(texts: list) -> list:
    """
    Perform sentiment analysis on a list of texts.
    Returns a list of dictionaries with label and score.
    """
    results = []
    for t in texts:
        if t.strip():
            try:
                result = sentiment_pipeline(t)[0]
                results.append({
                    "text": t,
                    "label": result["label"],
                    "score": float(result["score"])
                })
            except Exception as e:
                results.append({
                    "text": t,
                    "label": "ERROR",
                    "score": 0.0,
                    "error": str(e)
                })
    return results