Spaces:
Runtime error
Runtime error
Download modules/sentiment_analysis.py from Pro-Coder/Sentiment_Analysis: direct link, hf CLI and curl.
- Browser
- Download file 994 Bytes
-
https://huggingface.co/spaces/Pro-Coder/Sentiment_Analysis/resolve/8a53061e4d89cb38adafaabb32eff7c016ff1ad1/modules/sentiment_analysis.py
- Command line
-
hf download hf://spaces/Pro-Coder/Sentiment_Analysis@8a53061e4d89cb38adafaabb32eff7c016ff1ad1/modules/sentiment_analysis.py
-
curl -L -o sentiment_analysis.py https://huggingface.co/spaces/Pro-Coder/Sentiment_Analysis/resolve/8a53061e4d89cb38adafaabb32eff7c016ff1ad1/modules/sentiment_analysis.py
994 Bytes
| # 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 | |