File size: 2,285 Bytes
0b6b733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
os.system("git lfs pull")

import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import numpy as np

st.set_page_config(page_title="Emotion Classifier", layout="centered")

# Load GoEmotions label names
GOEMOTIONS_LABELS = [
    "admiration", "amusement", "anger", "annoyance", "approval", "caring",
    "confusion", "curiosity", "desire", "disappointment", "disapproval",
    "disgust", "embarrassment", "excitement", "fear", "gratitude", "grief",
    "joy", "love", "nervousness", "optimism", "pride", "realization", "relief",
    "remorse", "sadness", "surprise", "neutral"
]

# -----------------------------
# Load model and tokenizer
# -----------------------------
MODEL_PATH = "/mount/src/emotion-classifier-nlp/outputs/model"
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH, device_map="auto", low_cpu_mem_usage=True)
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
print("Model device:", next(model.parameters()).device)

# -----------------------------
# Streamlit UI
# -----------------------------
st.title("🧠 Emotion Classifier (NLP)")
st.markdown("Enter a sentence to analyze:")

input_text = st.text_area(" ", height=100)

if st.button("Classify") and input_text.strip():

    with torch.no_grad():
        inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
        outputs = model(**inputs)
        probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]

    pred_label_idx = torch.argmax(probs).cpu().item()
    pred_score = probs[pred_label_idx].cpu().item()
    pred_emotion = GOEMOTIONS_LABELS[pred_label_idx]

    # -----------------------------
    # Confidence threshold logic
    # -----------------------------
    threshold = 0.6
    if pred_score < threshold:
        st.warning(f"**Predicted Emotion:** Unclear / Not enough signal (Confidence: {pred_score:.0%})")
    else:
        st.success(f"**Predicted Emotion:** {pred_emotion} (Confidence: {pred_score:.0%})")

    # -----------------------------
    # Show all probabilities
    # -----------------------------
    st.markdown("### Prediction Probabilities:")
    for i, prob in enumerate(probs):
        st.write(f"- {GOEMOTIONS_LABELS[i]}: {prob.item():.4f}")