DilshanKavinda commited on
Commit
1694d91
·
verified ·
1 Parent(s): c689409

create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+
4
+
5
+ def main():
6
+ st.title("Sentiment analysis")
7
+
8
+ st.header("Add comment")
9
+ input = st.text_input("Enter a new comment:")
10
+ if st.button("Add"):
11
+ add_input_text(input)
12
+ result_list(input)
13
+
14
+ display_comments()
15
+
16
+
17
+ def add_input_text(input):
18
+ if input:
19
+ if 'input_text' not in st.session_state:
20
+ st.session_state.input_text = []
21
+ st.session_state.input_text.append(input)
22
+
23
+
24
+ def result_list(input):
25
+ if input:
26
+ if 'result_list' not in st.session_state:
27
+ st.session_state.result_list = []
28
+ pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
29
+ sentiment = pipe(input)
30
+ result = sentiment[0]['label']
31
+ st.session_state.result_list.append(result)
32
+
33
+
34
+ def display_comments():
35
+ if 'result_list' in st.session_state:
36
+ st.header("Filter by Type")
37
+ filter_option = st.selectbox("Select type:", ["All", "Positive", "Negative"])
38
+
39
+
40
+ if filter_option == "All":
41
+ st.header(f"{len(st.session_state.result_list)} comments")
42
+ elif filter_option == "Positive":
43
+ st.header(f"{st.session_state.result_list.count('positive')} comments")
44
+ elif filter_option == "Negative":
45
+ st.header(f"{st.session_state.result_list.count('negative')} comments")
46
+
47
+ for id,result in enumerate(st.session_state.result_list):
48
+ if filter_option == "All":
49
+ # st.header(f"{len(st.session_state.result_list)} comments")
50
+ if result == 'positive':
51
+ st.success(st.session_state.input_text[id])
52
+ else:
53
+ st.error(st.session_state.input_text[id])
54
+ elif filter_option == "Positive" and result == 'positive':
55
+ # st.header(f"{st.session_state.result_list.count('positive')} comments")
56
+ st.success(st.session_state.input_text[id])
57
+ elif filter_option == "Negative" and result == 'negative':
58
+ st.error(st.session_state.input_text[id])
59
+
60
+
61
+ if __name__ == "__main__":
62
+ main()