File size: 2,644 Bytes
76df3c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from tensorflow import keras
import streamlit as st
import altair as alt
import plotly.express as px

import pandas as pd
import numpy as np
from datetime import datetime


import joblib

from google.cloud import storage
from tempfile import TemporaryFile
from csv import writer
from datetime import datetime
import os
from dotenv import load_dotenv
from nltk.stem import PorterStemmer
from nltk.corpus import stopwords
import re
from tensorflow import keras
import numpy as np
import pandas as pd

from tensorflow.keras.preprocessing.sequence import pad_sequences
import nltk
from tensorflow.keras.preprocessing.text import one_hot


import re
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer

import pickle
pkl_file = open('m_lb.pkl', 'rb')
le_departure = pickle.load(pkl_file)
pkl_file.close()
model = keras.models.load_model('m_odel.h5')
nltk.download('stopwords')
stopwords = set(nltk.corpus.stopwords.words('english'))
vocabSize = 11000
max_len = 1160
load_dotenv()


def predict_emotions(sentence):
    sentence = sentence_cleaning(sentence)
    result = le_departure.inverse_transform(
        np.argmax(model.predict(sentence), axis=-1))[0]
    proba = np.max(model.predict(sentence))
    print(result)
    print(proba)
    return result, proba


def sentence_cleaning(sentence):
    """Pre-processing sentence for prediction"""
    stemmer = PorterStemmer()
    corpus = []
    text = re.sub("[^a-zA-Z]", " ", sentence)
    text = text.lower()
    text = text.split()
    text = [stemmer.stem(word) for word in text if word not in stopwords]
    text = " ".join(text)
    corpus.append(text)
    one_hot_word = [one_hot(input_text=word, n=vocabSize) for word in corpus]
    pad = pad_sequences(sequences=one_hot_word, maxlen=max_len, padding='pre')
    return pad


def main():
    st.title("Emotion Classifier")
    menu = ["Home", "Monitor"]
    choice = st.sidebar.selectbox("Menu", menu)
    if choice == "Home":
        st.subheader("Home-Emotion In Text")

        with st.form(key='emotion_clf_form'):
            raw_text = st.text_area("Type Here")
            submit_text = st.form_submit_button(label='Submit')

        if submit_text:
            col1, col2 = st.beta_columns(2)

            # Apply Fxn Here
            res, proba = predict_emotions(raw_text)

            with col1:
                st.success("Original Text")
                st.write(raw_text)

                st.success("Prediction")
                st.write("{}:{}".format(res, proba))
                st.write("Confidence:{}".format(proba))
 
  

    else:
        st.subheader("About")


if __name__ == '__main__':
    main()