Tuyet3005 commited on
Commit
24e4457
β€’
1 Parent(s): 0f61eb8

Upload 2 files

Browse files
streamlit_app.py/pages/Sentiment_Analysis.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from os import path
2
+ import streamlit as st
3
+
4
+ # import pickle
5
+
6
+ # from tensorflow import keras
7
+ import tensorflow as tf
8
+ import torch
9
+ from torch import nn
10
+ from transformers import BertModel, BertTokenizer
11
+
12
+
13
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
+ MODEL_NAME = "bert-base-cased"
15
+ MODEL_PATH = path.join(path.dirname(__file__), "bert_model.h5")
16
+
17
+
18
+ # Build the Sentiment Classifier class
19
+ class SentimentClassifier(nn.Module):
20
+ # Constructor class
21
+ def __init__(self, n_classes):
22
+ super(SentimentClassifier, self).__init__()
23
+ self.bert = BertModel.from_pretrained(MODEL_NAME)
24
+ self.drop = nn.Dropout(p=0.3)
25
+ self.out = nn.Linear(self.bert.config.hidden_size, n_classes)
26
+
27
+ # Forward propagaion class
28
+ def forward(self, input_ids, attention_mask):
29
+ _, pooled_output = self.bert(
30
+ input_ids=input_ids, attention_mask=attention_mask, return_dict=False
31
+ )
32
+ # Add a dropout layer
33
+ output = self.drop(pooled_output)
34
+ return self.out(output)
35
+
36
+
37
+ @st.cache_resource
38
+ def load_model_and_tokenizer():
39
+ model = SentimentClassifier(3)
40
+ model.load_state_dict(torch.load(MODEL_PATH, map_location=torch.device("cpu")))
41
+ model.eval()
42
+ return model, BertTokenizer.from_pretrained("bert-base-cased")
43
+
44
+
45
+ def predict(content):
46
+ model, tokenizer = load_model_and_tokenizer()
47
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
48
+
49
+ encoded_review = tokenizer.encode_plus(
50
+ content,
51
+ max_length=160,
52
+ add_special_tokens=True,
53
+ return_token_type_ids=False,
54
+ pad_to_max_length=True,
55
+ return_attention_mask=True,
56
+ return_tensors="pt",
57
+ )
58
+
59
+ input_ids = encoded_review["input_ids"].to(device)
60
+ attention_mask = encoded_review["attention_mask"].to(device)
61
+
62
+ output = model(input_ids, attention_mask)
63
+ _, prediction = torch.max(output, dim=1)
64
+
65
+ class_names = ["negative", "neutral", "positive"]
66
+
67
+ return class_names[prediction]
68
+
69
+
70
+ def main():
71
+ st.set_page_config(page_title="Sentiment Analysis", page_icon="πŸ“")
72
+
73
+ # giving a title to our page
74
+ st.title("Sentiment analysis")
75
+ contents = st.text_area(
76
+ "Please enter reviews/sentiment/setences/contents:",
77
+ placeholder="Enter your text here",
78
+ height=200,
79
+ )
80
+
81
+ prediction = ""
82
+
83
+ # Create a prediction button
84
+ if st.button("Analyze Sentiment"):
85
+ stripped = contents.strip()
86
+ if not stripped:
87
+ st.error("Please enter some text.")
88
+ return
89
+
90
+ prediction = predict(contents)
91
+ if prediction == "positive":
92
+ st.success("This is positive πŸ˜„")
93
+ elif prediction == "negative":
94
+ st.error("This is negative 😟")
95
+ else:
96
+ st.warning("This is neutral πŸ™‚")
97
+
98
+ upload_file = st.file_uploader("Or upload a file", type=["txt"])
99
+ if upload_file is not None:
100
+ contents = upload_file.read().decode("utf-8")
101
+
102
+ for line in contents.splitlines():
103
+ line = line.strip()
104
+ if not line:
105
+ continue
106
+
107
+ prediction = predict(line)
108
+ if prediction == "positive":
109
+ st.success(line + "\n\nThis is positive πŸ˜„")
110
+ elif prediction == "negative":
111
+ st.error(line + "\n\nThis is negative 😟")
112
+ else:
113
+ st.warning(line + "\n\nThis is neutral πŸ™‚")
114
+
115
+
116
+ if __name__ == "__main__":
117
+ main()