Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- app.py +100 -0
- img/kakaotalk.png +0 -0
- img/negative_emoji.jpg +0 -0
- img/positive_emoji.jpg +0 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import altair as alt
|
| 5 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 6 |
+
|
| 7 |
+
@st.cache_data
|
| 8 |
+
def get_model():
|
| 9 |
+
tokenizer = BertTokenizer.from_pretrained('Dilwolf/Kakao_app-kr_sentiment')
|
| 10 |
+
model = BertForSequenceClassification.from_pretrained("Dilwolf/Kakao_app-kr_sentiment")
|
| 11 |
+
return tokenizer, model
|
| 12 |
+
|
| 13 |
+
tokenizer, model = get_model()
|
| 14 |
+
|
| 15 |
+
# Define the "How to Use" message
|
| 16 |
+
how_to_use = """
|
| 17 |
+
**How to Use**
|
| 18 |
+
1. Enter text in the text area
|
| 19 |
+
2. Click the 'Analyze' button to get the predicted sentiment of the input text
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
# Functions
|
| 23 |
+
def main():
|
| 24 |
+
st.title("Kakao App Review Sentiment Analysis using BERT")
|
| 25 |
+
st.subheader("Dilshod's Portfolio Project")
|
| 26 |
+
|
| 27 |
+
# Add the cover image
|
| 28 |
+
st.image("img/kakaotalk.png")
|
| 29 |
+
|
| 30 |
+
menu = ["Home", "About"]
|
| 31 |
+
choice = st.sidebar.selectbox("Menu", menu)
|
| 32 |
+
|
| 33 |
+
# Add the "How to Use" message to the sidebar
|
| 34 |
+
st.sidebar.markdown(how_to_use)
|
| 35 |
+
|
| 36 |
+
if choice == "Home":
|
| 37 |
+
st.subheader("Home")
|
| 38 |
+
|
| 39 |
+
with st.form(key="nlpForm"):
|
| 40 |
+
raw_text = st.text_area("Enter Text Here")
|
| 41 |
+
submit_button = st.form_submit_button(label="Analyze")
|
| 42 |
+
|
| 43 |
+
# Layout
|
| 44 |
+
col1, col2 = st.columns(2)
|
| 45 |
+
if submit_button and raw_text:
|
| 46 |
+
# Display balloons
|
| 47 |
+
st.balloons()
|
| 48 |
+
with col1:
|
| 49 |
+
st.info("Results")
|
| 50 |
+
# Tokenize the input text
|
| 51 |
+
inputs = tokenizer([raw_text], padding=True, truncation=True, max_length=512, return_tensors='pt')
|
| 52 |
+
|
| 53 |
+
# Make a forward pass through the model
|
| 54 |
+
outputs = model(**inputs)
|
| 55 |
+
|
| 56 |
+
# Get the predicted class and associated score
|
| 57 |
+
predicted_class = outputs.logits.argmax().item()
|
| 58 |
+
scores = outputs.logits.softmax(dim=1).detach().numpy()[0]
|
| 59 |
+
|
| 60 |
+
# Mapping of prediction to sentiment labels
|
| 61 |
+
sentiment_dict = {0: 'Negative', 1: 'Positive'}
|
| 62 |
+
sentiment_label = sentiment_dict[predicted_class]
|
| 63 |
+
confidence_level = scores[predicted_class]
|
| 64 |
+
|
| 65 |
+
# Display sentiment
|
| 66 |
+
st.write(f"Sentiment: {sentiment_label}, Confidence Level: {confidence_level:.2f}")
|
| 67 |
+
|
| 68 |
+
# Emoji and sentiment image
|
| 69 |
+
if predicted_class == 1:
|
| 70 |
+
st.markdown("Sentiment: Positive :smiley:")
|
| 71 |
+
st.image("img/positive_emoji.jpg")
|
| 72 |
+
else:
|
| 73 |
+
st.markdown("Sentiment: Negative :angry:")
|
| 74 |
+
st.image("img/negative_emoji.jpg")
|
| 75 |
+
|
| 76 |
+
# Create the results DataFrame
|
| 77 |
+
results_df = pd.DataFrame({
|
| 78 |
+
'Sentiment Class': ['Negative', 'Positive'],
|
| 79 |
+
'Score': scores
|
| 80 |
+
})
|
| 81 |
+
|
| 82 |
+
# Create the Altair chart
|
| 83 |
+
chart = alt.Chart(results_df).mark_bar(width=50).encode(
|
| 84 |
+
x="Sentiment Class",
|
| 85 |
+
y="Score",
|
| 86 |
+
color="Sentiment Class"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Display the chart
|
| 90 |
+
with col2:
|
| 91 |
+
st.altair_chart(chart, use_container_width=True)
|
| 92 |
+
st.write(results_df)
|
| 93 |
+
|
| 94 |
+
else:
|
| 95 |
+
st.subheader("About")
|
| 96 |
+
st.write("This is a sentiment analysis NLP app developed by Dilshod for analyzing reviews for KakakTalk mobile app on Google Play Store. It uses a fine-tuned model to predict the sentiment of the input text. The app is part of a portfolio project to showcase my nlp skills and collaboration among developers.")
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
main()
|
| 100 |
+
|
img/kakaotalk.png
ADDED
|
img/negative_emoji.jpg
ADDED
|
img/positive_emoji.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
torch
|
| 3 |
+
transformers
|
| 4 |
+
altair
|
| 5 |
+
pandas
|