EmotionSense / predict.py
faizan20's picture
Upload 8 files
c46e765 verified
from app.model_loader import model_registry
class TextPredictor:
def __init__(self):
model_registry.initialize()
self.sentiment_model = model_registry.get("sentiment")
self.emotion_model = model_registry.get("emotion")
self.emotion_emojis = {
"joy": "😊",
"anger": "😠",
"sadness": "😞",
"fear": "😨",
"love": "❀️",
"surprise": "😲",
"disgust": "🀒",
"neutral": "😐"
}
def predict(self, text: str):
if not text or not isinstance(text, str):
raise ValueError("Input text must be a non-empty string.")
cleaned_text = text.strip()
sentiment_result = self.sentiment_model(cleaned_text)[0]
sentiment_label = sentiment_result["label"].capitalize()
emotion_result = self.emotion_model(cleaned_text)[0]
emotion_label = emotion_result["label"].lower()
emotion_with_emoji = f"{emotion_label} {self.emotion_emojis.get(emotion_label, '')}"
result = {
"input_text": text,
"cleaned_text": cleaned_text,
"sentiment": sentiment_label,
"emotion": emotion_with_emoji
}
return result
text_predictor = TextPredictor()