from flask import Flask from keras.models import load_model from tensorflow.keras.preprocessing.sequence import pad_sequences import re from tensorflow.keras.preprocessing.text import one_hot as oh import numpy as np import tensorflow as tf app = Flask(__name__) # Load the saved model new_model = load_model('m.h5') stopwords_lst = [ 'i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"] import pickle import random random.seed(42) with open ('oh.pkl','rb') as f: oh = pickle.load(f) with open ('ps.pkl','rb') as f: ps = pickle.load(f) def predict_emotion2(stri): review = re.sub('[^a-zA-Z]', ' ', stri) review = review.lower() review = review.split() # print(ps.stem(word)) review = [ps.stem(word) for word in review if not word in stopwords_lst] review = [int(tf.strings.to_hash_bucket_fast(word, 1000)) for word in review] onehot_repr = [review] print(onehot_repr) embed = pad_sequences(onehot_repr,padding='pre',maxlen=35) # predicti = new_model.predict(embed) # return np.argmax(predicti) strs = ["I am surprised of my work", "I am happy of my work", "I am sad of my work", "I love my country and I am happy"] for s in strs: predict_emotion2(s) # print("em: ", predict_emotion2("I am surprised of my work")) # print("em: ", predict_emotion2("I am happy of my work")) # print("em: ", predict_emotion2("I am sad of my work")) # print("em: ", predict_emotion2("I love my country and I am happy"))