Spaces:
Runtime error
Runtime error
Update sentiment_analysis.py
Browse files- sentiment_analysis.py +17 -14
sentiment_analysis.py
CHANGED
|
@@ -1,14 +1,17 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
class SentimentAnalyzer:
|
| 4 |
+
def __init__(self):
|
| 5 |
+
self.analyzer = pipeline(
|
| 6 |
+
"sentiment-analysis",
|
| 7 |
+
model="distilbert-base-uncased-finetuned-sst-2-english",
|
| 8 |
+
return_all_scores=True
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
def analyze(self, text):
|
| 12 |
+
try:
|
| 13 |
+
results = self.analyzer(text)
|
| 14 |
+
return results[0]
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error in sentiment analysis: {str(e)}")
|
| 17 |
+
return [{"label": "ERROR", "score": 0.0}]
|