Download src/components/emotionanalyz.py from Umesh143/Chatmate: direct link, hf CLI and curl.
- Browser
- Download file 1.23 kB
-
https://huggingface.co/spaces/Umesh143/Chatmate/resolve/main/src/components/emotionanalyz.py
- Command line
-
hf download hf://spaces/Umesh143/Chatmate/src/components/emotionanalyz.py
-
curl -L -o emotionanalyz.py https://huggingface.co/spaces/Umesh143/Chatmate/resolve/main/src/components/emotionanalyz.py
1.23 kB
| # Emotion analysis | |
| from src.exception.exception import customexception | |
| from src.logger.logger import logging | |
| from transformers import AutoTokenizer | |
| from transformers import AutoModelForSequenceClassification | |
| from scipy.special import softmax | |
| import sys | |
| # Pretrained model | |
| MODEL = f"cardiffnlp/twitter-roberta-base-sentiment" | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL) | |
| sentiment_model = AutoModelForSequenceClassification.from_pretrained(MODEL) | |
| class EmotionAnalyzer: | |
| def __init__(self): | |
| self.emotion_classifier = sentiment_model | |
| self.emotion = '' | |
| # Roberta model | |
| def analyze_emotion(self, text): | |
| try: | |
| encoded_text = tokenizer(text, return_tensors='pt') | |
| output = self.emotion_classifier(**encoded_text) | |
| scores = output[0][0].detach().numpy() | |
| scores = softmax(scores) | |
| scores_dict = { | |
| 'negative': scores[0], | |
| 'neutral': scores[1], | |
| 'positive': scores[2], | |
| } | |
| self.emotion = max(scores_dict) | |
| logging.info("Sentiment of response generated.") | |
| return self.emotion | |
| except Exception as e: | |
| raise customexception(e,sys) |