Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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',
|
| 18 |
+
2: 'love',
|
| 19 |
+
3: 'anger',
|
| 20 |
+
4: 'fear',
|
| 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()
|