Spaces:
Sleeping
Sleeping
import streamlit as st | |
import numpy as np | |
import pandas as pd | |
import altair as alt | |
from transformers import BertTokenizer, BertForSequenceClassification | |
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() | |