File size: 2,179 Bytes
1694d91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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":
                # st.header(f"{len(st.session_state.result_list)} comments")
                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.header(f"{st.session_state.result_list.count('positive')} comments")
                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()