Sumit404 commited on
Commit
c68497b
·
verified ·
1 Parent(s): d1cd0d2

Update sentiment_analysis.py

Browse files
Files changed (1) hide show
  1. sentiment_analysis.py +17 -14
sentiment_analysis.py CHANGED
@@ -1,14 +1,17 @@
1
- from transformers import pipeline
2
-
3
- # Load Pre-trained Sentiment Model
4
- sentiment_analyzer = pipeline("sentiment-analysis")
5
-
6
- def analyze_sentiment(text):
7
- result = sentiment_analyzer(text)
8
- return result[0]
9
-
10
- # Test
11
- if __name__ == "__main__":
12
- text = "I am really happy with the service!"
13
- sentiment = analyze_sentiment(text)
14
- print("Sentiment:", sentiment)
 
 
 
 
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}]