realmanise commited on
Commit
6cadcd6
·
verified ·
1 Parent(s): c9b8223

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -22
app.py CHANGED
@@ -1,17 +1,33 @@
1
- import gradio as gr
2
- import joblib
3
  import os
 
 
 
4
  import spacy
5
  import nltk
6
  from nltk.stem import WordNetLemmatizer
7
- nltk.download('wordnet')
8
- nlp = spacy.load('en_core_web_sm')
9
- lemmatizer = WordNetLemmatizer()
 
 
 
 
 
 
10
 
11
- logistic_regression_tfidf_model = joblib.load(os.path.join(OUTPUT_DIR, 'logistic_regression_bow.pkl')) # Model saved as bow.pkl
12
- tfidf_vectorizer = joblib.load(os.path.join(OUTPUT_DIR, 'vectorizer_tfidf.pkl'))
 
 
 
13
 
14
- # Define the label mapping
 
 
 
 
 
 
15
  label_mapping = {
16
  0: 'sadness',
17
  1: 'joy',
@@ -21,26 +37,39 @@ label_mapping = {
21
  5: 'surprise'
22
  }
23
 
24
- # Ensure nlp and lemmatizer are available for fully_cleaned function
25
- # (Assuming these were defined in previous cells and are still in scope)
26
- # If not, they would need to be re-initialized here:
 
 
 
 
 
27
 
 
 
 
 
 
 
 
28
 
 
29
  def predict_emotion(text):
30
  cleaned_text = fully_cleaned(text)
31
-
32
  text_tfidf = tfidf_vectorizer.transform([cleaned_text])
33
  prediction = logistic_regression_tfidf_model.predict(text_tfidf)[0]
34
- predicted_emotion = label_mapping.get(prediction, "Unknown")
35
-
36
- return predicted_emotion
37
 
38
- # Create the Gradio interface
39
- iface = gr.Interface(fn=predict_emotion,
40
- inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
41
- outputs="text",
42
- title="Emotion Classifier",
43
- description="Enter a sentence to classify its emotion (sadness, joy, love, anger, fear, surprise).")
 
 
44
 
45
  # Launch the interface
46
- iface.launch()
 
 
 
 
1
  import os
2
+ import re
3
+ 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
+ # 1. Setup NLTK and SpaCy dependencies
11
+ try:
12
+ nltk.data.find('tokenizers/punkt')
13
+ nltk.data.find('tokenizers/punkt_tab')
14
+ except LookupError:
15
+ nltk.download('punkt')
16
+ nltk.download('punkt_tab')
17
 
18
+ try:
19
+ nlp = spacy.load("en_core_web_sm")
20
+ except OSError:
21
+ from spacy.cli import download
22
+ download("en_core_web_sm")
23
 
24
+ lemmatizer = WordNetLemmatizer()
25
+
26
+
27
+ logistic_regression_tfidf_model = joblib.load('logistic_regression_bow.pkl')
28
+ tfidf_vectorizer = joblib.load('vectorizer_tfidf.pkl')
29
+
30
+ # 3. Label mapping
31
  label_mapping = {
32
  0: 'sadness',
33
  1: 'joy',
 
37
  5: 'surprise'
38
  }
39
 
40
+ # 4. Include the text cleaning functions from your notebook
41
+ def basic_cleaning(text):
42
+ text = re.sub(r'http\S+', '', text)
43
+ text = re.sub(r'[^a-zA-Z\s]', '', text)
44
+ text = re.sub(r'\w*\d\w*', '', text)
45
+ text = re.sub(r'<.*?>', '', text)
46
+ text = text.lower()
47
+ return text
48
 
49
+ def fully_cleaned(text):
50
+ cleaned = basic_cleaning(text)
51
+ doc = nlp(cleaned)
52
+ tokens = [token.text for token in doc]
53
+ tokens = [token for token in tokens if token not in STOP_WORDS]
54
+ tokens = [lemmatizer.lemmatize(token) for token in tokens]
55
+ return ' '.join(tokens)
56
 
57
+ # 5. Prediction function
58
  def predict_emotion(text):
59
  cleaned_text = fully_cleaned(text)
 
60
  text_tfidf = tfidf_vectorizer.transform([cleaned_text])
61
  prediction = logistic_regression_tfidf_model.predict(text_tfidf)[0]
62
+ return label_mapping.get(prediction, "Unknown")
 
 
63
 
64
+ # 6. Gradio Interface
65
+ iface = gr.Interface(
66
+ fn=predict_emotion,
67
+ inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
68
+ outputs="text",
69
+ title="Emotion Classifier",
70
+ description="Enter a sentence to classify its emotion (sadness, joy, love, anger, fear, surprise)."
71
+ )
72
 
73
  # Launch the interface
74
+ if __name__ == "__main__":
75
+ iface.launch()