Spaces:
Sleeping
Sleeping
File size: 1,359 Bytes
c46e765 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
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()
|