Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tensorflow import keras
|
2 |
+
import streamlit as st
|
3 |
+
import altair as alt
|
4 |
+
import plotly.express as px
|
5 |
+
|
6 |
+
import pandas as pd
|
7 |
+
import numpy as np
|
8 |
+
from datetime import datetime
|
9 |
+
|
10 |
+
|
11 |
+
import joblib
|
12 |
+
|
13 |
+
from google.cloud import storage
|
14 |
+
from tempfile import TemporaryFile
|
15 |
+
from csv import writer
|
16 |
+
from datetime import datetime
|
17 |
+
import os
|
18 |
+
from dotenv import load_dotenv
|
19 |
+
from nltk.stem import PorterStemmer
|
20 |
+
from nltk.corpus import stopwords
|
21 |
+
import re
|
22 |
+
from tensorflow import keras
|
23 |
+
import numpy as np
|
24 |
+
import pandas as pd
|
25 |
+
|
26 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
27 |
+
import nltk
|
28 |
+
from tensorflow.keras.preprocessing.text import one_hot
|
29 |
+
|
30 |
+
|
31 |
+
import re
|
32 |
+
from nltk.corpus import stopwords
|
33 |
+
from nltk.stem import PorterStemmer
|
34 |
+
|
35 |
+
import pickle
|
36 |
+
pkl_file = open('m_lb.pkl', 'rb')
|
37 |
+
le_departure = pickle.load(pkl_file)
|
38 |
+
pkl_file.close()
|
39 |
+
model = keras.models.load_model('m_odel.h5')
|
40 |
+
nltk.download('stopwords')
|
41 |
+
stopwords = set(nltk.corpus.stopwords.words('english'))
|
42 |
+
vocabSize = 11000
|
43 |
+
max_len = 1160
|
44 |
+
load_dotenv()
|
45 |
+
|
46 |
+
|
47 |
+
def predict_emotions(sentence):
|
48 |
+
sentence = sentence_cleaning(sentence)
|
49 |
+
result = le_departure.inverse_transform(
|
50 |
+
np.argmax(model.predict(sentence), axis=-1))[0]
|
51 |
+
proba = np.max(model.predict(sentence))
|
52 |
+
print(result)
|
53 |
+
print(proba)
|
54 |
+
return result, proba
|
55 |
+
|
56 |
+
|
57 |
+
def sentence_cleaning(sentence):
|
58 |
+
"""Pre-processing sentence for prediction"""
|
59 |
+
stemmer = PorterStemmer()
|
60 |
+
corpus = []
|
61 |
+
text = re.sub("[^a-zA-Z]", " ", sentence)
|
62 |
+
text = text.lower()
|
63 |
+
text = text.split()
|
64 |
+
text = [stemmer.stem(word) for word in text if word not in stopwords]
|
65 |
+
text = " ".join(text)
|
66 |
+
corpus.append(text)
|
67 |
+
one_hot_word = [one_hot(input_text=word, n=vocabSize) for word in corpus]
|
68 |
+
pad = pad_sequences(sequences=one_hot_word, maxlen=max_len, padding='pre')
|
69 |
+
return pad
|
70 |
+
|
71 |
+
|
72 |
+
def main():
|
73 |
+
st.title("Emotion Classifier")
|
74 |
+
menu = ["Home", "Monitor"]
|
75 |
+
choice = st.sidebar.selectbox("Menu", menu)
|
76 |
+
if choice == "Home":
|
77 |
+
st.subheader("Home-Emotion In Text")
|
78 |
+
|
79 |
+
with st.form(key='emotion_clf_form'):
|
80 |
+
raw_text = st.text_area("Type Here")
|
81 |
+
submit_text = st.form_submit_button(label='Submit')
|
82 |
+
|
83 |
+
if submit_text:
|
84 |
+
col1, col2 = st.beta_columns(2)
|
85 |
+
|
86 |
+
# Apply Fxn Here
|
87 |
+
res, proba = predict_emotions(raw_text)
|
88 |
+
|
89 |
+
with col1:
|
90 |
+
st.success("Original Text")
|
91 |
+
st.write(raw_text)
|
92 |
+
|
93 |
+
st.success("Prediction")
|
94 |
+
st.write("{}:{}".format(res, proba))
|
95 |
+
st.write("Confidence:{}".format(proba))
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
else:
|
100 |
+
st.subheader("About")
|
101 |
+
|
102 |
+
|
103 |
+
if __name__ == '__main__':
|
104 |
+
main()
|