File size: 7,532 Bytes
49e3fdb
 
fa5e4f0
49e3fdb
fa5e4f0
 
49e3fdb
 
a113dbe
fa5e4f0
49e3fdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a113dbe
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from transformers import AutoTokenizer, AutoModel
import kagglehub
import numpy as np
import os
import pandas as pd
import streamlit as st
import torch
import torch.nn as nn
import matplotlib.pyplot as plt

MODEL_HANDLE = "prathabmurugan/dlgenai-emotion-classification/pyTorch/1a"
EMOTION_LABELS = ['anger', 'fear', 'joy', 'sadness', 'surprise']
THRESHOLDS = np.array([0.85, 0.43, 0.21, 0.7, 0.36])
MAX_LEN = 100
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")


class RobertaClassifier(nn.Module):
    def __init__(self, model_name: str, num_labels: int, dropout: float = 0.3):
        super().__init__()
        self.roberta = AutoModel.from_pretrained(model_name)
        hidden_size = self.roberta.config.hidden_size
        self.dropout = nn.Dropout(dropout)
        self.classifier = nn.Linear(hidden_size, num_labels)

    def forward(self, input_ids, attention_mask):
        outputs = self.roberta(
            input_ids=input_ids, attention_mask=attention_mask
        )
        pooled = outputs.pooler_output
        pooled = self.dropout(pooled)
        logits = self.classifier(pooled)
        return logits


def standardize_space(text):
    """Normalize whitespace in text."""
    return " ".join(str(text).split())


@st.cache_resource
def load_resources():
    status_container = st.empty()

    # 1. Download Model Weights
    status_container.info(
        f"Downloading model weights from KaggleHub [{MODEL_HANDLE}]")
    try:
        model_dir = kagglehub.model_download(MODEL_HANDLE)
        model_path = os.path.join(model_dir, "roberta_best_model.pth")
    except Exception as e:
        status_container.error(f"Failed to download model [{e}]")
        st.stop()

    # 2. Initialize Architecture
    status_container.info("Initializing RoBERTa architecture")
    tokenizer = AutoTokenizer.from_pretrained("roberta-base")
    model = RobertaClassifier("roberta-base", num_labels=5)

    # 3. Load Weights
    try:
        model.load_state_dict(torch.load(model_path, map_location=DEVICE))
        model.to(DEVICE)
        model.eval()
    except Exception as e:
        status_container.error(f"Error loading state dict [{e}]")
        st.stop()

    status_container.empty()  # Clear the status messages
    return model, tokenizer


def predict(texts, model, tokenizer):
    # Preprocessing
    processed_texts = [standardize_space(t) for t in texts]

    # Tokenization
    encodings = tokenizer(
        processed_texts,
        truncation=True,
        max_length=MAX_LEN,
        padding='max_length',
        return_tensors='pt'
    )

    input_ids = encodings['input_ids'].to(DEVICE)
    attention_mask = encodings['attention_mask'].to(DEVICE)

    # Inference
    with torch.no_grad():
        logits = model(input_ids, attention_mask)
        probs = torch.sigmoid(logits).cpu().numpy()

    # Apply specific thresholds
    preds = (probs > THRESHOLDS).astype(int)

    return preds, probs


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

st.title("Emotion Classification")
st.markdown(
    "This app pulls a custom fine-tuned **RoBERTa** model from Kaggle to classify text into 5 emotions.")

# Load model
model, tokenizer = load_resources()

# Tabs for different input modes
tab1, tab2 = st.tabs(["Single Text Inference", "Batch CSV Inference"])

with tab1:
    st.header("Test a single sentence")
    user_input = st.text_area(
        "Enter text here:", "Hello World!")

    if st.button("Analyze Text", type="primary"):
        if user_input.strip():
            with st.spinner("Analyzing..."):
                preds, probs = predict([user_input], model, tokenizer)

            st.subheader("Results:")

            # Display nicely
            col1, col2 = st.columns(2)

            with col1:
                st.write("**Detected Emotions:**")
                detected = []
                for idx, is_present in enumerate(preds[0]):
                    if is_present:
                        detected.append(EMOTION_LABELS[idx].capitalize())

                if detected:
                    for d in detected:
                        st.markdown(f"### ✅ {d}")
                else:
                    st.markdown(
                        "*No specific emotion detected above thresholds.*")

            with col2:
                st.write("**Confidence Scores:**")
                scores_df = pd.DataFrame({
                    "Emotion": EMOTION_LABELS,
                    "Score": probs[0],
                    "Threshold": THRESHOLDS,
                    "Detected": preds[0].astype(bool)
                })
                # Formatting the dataframe for visual appeal
                st.dataframe(
                    scores_df.style.format(
                        {"Score": "{:.2%}", "Threshold": "{:.2f}"})
                    .background_gradient(subset=["Score"], cmap="Greens"),
                    hide_index=True,
                    use_container_width=True
                )
        else:
            st.warning("Please enter some text.")

with tab2:
    st.header("Batch Process (CSV)")
    st.markdown("Upload a CSV file with a `text` and `id` column.")

    uploaded_file = st.file_uploader("Upload CSV", type=["csv"])

    if uploaded_file is not None:
        try:
            input_df = pd.read_csv(uploaded_file)
            if 'text' not in input_df.columns:
                st.error("CSV must have a 'text' column.")
            else:
                st.info(
                    f"Loaded [{len(input_df)}] rows. Click below to start.")

                if st.button("Generate Predictions"):
                    progress_bar = st.progress(0)
                    status_text = st.empty()

                    # Process in batches
                    batch_size = 16
                    all_preds = []
                    texts = input_df['text'].tolist()

                    for i in range(0, len(texts), batch_size):
                        batch_texts = texts[i:i + batch_size]
                        batch_preds, _ = predict(batch_texts, model, tokenizer)
                        all_preds.append(batch_preds)

                        # Update progress
                        progress = min((i + batch_size) / len(texts), 1.0)
                        progress_bar.progress(progress)
                        status_text.text(
                            f"Processed {i + len(batch_texts)}/{len(texts)} rows")

                    # Aggregate results
                    predictions_np = np.vstack(all_preds)
                    submission_df = pd.DataFrame(
                        predictions_np, columns=EMOTION_LABELS, dtype=int)

                    # Combine with original IDs
                    if 'id' in input_df.columns:
                        final_df = pd.concat(
                            [input_df[['id']], submission_df], axis=1)
                    else:
                        final_df = submission_df

                    st.success("Processing complete!")
                    st.dataframe(final_df.head(), use_container_width=True)

                    # Download button
                    csv = final_df.to_csv(index=False).encode('utf-8')
                    st.download_button(
                        label="Download Predictions CSV",
                        data=csv,
                        file_name="submission.csv",
                        mime="text/csv"
                    )
        except Exception as e:
            st.error(f"Error reading CSV: {e}")