File size: 8,988 Bytes
6d31173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df6116e
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.multioutput import MultiOutputClassifier
from sklearn.metrics import classification_report, f1_score, accuracy_score, hamming_loss
import gradio as gr

# Load dataset
splits = {'train': 'simplified/train-00000-of-00001.parquet'}
df = pd.read_parquet("hf://datasets/google-research-datasets/go_emotions/" + splits["train"])

emotion_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"
]

index_to_emotion = {i: label for i, label in enumerate(emotion_labels)}

mlb = MultiLabelBinarizer(classes=range(28))
y = mlb.fit_transform(df['labels'])

vectorizer = TfidfVectorizer(max_features=5000)
X = vectorizer.fit_transform(df['text'])

# Placeholder for trained model
model = None
metrics_report = ""

def train_model(test_size=0.2, max_iter=1000, random_state=42):
    global model, metrics_report

    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=test_size, random_state=random_state
    )

    model = MultiOutputClassifier(LogisticRegression(max_iter=max_iter))
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)

    # Calculate standard classification report + other metrics
    report = classification_report(
        y_test, y_pred, target_names=[str(i) for i in range(28)]
    )
    micro_f1 = f1_score(y_test, y_pred, average="micro")
    macro_f1 = f1_score(y_test, y_pred, average="macro")
    acc = accuracy_score(y_test, y_pred)
    hamming = hamming_loss(y_test, y_pred)

    metrics_summary = f"""
    Micro F1-score: {micro_f1:.4f}
    Macro F1-score: {macro_f1:.4f}
    Accuracy (Exact Match): {acc:.4f}
    Hamming Loss: {hamming:.4f}
    """

    # Save the full report
    metrics_report = metrics_summary.strip() + "\n\n" + report

    return "Training Complete!"

def predict_emotions(text):
    if model is None:
        return "Please train the model first.", ""

    vectorized = vectorizer.transform([text])
    probas = model.predict_proba(vectorized)

    result = {}
    for i, emotion in enumerate(mlb.classes_):
        prob_class_1 = probas[i][0][1]
        result[emotion] = round(prob_class_1 * 100, 2)

    sorted_result = sorted(result.items(), key=lambda x: x[1], reverse=True)
    return sorted_result

def predict_and_display(sentence):
    predictions = predict_emotions(sentence)
    if isinstance(predictions, str):
        return predictions, ""

    max_len = max(len(index_to_emotion[emo_id]) for emo_id, _ in predictions)
    result = "```" + "\nEmotion Predictions:\n\n"
    for emo_id, score in predictions:
        emo_name = index_to_emotion[emo_id]
        result += f"{emo_name.ljust(max_len)}{score}%\n"
    result += "```"
    top_emotion = index_to_emotion[predictions[0][0]]
    return result, top_emotion

# Gradio App
with gr.Blocks(title="Interactive Emotion Detector", theme=gr.themes.Soft()) as demo:
    with gr.Tabs():
        with gr.Tab("Emotion Detection"):
            gr.Markdown("## Emotion Detection")
            with gr.Row():
                with gr.Column():
                    input_text = gr.Textbox(
                        lines=3, placeholder="Enter a sentence...", label="Input Sentence"
                    )
                    submit_btn = gr.Button("Analyze Emotion")
                with gr.Column():
                    output_text = gr.Markdown(label="Prediction Results")
                    top_emotion = gr.Label(label="Top Emotion")
            submit_btn.click(
                fn=predict_and_display,
                inputs=input_text,
                outputs=[output_text, top_emotion]
            )

        with gr.Tab("Dataset"):
            gr.Markdown("## Dataset Information")

            def dataset_info():
                df = pd.read_parquet("hf://datasets/google-research-datasets/go_emotions/simplified/train-00000-of-00001.parquet")

                total_samples = len(df)
                emotions = sorted(set(e for label in df['labels'] for e in label))
                emotion_names = [emotion_labels[i] for i in emotions]

                # Count distribution
                all_labels = [emotion_labels[i] for sublist in df['labels'] for i in sublist]
                label_counts = pd.Series(all_labels).value_counts().sort_index()
                label_df = pd.DataFrame({
                    "Emotion": label_counts.index,
                    "Count": label_counts.values
                })

                stats = f"""
                    **Total Samples**: {total_samples}
                    **Emotion Classes**: {', '.join(emotion_names)}
                    """

                return stats, label_df

            stats_display = gr.Markdown()
            dist_table = gr.Dataframe(headers=["Emotion", "Count"], interactive=False)

            load_btn = gr.Button("Load Dataset Info")
            load_btn.click(fn=dataset_info, inputs=[], outputs=[stats_display, dist_table])


        with gr.Tab("EDA"):
            gr.Markdown("## Exploratory Data Analysis")

            eda_btn = gr.Button("Run EDA")

            eda_output = gr.Plot(label="EDA Output")

            def run_eda():
                import matplotlib.pyplot as plt
                from collections import Counter
                import re

                # Define the label map inside the function
                label_map = [
                    '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'
                ]

                fig, axs = plt.subplots(2, 2, figsize=(18, 10))

                # Label distribution
                label_counts = df['labels'].explode().value_counts().sort_index()
                axs[0, 0].bar(label_map, label_counts)
                axs[0, 0].set_title("Label Frequency Distribution")
                axs[0, 0].tick_params(axis='x', rotation=45)

                # Labels per example
                df['num_labels'] = df['labels'].apply(len)
                df['num_labels'].value_counts().sort_index().plot(kind='bar', ax=axs[0, 1])
                axs[0, 1].set_title("Number of Labels per Example")

                # Text length distribution
                df['text_length'] = df['text'].apply(len)
                df['text_length'].hist(bins=50, ax=axs[1, 0])
                axs[1, 0].set_title("Distribution of Text Lengths")
                axs[1, 0].set_xlabel("Text Length (characters)")
                axs[1, 0].set_ylabel("Frequency")

                # Most common words
                all_words = " ".join(df['text']).lower()
                tokens = re.findall(r'\b\w+\b', all_words)
                common_words = Counter(tokens).most_common(20)
                words, freqs = zip(*common_words)
                axs[1, 1].bar(words, freqs)
                axs[1, 1].set_title("Top 20 Most Common Words")
                axs[1, 1].tick_params(axis='x', rotation=45)

                plt.tight_layout()
                return fig

            eda_btn.click(fn=run_eda, inputs=[], outputs=eda_output)



        with gr.Tab("Train Model"):
            gr.Markdown("## Train Your Emotion Model")
            test_size = gr.Slider(0.1, 0.5, step=0.05, value=0.2, label="Test Size")
            max_iter = gr.Slider(100, 5000, step=100, value=1000, label="Max Iterations")
            random_state = gr.Number(value=42, label="Random State")
            train_button = gr.Button("Train Model")
            train_status = gr.Textbox(label="Training Status")
            train_button.click(
                fn=train_model,
                inputs=[test_size, max_iter, random_state],
                outputs=train_status
            )

        with gr.Tab("Results"):
            gr.Markdown("## Evaluation Metrics")
            results_output = gr.Markdown(label="Classification Report")
    
            def get_report():
                return "```\n" + metrics_report + "\n```"
            
            refresh_btn = gr.Button("Refresh Report")
            refresh_btn.click(
                fn=get_report,
                inputs=[],
                outputs=results_output
            )

demo.launch()