Dilwolf's picture
Update app.py
5d525d9 verified
import streamlit as st
import numpy as np
import pandas as pd
import altair as alt
from transformers import BertTokenizer, BertForSequenceClassification
@st.cache_data
def get_model():
tokenizer = BertTokenizer.from_pretrained('Dilwolf/Kakao_app-kr_sentiment')
model = BertForSequenceClassification.from_pretrained("Dilwolf/Kakao_app-kr_sentiment")
return tokenizer, model
tokenizer, model = get_model()
# Define the "How to Use" message
how_to_use = """
**์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•**
1. ํ…์ŠคํŠธ ์˜์—ญ์— ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
2. ํ•œ๊ตญ์–ด ์ž…๋ ฅ ํ…์ŠคํŠธ์˜ ์˜ˆ์ธก ๊ฐ์ •์„ ์–ป์œผ๋ ค๋ฉด '๋ถ„์„' ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์„ธ์š”.
"""
# Functions
def main():
st.title("BERT๋ฅผ ํ™œ์šฉํ•œ ์นด์นด์˜คํ†ก ์•ฑ ๋ฆฌ๋ทฐ ๊ฐ์„ฑ ๋ถ„์„")
st.subheader("Dilshod์˜ ํฌํŠธํด๋ฆฌ์˜ค ํ”„๋กœ์ ํŠธ")
# Add the cover image
st.image("img/kakaotalk.png")
menu = ["ํ™ˆ", "์†Œ๊ฐœ"]
choice = st.sidebar.selectbox("Menu", menu)
# Add the "How to Use" message to the sidebar
st.sidebar.markdown(how_to_use)
if choice == "ํ™ˆ":
st.subheader("ํ™ˆ")
with st.form(key="nlpForm"):
raw_text = st.text_area("์—ฌ๊ธฐ์— ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”!")
submit_button = st.form_submit_button(label="Analyze")
# Layout
col1, col2 = st.columns(2)
if submit_button and raw_text:
# Display balloons
st.balloons()
with col1:
st.info("๊ฒฐ๊ณผ")
# Tokenize the input text
inputs = tokenizer([raw_text], padding=True, truncation=True, max_length=512, return_tensors='pt')
# Make a forward pass through the model
outputs = model(**inputs)
# Get the predicted class and associated score
predicted_class = outputs.logits.argmax().item()
scores = outputs.logits.softmax(dim=1).detach().numpy()[0]
# Mapping of prediction to sentiment labels
sentiment_dict = {0: '๋ถ€์ •์ ์ธ', 1: '๊ธ์ •์ ์ธ'}
sentiment_label = sentiment_dict[predicted_class]
confidence_level = scores[predicted_class]
# Display sentiment
st.write(f"๊ฐ์ •: {sentiment_label}, ์‹ ๋ขฐ ์ ์ˆ˜: {confidence_level:.2f}")
# Emoji and sentiment image
if predicted_class == 1:
st.markdown("๊ฐ์ • ํด๋ž˜์Šค: ๊ธ์ •์ ์ธ :smiley:")
st.image("img/positive_emoji.jpg")
else:
st.markdown("๊ฐ์ • ํด๋ž˜์Šค: ๋ถ€์ •์ ์ธ :angry:")
st.image("img/negative_emoji.jpg")
# Create the results DataFrame
results_df = pd.DataFrame({
'๊ฐ์ • ํด๋ž˜์Šค': ['๋ถ€์ •์ ์ธ', '๊ธ์ •์ ์ธ'],
'Score': scores
})
# Create the Altair chart
chart = alt.Chart(results_df).mark_bar(width=50).encode(
x="๊ฐ์ • ํด๋ž˜์Šค",
y="์‹ ๋ขฐ ์ ์ˆ˜",
color="๊ฐ์ • ํด๋ž˜์Šค"
)
# Display the chart
with col2:
st.altair_chart(chart, use_container_width=True)
st.write(results_df)
else:
st.subheader("์†Œ๊ฐœ")
st.write("์ด๊ฒƒ์€ ๊ตฌ๊ธ€ ํ”Œ๋ ˆ์ด ์Šคํ† ์–ด์—์„œ ๊ฐ€ํ†ก ๋ชจ๋ฐ”์ผ ์•ฑ ๋ฆฌ๋ทฐ๋ฅผ ๋ถ„์„ํ•˜๊ธฐ ์œ„ํ•ด Dilshod๊ฐ€ ๊ฐœ๋ฐœํ•œ ๊ฐ์„ฑ ๋ถ„์„ NLP ์•ฑ์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ๋œ ํ…์ŠคํŠธ์˜ ๊ฐ์ •์„ ์˜ˆ์ธกํ•˜๊ธฐ ์œ„ํ•ด ์ •๋ฐ€ํ•˜๊ฒŒ ์กฐ์ •๋œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด ์•ฑ์€ ์ €์˜ NLP ๊ธฐ์ˆ ๊ณผ ๊ฐœ๋ฐœ์ž ๊ฐ„์˜ ํ˜‘์—…์„ ๋ณด์—ฌ์ฃผ๋Š” ํฌํŠธํด๋ฆฌ์˜ค ํ”„๋กœ์ ํŠธ์˜ ์ผ๋ถ€์ž…๋‹ˆ๋‹ค.")
if __name__ == "__main__":
main()