Upload 3 files
Browse files- app.py +91 -0
- requirements.txt +5 -0
- style.css +57 -0
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import transformers
|
| 4 |
+
import re
|
| 5 |
+
import string
|
| 6 |
+
import preprocessor as pre
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 10 |
+
|
| 11 |
+
with open("style.css") as f:
|
| 12 |
+
st.markdown('<style>{}</style>'.format(f.read()), unsafe_allow_html=True)
|
| 13 |
+
|
| 14 |
+
# Preparation model and tokenizer
|
| 15 |
+
model_path = "ninahf1503/SA-BERTchatgptapp"
|
| 16 |
+
tokenizer = BertTokenizer.from_pretrained(model_path)
|
| 17 |
+
model = BertForSequenceClassification.from_pretrained(model_path, ignore_mismatched_sizes=True )
|
| 18 |
+
|
| 19 |
+
# Define the maximum sequence length
|
| 20 |
+
seq_max_length = 55
|
| 21 |
+
|
| 22 |
+
# Function to tokenizing input text
|
| 23 |
+
def tokenizing_text(sentence):
|
| 24 |
+
sentence = preprocess_text(sentence)
|
| 25 |
+
encoded = tokenizer.encode_plus(
|
| 26 |
+
sentence,
|
| 27 |
+
add_special_tokens=True,
|
| 28 |
+
max_length=seq_max_length,
|
| 29 |
+
truncation=True,
|
| 30 |
+
padding='max_length',
|
| 31 |
+
return_tensors='pt'
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
input_ids = encoded['input_ids']
|
| 35 |
+
attention_mask = encoded['attention_mask']
|
| 36 |
+
return input_ids, attention_mask
|
| 37 |
+
|
| 38 |
+
# Function to preprocessing input text
|
| 39 |
+
def preprocess_text(sentence):
|
| 40 |
+
re_cleansing = "@\S+|https?:\S+|http?:\S|#[A-Za-z0-9]+|^RT[\s]+|(^|\W)\d+"
|
| 41 |
+
for punctuation in string.punctuation:
|
| 42 |
+
sentence = sentence.encode().decode('unicode_escape')
|
| 43 |
+
sentence = re.sub(r'\n', ' ', sentence)
|
| 44 |
+
sentence = pre.clean(sentence)
|
| 45 |
+
sentence = re.sub(r'[^\w\s]', ' ', sentence)
|
| 46 |
+
sentence = re.sub(r'[0-9]', ' ', sentence)
|
| 47 |
+
sentence = re.sub(re_cleansing, ' ', sentence).strip()
|
| 48 |
+
sentence = sentence.replace(punctuation, '')
|
| 49 |
+
sentence = sentence.lower()
|
| 50 |
+
return sentence
|
| 51 |
+
|
| 52 |
+
# Function to predict sentiment
|
| 53 |
+
def predict_sentiment(input_text):
|
| 54 |
+
input_ids, attention_mask = tokenizing_text(input_text)
|
| 55 |
+
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
outputs = model(input_ids, attention_mask)
|
| 58 |
+
|
| 59 |
+
logits = outputs.logits
|
| 60 |
+
predict_class = torch.argmax(logits, dim=1).item()
|
| 61 |
+
|
| 62 |
+
label_sentiment = {0: "Bad", 1: "Good", 2: "Neutral"}
|
| 63 |
+
predict_label = label_sentiment[predict_class]
|
| 64 |
+
|
| 65 |
+
return predict_label
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# Streamlit web app
|
| 70 |
+
def main():
|
| 71 |
+
st.title("Analisis Sentimen Aplikasi ChatGPT", anchor=False)
|
| 72 |
+
tweet_text = st.text_area(" ", placeholder="Enter the sentence you want to analyze", label_visibility="collapsed")
|
| 73 |
+
|
| 74 |
+
if st.button("SUBMIT"):
|
| 75 |
+
if tweet_text.strip() == "":
|
| 76 |
+
st.title("Text Input Still Empty", anchor=False)
|
| 77 |
+
st.info("Please fill in the sentence you want to analyze")
|
| 78 |
+
else:
|
| 79 |
+
sentiment = predict_sentiment(tweet_text)
|
| 80 |
+
if sentiment == "Good":
|
| 81 |
+
st.title("Sentiment Analysis Results", anchor=False)
|
| 82 |
+
st.markdown('<div style="background-color: #5d9c59; padding: 16px; border-radius: 5px; font-weight: bold; color:white;">This sentence contains a positive sentiment</div>', unsafe_allow_html=True)
|
| 83 |
+
elif sentiment == "Bad":
|
| 84 |
+
st.title("Sentiment Analysis Results", anchor=False)
|
| 85 |
+
st.markdown('<div style="background-color: #df2e38; padding: 16px; border-radius: 5px; font-weight: bold; color:white;">This sentence contains a negative sentiment</div>', unsafe_allow_html=True)
|
| 86 |
+
else:
|
| 87 |
+
st.title("Sentiment Analysis Results", anchor=False)
|
| 88 |
+
st.markdown('<div style="background-color: #ffa500; padding: 16px; border-radius: 5px; font-weight: bold; color:white;">This sentence is neutral</div>', unsafe_allow_html=True)
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit == 1.23.0
|
| 2 |
+
torch
|
| 3 |
+
transformers == 4.30.2
|
| 4 |
+
numpy == 1.23.0
|
| 5 |
+
tweet-preprocessor == 0.6.0
|
style.css
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
span {
|
| 2 |
+
font-size: 28px !important;
|
| 3 |
+
margin-bottom: 5px;
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
.css-zt5igj span {
|
| 7 |
+
text-align: center;
|
| 8 |
+
color: black;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
.stTextArea .css-1om1ktf .st-bs {
|
| 12 |
+
background-color: #fff;
|
| 13 |
+
border: 1px #00381d;
|
| 14 |
+
border-radius: 8px;
|
| 15 |
+
padding: 8px 12px;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
.stButton button {
|
| 19 |
+
background-color: #33fb9a;
|
| 20 |
+
width: 150px;
|
| 21 |
+
height: 40px;
|
| 22 |
+
color: white;
|
| 23 |
+
font-size: 16px;
|
| 24 |
+
border: none;
|
| 25 |
+
border-radius: 8px;
|
| 26 |
+
cursor: pointer;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
.stButton button p {
|
| 30 |
+
color: white;
|
| 31 |
+
font-weight: 600;
|
| 32 |
+
font-size: 18px !important;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
.stButton button:hover {
|
| 36 |
+
background-color: #1bcc76;
|
| 37 |
+
color: white;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
#text-input-still-empty span {
|
| 41 |
+
font-size: 30px !important;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
#sentiment-analysis-results span {
|
| 45 |
+
font-size: 30px !important;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
.stAlert .st-at {
|
| 49 |
+
background-color: #18d379;
|
| 50 |
+
border-radius: 5px;
|
| 51 |
+
color: white;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
.css-5rimss, .css-1w6rlcb p {
|
| 55 |
+
font-weight: bold;
|
| 56 |
+
}
|
| 57 |
+
|