|
import streamlit as st |
|
from transformers import pipeline |
|
|
|
|
|
def main(): |
|
st.title("Sentiment analysis") |
|
|
|
st.header("Add comment") |
|
input = st.text_input("Enter a new comment:") |
|
if st.button("Add"): |
|
add_input_text(input) |
|
result_list(input) |
|
|
|
display_comments() |
|
|
|
|
|
def add_input_text(input): |
|
if input: |
|
if 'input_text' not in st.session_state: |
|
st.session_state.input_text = [] |
|
st.session_state.input_text.append(input) |
|
|
|
|
|
def result_list(input): |
|
if input: |
|
if 'result_list' not in st.session_state: |
|
st.session_state.result_list = [] |
|
pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest") |
|
sentiment = pipe(input) |
|
result = sentiment[0]['label'] |
|
st.session_state.result_list.append(result) |
|
|
|
|
|
def display_comments(): |
|
if 'result_list' in st.session_state: |
|
st.header("Filter by Type") |
|
filter_option = st.selectbox("Select type:", ["All", "Positive", "Negative"]) |
|
|
|
|
|
if filter_option == "All": |
|
st.header(f"{len(st.session_state.result_list)} comments") |
|
elif filter_option == "Positive": |
|
st.header(f"{st.session_state.result_list.count('positive')} comments") |
|
elif filter_option == "Negative": |
|
st.header(f"{st.session_state.result_list.count('negative')} comments") |
|
|
|
for id,result in enumerate(st.session_state.result_list): |
|
if filter_option == "All": |
|
|
|
if result == 'positive': |
|
st.success(st.session_state.input_text[id]) |
|
else: |
|
st.error(st.session_state.input_text[id]) |
|
elif filter_option == "Positive" and result == 'positive': |
|
|
|
st.success(st.session_state.input_text[id]) |
|
elif filter_option == "Negative" and result == 'negative': |
|
st.error(st.session_state.input_text[id]) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|