Tuyet3005 commited on
Commit
5ea949b
β€’
1 Parent(s): febe13d

Upload 3 files

Browse files
.streamlit/config.toml ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [server]
2
+ runOnSave = true
3
+ fileWatcherType = "poll"
streamlit_app.py/Homepage.py CHANGED
@@ -1,22 +1,46 @@
1
  import streamlit as st
 
2
 
3
- st.set_page_config(
4
- page_title="Sentiment Detection"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  )
6
-
7
- st.title("Sentiment Detection")
8
-
9
- st.sidebar.success("Select a page above.")
10
 
11
  st.header("The Need for Sentiment Detection")
12
- st.text("""
13
- Spam detection algorithms are used to detect and filter junk and spam emails with a high level of accuracy.
14
- It is said that around half of all emails are spam, depending on the user. These emails can include scams or viruses intended to cause harm.
15
- """)
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- st.header("Data Source")
18
- st.text("""
19
- Data Source: Preprocessed TREC 2007 Public Corpus Dataset.
20
- Link: https://www.kaggle.com/datasets/imdeepmind/preprocessed-trec-2007-public-corpus-dataset
21
-
22
- """)
 
1
  import streamlit as st
2
+ from st_pages import Page, show_pages
3
 
4
+ st.set_page_config(page_title="Sentiment Detection", page_icon="🏠")
5
+
6
+ show_pages(
7
+ [
8
+ Page("streamlit_app.py/Homepage.py", "Home", "🏠"),
9
+ Page(
10
+ "streamlit_app.py/pages/Sentiment_Detection.py", "Sentiment Detection", "πŸ“"
11
+ ),
12
+ ]
13
+ )
14
+
15
+ st.title("Final Project in Machine Learning Course - Sentiment Detection")
16
+ st.markdown(
17
+ """
18
+ **Team members:**
19
+ | Student ID | Full Name |
20
+ | ---------- | ------------------------ |
21
+ | 19120600 | BΓΉi NguyΓͺn NghΔ©a |
22
+ | 20120089 | LΓͺ XuΓ’n HoΓ ng |
23
+ | 20120422 | Nguyα»…n Thα»‹ Ánh TuyαΊΏt |
24
+ | 20120460 | LΓͺ Nguyα»…n HαΊ£i DΖ°Ζ‘ng |
25
+ | 20120494 | LΓͺ XuΓ’n Huy |
26
+ """
27
  )
 
 
 
 
28
 
29
  st.header("The Need for Sentiment Detection")
30
+ st.markdown(
31
+ """
32
+ Sentiment detection algorithms are used to detect sentiment in a comment or a review.
33
+ It is said that around 90% of consumers read online reviews before visiting a business or buying a product.
34
+ These reviews can be positive or negative or neutral, and it is important to know what the customers are saying about your business.
35
+ """
36
+ )
37
+
38
+ st.header("Technology used")
39
+ st.markdown(
40
+ """
41
+ In this demo, we used BERT as the model for sentiment detection. BERT is a transformer-based model that was proposed in 2018 by Google.
42
+ It is a pre-trained model that can be used for various NLP tasks such as sentiment detection, question answering, etc.
43
+ """
44
+ )
45
+
46
 
 
 
 
 
 
 
streamlit_app.py/pages/Sentiment_Detection.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 Detection", page_icon="πŸ“")
72
+
73
+ # giving a title to our page
74
+ st.title("Sentiment detection")
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()