realmanise commited on
Commit
a0bf729
·
verified ·
1 Parent(s): e71c442

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -20
app.py CHANGED
@@ -4,10 +4,10 @@ import joblib
4
  import gradio as gr
5
  import spacy
6
  import nltk
 
7
  from nltk.stem import WordNetLemmatizer
8
  from spacy.lang.en.stop_words import STOP_WORDS
9
 
10
-
11
  # 1. Setup NLTK and SpaCy dependencies
12
  try:
13
  nltk.data.find('tokenizers/punkt')
@@ -19,19 +19,20 @@ except LookupError:
19
  nltk.download('wordnet')
20
 
21
  try:
22
- nlp = spacy.load("en_core_web_sm")
23
  except OSError:
24
- from spacy.cli import download
25
-
26
- download("en_core_web_sm")
27
- nlp = spacy.load("en_core_web_sm")
28
 
29
  lemmatizer = WordNetLemmatizer()
30
 
31
-
32
  logistic_regression_tfidf_model = joblib.load('logistic_regression_bow.pkl')
33
  tfidf_vectorizer = joblib.load('vectorizer_tfidf.pkl')
34
 
 
 
 
35
  # 3. Label mapping
36
  label_mapping = {
37
  0: 'sadness',
@@ -42,7 +43,7 @@ label_mapping = {
42
  5: 'surprise'
43
  }
44
 
45
- # 4. Include the text cleaning functions from your notebook
46
  def basic_cleaning(text):
47
  text = re.sub(r'http\S+', '', text)
48
  text = re.sub(r'[^a-zA-Z\s]', '', text)
@@ -59,24 +60,74 @@ def fully_cleaned(text):
59
  tokens = [lemmatizer.lemmatize(token) for token in tokens]
60
  return ' '.join(tokens)
61
 
62
- # 5. Prediction function
63
  @spaces.GPU
64
- def predict_emotion(text):
 
 
 
65
  cleaned_text = fully_cleaned(text)
66
  text_tfidf = tfidf_vectorizer.transform([cleaned_text])
 
 
67
  prediction = logistic_regression_tfidf_model.predict(text_tfidf)[0]
68
- return label_mapping.get(prediction, "Unknown")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- # 6. Gradio Interface
71
- iface = gr.Interface(
72
- fn=predict_emotion,
73
- inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
74
- outputs="text",
75
- title="Emotion Classifier",
76
- description="Enter a sentence to classify its emotion (sadness, joy, love, anger, fear, surprise)."
77
- )
 
 
 
 
 
 
 
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  # Launch the interface
81
  if __name__ == "__main__":
82
- iface.launch()
 
4
  import gradio as gr
5
  import spacy
6
  import nltk
7
+ import numpy as np
8
  from nltk.stem import WordNetLemmatizer
9
  from spacy.lang.en.stop_words import STOP_WORDS
10
 
 
11
  # 1. Setup NLTK and SpaCy dependencies
12
  try:
13
  nltk.data.find('tokenizers/punkt')
 
19
  nltk.download('wordnet')
20
 
21
  try:
22
+ nlp = spacy.load("en_core_web_sm")
23
  except OSError:
24
+ from spacy.cli import download
25
+ download("en_core_web_sm")
26
+ nlp = spacy.load("en_core_web_sm")
 
27
 
28
  lemmatizer = WordNetLemmatizer()
29
 
 
30
  logistic_regression_tfidf_model = joblib.load('logistic_regression_bow.pkl')
31
  tfidf_vectorizer = joblib.load('vectorizer_tfidf.pkl')
32
 
33
+ # Extract feature names at startup for word mapping
34
+ feature_names = np.array(tfidf_vectorizer.get_feature_names_out())
35
+
36
  # 3. Label mapping
37
  label_mapping = {
38
  0: 'sadness',
 
43
  5: 'surprise'
44
  }
45
 
46
+ # 4. Include the text cleaning functions from your notebook (Kept identical)
47
  def basic_cleaning(text):
48
  text = re.sub(r'http\S+', '', text)
49
  text = re.sub(r'[^a-zA-Z\s]', '', text)
 
60
  tokens = [lemmatizer.lemmatize(token) for token in tokens]
61
  return ' '.join(tokens)
62
 
63
+ # 5. Prediction & Interpretation function
64
  @spaces.GPU
65
+ def predict_emotion_with_interpretation(text):
66
+ if not text.strip():
67
+ return "Unknown", []
68
+
69
  cleaned_text = fully_cleaned(text)
70
  text_tfidf = tfidf_vectorizer.transform([cleaned_text])
71
+
72
+ # Get the predicted class index
73
  prediction = logistic_regression_tfidf_model.predict(text_tfidf)[0]
74
+ predicted_emotion = label_mapping.get(prediction, "Unknown")
75
+
76
+ # --- Interpretation Logic ---
77
+ word_weights = {}
78
+ if hasattr(logistic_regression_tfidf_model, "coef_"):
79
+ # Get the coefficients assigned to features for the predicted class
80
+ coefs = logistic_regression_tfidf_model.coef_[prediction]
81
+
82
+ # Identify features present in the user's input string
83
+ _, present_word_indices = text_tfidf.nonzero()
84
+
85
+ for idx in present_word_indices:
86
+ word = feature_names[idx]
87
+ weight = coefs[idx]
88
+ if weight > 0: # Only look at words that positively correlate with this class
89
+ word_weights[word] = weight
90
 
91
+ # Break original text into words to build the highlight tuples for Gradio
92
+ words = text.split()
93
+ highlighted_output = []
94
+
95
+ for word in words:
96
+ # Match back using the exact basic cleaning and lemmatization logic
97
+ clean_w = lemmatizer.lemmatize(basic_cleaning(word).strip())
98
+
99
+ if clean_w in word_weights:
100
+ # Append word with the classification label to highlight it
101
+ highlighted_output.append((word + " ", predicted_emotion))
102
+ else:
103
+ highlighted_output.append((word + " ", None))
104
+
105
+ return predicted_emotion, highlighted_output
106
 
107
+ # 6. Gradio Interface (Updated layout structure to present the interpretation)
108
+ with gr.Blocks(title="Emotion Classifier") as demo:
109
+ gr.Markdown("# Emotion Classifier")
110
+ gr.Markdown("Enter a sentence to classify its emotion and instantly see which words contributed to the prediction.")
111
+
112
+ with gr.Row():
113
+ with gr.Column():
114
+ input_text = gr.Textbox(lines=5, placeholder="Enter text here...", label="Input Text")
115
+ submit_btn = gr.Button("Classify Emotion")
116
+
117
+ with gr.Column():
118
+ output_label = gr.Textbox(label="Predicted Emotion")
119
+ output_highlights = gr.HighlightedText(
120
+ label="Word Contribution Interpretation",
121
+ combine_adjacent=False,
122
+ show_legend=True
123
+ )
124
+
125
+ submit_btn.click(
126
+ fn=predict_emotion_with_interpretation,
127
+ inputs=input_text,
128
+ outputs=[output_label, output_highlights]
129
+ )
130
 
131
  # Launch the interface
132
  if __name__ == "__main__":
133
+ demo.launch()