raj-jaiswal-98/Sentiment-Analysis-LSTM
0
1import numpy as np # linear algebra2import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)3import torch4import torch.nn as nn5import torch.nn.functional as F6# from nltk.corpus import stopwords 7import nltk8# from collections import Counter9import string10import re11# import seaborn as sns12# from tqdm import tqdm13# import matplotlib.pyplot as plt14# from torch.utils.data import TensorDataset, DataLoader15# from sklearn.model_selection import train_test_split16import pickle17import streamlit as st18import time19 20 21is_cuda = torch.cuda.is_available()22 23# If we have a GPU available, we'll set our device to GPU. We'll use this device variable later in our code.24if is_cuda:25 device = torch.device("cuda")26 print("GPU is available")27else:28 device = torch.device("cpu")29 print("GPU not available, CPU used")30 31# device = torch.device("cpu")32output_dim = 133#model class34 35class SentimentLSTM(nn.Module):36 def __init__(self, no_layers, vocab_size, hidden_dim, embedding_dim, drop_prob = 0.5):37 super(SentimentLSTM, self).__init__()38 39 self.no_layers = no_layers40 self.output_dim = output_dim41 self.hidden_dim = hidden_dim42 self.vocab_size = vocab_size43 44 #embedding layer45 self.embedding = nn.Embedding(vocab_size, embedding_dim) 46 47 #LSTM48 self.lstm = nn.LSTM(input_size = embedding_dim, hidden_size = self.hidden_dim, num_layers = no_layers, batch_first=True)49 50 #dropout layers51 self.dropout = nn.Dropout(0.3)52 53 #linear and Sigmoid layer54 55 self.fc = nn.Linear(self.hidden_dim, self.output_dim)56 self.sig = nn.Sigmoid()57 58 def forward(self, x, hidden):59 # we just passed a batch60 batch_size = x.size(0) # batch size -> B61 #embed shape -> [B, max_len, embed_dim]62 embeds = self.embedding(x)63 64 65 lstm_out, hidden = self.lstm(embeds, hidden)66 lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim)67 68 69 # drop out and fully connected70 out = self.dropout(lstm_out)71 out = self.fc(out)72 73 # sigmoid 74 75 sig_out = self.sig(out)76 77 #reshape to batch size first78 79 sig_out = sig_out.view(batch_size, -1)80 81 sig_out = sig_out[:, -1]82 83 84 return sig_out, hidden85 86 87 def init_hidden(self, batch_size):88 89 # create hidden state and cell state tensors with size [no_layers x batch_size x hidden_dim]90 91 hidden_state = torch.zeros((self.no_layers, batch_size, self.hidden_dim)).to(device)92 cell_state = torch.zeros((self.no_layers, batch_size, self.hidden_dim)).to(device)93 hidden = (hidden_state, cell_state)94 return hidden95 96# import saved model with weights from pickle file97 98 99# model = pickle.load(open('model.pkl', 'rb'))100vocab = pickle.load(open('vocab.pkl', 'rb'))101PATH = 'model_state.pkl'102model = SentimentLSTM(2, len(vocab)+1, 256, 64)103model.load_state_dict(torch.load(PATH, map_location=device))104model.eval()105 106# pre-processing input data107 108def preprocess_string(s):109 # remove all characters except letters and digits110 s = re.sub(r"[^\w\s]", '', s)111 #remove all extra whites spaces112 s = re.sub(r"\s+", '', s)113 #remove digits114 s = re.sub(r"\d", '', s)115 return s116 117def padding(sents, seq_len):118 features = np.zeros((len(sents), seq_len), dtype = int)119 for i, rev in enumerate(sents):120 if len(rev) != 0:121 features[i, -len(rev):] = np.array(rev)[:seq_len]122 return features123 124 125 126# predict sentiment of given text127 128 129def predict_sentiment(text):130 word_seq = np.array([vocab[preprocess_string(word)] for word in text.split() if preprocess_string(word) in vocab.keys()])131 word_seq = np.expand_dims(word_seq, axis = 0)132 # print(word_seq)133 pad = torch.from_numpy(padding(word_seq, 500))134 135 inputs = pad.to(device)136 batch_size = 1137 h = model.init_hidden(batch_size)138 output, h = model(inputs, h)139 prob = output.item()140 pred = ''141 if prob > 0.5:142 pred = f"This Statement seems Positive ๐ค to us, with probability of {prob}"143 else:144 pred = f"This Statement seems Negative ๐ค to us, with probability of {1-prob}"145 return pred146 147 148# Streamlit UI149 150st.title('Analyse the Sentiment of any Statement ๐ค/๐ค')151 152text = st.text_input("Enter your statement/review here!!")153 154if text != '':155 latest_iteration = st.empty()156 bar = st.progress(1)157 158 for i in range(3):159 # Update the progress bar with each iteration.160 # latest_iteration.text(f'Iteration {i+1}')161 bar.progress((i+1) * 33)162 time.sleep(0.1)163 st.write(predict_sentiment(text))164 165 