File size: 4,385 Bytes
769b4fb
6cadcd6
 
 
268ecd7
 
a0bf729
268ecd7
6cadcd6
460b7a1
6931b9a
6cadcd6
 
 
e71c442
6cadcd6
 
 
e71c442
6931b9a
6cadcd6
a0bf729
6cadcd6
a0bf729
 
 
268ecd7
6cadcd6
 
6931b9a
6cadcd6
 
6931b9a
a0bf729
 
6cadcd6
268ecd7
 
 
 
 
 
 
 
 
6931b9a
6cadcd6
 
6931b9a
6cadcd6
 
 
 
268ecd7
6cadcd6
 
 
 
 
 
 
268ecd7
6931b9a
460b7a1
6931b9a
a0bf729
6931b9a
 
268ecd7
 
6931b9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0bf729
6931b9a
 
 
 
 
 
 
 
 
a0bf729
 
6931b9a
 
 
a0bf729
6931b9a
a0bf729
6931b9a
a0bf729
 
 
6931b9a
a0bf729
6931b9a
a0bf729
6931b9a
a0bf729
460b7a1
6931b9a
6cadcd6
6931b9a
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
import spaces
import re
import joblib
import gradio as gr
import spacy
import nltk
import numpy as np
from nltk.stem import WordNetLemmatizer
from spacy.lang.en.stop_words import STOP_WORDS

# 1. Setup NLTK and SpaCy dependencies
try:
    nltk.data.find('tokenizers/punkt')
    nltk.data.find('tokenizers/punkt_tab')
    nltk.download('wordnet')
except LookupError:
    nltk.download('punkt')
    nltk.download('punkt_tab')
    nltk.download('wordnet')
    
try:
    nlp = spacy.load("en_core_web_sm")
except OSError:
    from spacy.cli import download
    download("en_core_web_sm")
    nlp = spacy.load("en_core_web_sm")

lemmatizer = WordNetLemmatizer()

logistic_regression_tfidf_model = joblib.load('logistic_regression_bow.pkl')
tfidf_vectorizer = joblib.load('vectorizer_tfidf.pkl')

# Extract feature names at startup for word mapping
feature_names = np.array(tfidf_vectorizer.get_feature_names_out())

# 3. Label mapping
label_mapping = {
    0: 'sadness',
    1: 'joy',
    2: 'love',
    3: 'anger',
    4: 'fear',
    5: 'surprise'
}

# 4. Include the text cleaning functions from your notebook (Kept identical)
def basic_cleaning(text):
    text = re.sub(r'http\S+', '', text)
    text = re.sub(r'[^a-zA-Z\s]', '', text)
    text = re.sub(r'\w*\d\w*', '', text)
    text = re.sub(r'<.*?>', '', text)
    text = text.lower()
    return text

def fully_cleaned(text):
    cleaned = basic_cleaning(text)
    doc = nlp(cleaned)
    tokens = [token.text for token in doc]
    tokens = [token for token in tokens if token not in STOP_WORDS]
    tokens = [lemmatizer.lemmatize(token) for token in tokens]
    return ' '.join(tokens)

# 5. Prediction & Interpretation function
@spaces.GPU
def predict_emotion_with_interpretation(text):
    if not text.strip():
        return "Unknown", []
        
    cleaned_text = fully_cleaned(text)
    text_tfidf = tfidf_vectorizer.transform([cleaned_text])
    
    # Get the predicted class index
    prediction = logistic_regression_tfidf_model.predict(text_tfidf)[0]
    predicted_emotion = label_mapping.get(prediction, "Unknown")
    
    # --- Interpretation Logic ---
    word_weights = {}
    if hasattr(logistic_regression_tfidf_model, "coef_"):
        # Get the coefficients assigned to features for the predicted class
        coefs = logistic_regression_tfidf_model.coef_[prediction]
        
        # Identify features present in the user's input string
        _, present_word_indices = text_tfidf.nonzero()
        
        for idx in present_word_indices:
            word = feature_names[idx]
            weight = coefs[idx]
            if weight > 0:  # Only look at words that positively correlate with this class
                word_weights[word] = weight

    # Break original text into words to build the highlight tuples for Gradio
    words = text.split()
    highlighted_output = []
    
    for word in words:
        # Match back using the exact basic cleaning and lemmatization logic
        clean_w = lemmatizer.lemmatize(basic_cleaning(word).strip())
        
        if clean_w in word_weights:
            # Append word with the classification label to highlight it
            highlighted_output.append((word + " ", predicted_emotion))
        else:
            highlighted_output.append((word + " ", None))
            
    return predicted_emotion, highlighted_output

# 6. Gradio Interface (Updated layout structure to present the interpretation)
with gr.Blocks(title="Emotion Classifier") as demo:
    gr.Markdown("# Emotion Classifier")
    gr.Markdown("Enter a sentence to classify its emotion and instantly see which words contributed to the prediction.")
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(lines=5, placeholder="Enter text here...", label="Input Text")
            submit_btn = gr.Button("Classify Emotion")
            
        with gr.Column():
            output_label = gr.Textbox(label="Predicted Emotion")
            output_highlights = gr.HighlightedText(
                label="Word Contribution Interpretation",
                combine_adjacent=False,
                show_legend=True
            )
            
    submit_btn.click(
        fn=predict_emotion_with_interpretation,
        inputs=input_text,
        outputs=[output_label, output_highlights]
    )

# Launch the interface
if __name__ == "__main__":
    demo.launch()