-
Notifications
You must be signed in to change notification settings - Fork 0
/
Emotion_prediction.py
57 lines (43 loc) · 1.73 KB
/
Emotion_prediction.py
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
import streamlit as st
import pandas as pd
import numpy as np
import pickle
import re
from nltk.corpus import stopwords
import nltk
from nltk.tokenize import word_tokenize
from nltk.stem import SnowballStemmer
from keras.utils import pad_sequences
from tensorflow.keras.models import load_model
st.write("""
# *Emotion Text Classification*
**This app predicts whether the text expresses emotions of Joy, Sadness, Anger, or Fear.**
""")
st.subheader("Enter your Text to know Emotion of it")
input_text = st.text_area("Enter your Text here")
if st.button("Submit"):
nltk.download('stopwords')
nltk.download('punkt')
# Remove stopwords
stop_words = stopwords.words('english')
snowballstemmer = SnowballStemmer('english')
def preprocess_text(text):
text = re.sub('[^a-zA-Z]', ' ', text)
text = text.lower()
text = text.split()
text = [snowballstemmer.stem(word) for word in text if word not in stop_words]
return ' '.join(text)
model = load_model('emotion_classification_rnn.h5')
with open('tokenizer.pkl', 'rb') as file:
tokenizer = pickle.load(file)
with open('label_encoder.pkl', 'rb') as file:
encoder = pickle.load(file)
input_text = preprocess_text(input_text)
test_sequences = tokenizer.texts_to_sequences([input_text])
maxlen = 35 # Ensure this matches the maxlen used during training
X_test = pad_sequences(test_sequences, maxlen=maxlen)
# Make predictions
y_pred = model.predict(X_test)
y_pred_labels = np.argmax(y_pred, axis=1)
predicted_emotion = encoder.inverse_transform(y_pred_labels)
st.write(f"Your text has a Emotion of: {predicted_emotion[0]}")