Spaces:
Runtime error
Runtime error
| # 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 | |