Khaldi Abderrahmane commited on
Commit
ef38953
·
1 Parent(s): 4362255

Add application file

Browse files
Files changed (1) hide show
  1. app.py +144 -0
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ import pandas as pd
4
+
5
+ from transformers import AutoTokenizer, AutoModel,AutoModelForSequenceClassification
6
+ import torch
7
+
8
+
9
+
10
+ num_classes = 6
11
+
12
+
13
+
14
+
15
+
16
+ #tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
17
+ tokenizer = AutoTokenizer.from_pretrained("KhaldiAbderrhmane/bert-emotion",trust_remote_code=True)
18
+
19
+
20
+
21
+ model = AutoModelForSequenceClassification.from_pretrained("KhaldiAbderrhmane/bert-emotion",trust_remote_code=True)
22
+
23
+
24
+
25
+ def prediction_sentiment(review):
26
+
27
+
28
+ t= tokenizer(review, truncation=True, padding=True, max_length=128, return_tensors='pt')
29
+ inpt = t['input_ids']
30
+ mask = t['attention_mask']
31
+
32
+
33
+
34
+
35
+ outputs = model(inpt,mask)
36
+
37
+
38
+ outputs = outputs.logits
39
+
40
+
41
+
42
+ predicted= torch.max(outputs, 1).indices
43
+
44
+ if predicted == 0:
45
+ sentiment = "Sadness"
46
+ elif predicted == 1:
47
+ sentiment = "Joy"
48
+ elif predicted == 2:
49
+ sentiment = "Love"
50
+ elif predicted == 3:
51
+ sentiment = "Anger"
52
+ elif predicted == 4:
53
+ sentiment = "Fear"
54
+ else:
55
+ sentiment = "Surprise"
56
+ return sentiment
57
+
58
+
59
+
60
+
61
+
62
+ users = {"abdelmalek": [["this movie was so nice", "positive"], ["what the hell was that", "negative"], ["man this was good", "positive"]]}
63
+ columns = ["comment", "sentiment"]
64
+ user_name = st.text_input("User Name")
65
+
66
+ if user_name:
67
+ if user_name in users:
68
+ user_input = st.text_input("Enter your comment:")
69
+
70
+
71
+ if user_input:
72
+
73
+ sentiment = prediction_sentiment(user_input)
74
+ st.write('Your sentiment is:', sentiment)
75
+ users[user_name].append([user_input, sentiment])
76
+ else:
77
+ users[user_name] = []
78
+ st.write("Your user name has been added.")
79
+ user_input = st.text_input("Enter your comment:")
80
+ if user_input:
81
+ sentiment = prediction_sentiment(user_input)
82
+ st.write('Your sentiment is:', sentiment)
83
+ users[user_name].append([user_input, sentiment])
84
+
85
+ if st.button("Your comment:"):
86
+ if user_name in users:
87
+ df_t = pd.DataFrame(users[user_name], columns=columns)
88
+ card_css = """
89
+ <style>
90
+ .card {
91
+ background-color: #1A2E4D;
92
+ border-radius: 10px;
93
+ padding: 20px;
94
+ margin: 10px 0;
95
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
96
+ }
97
+ .card-title {
98
+ font-size: 24px;
99
+ font-weight: bold;
100
+ color: #F4F6FF;
101
+ }
102
+ .card-content {
103
+ font-size: 18px;
104
+ color: #52709E;
105
+ }
106
+ .sentiment-circle {
107
+ width: 15px; /* Adjust size as needed */
108
+ height: 15px; /* Adjust size as needed */
109
+ border-radius: 50%;
110
+ display: inline-block;
111
+ position: absolute;
112
+ top: 50%;
113
+ right: 0;
114
+ transform: translateY(-50%);
115
+ margin-right: 10px; /* Space between circle and text */
116
+ }
117
+ .positive {
118
+
119
+ background-color: #82D853; /* Green background for positive sentiment */
120
+ }
121
+ .negative {
122
+
123
+ background-color: #D85353; /* Red background for negative sentiment */
124
+ }
125
+ </style>
126
+ """
127
+ st.markdown(card_css, unsafe_allow_html=True)
128
+
129
+ for comment, sentiment in df_t.values:
130
+ sentiment_class = "positive" if sentiment == "positive" else "negative"
131
+ sentiment_circle = f'<div class="sentiment-circle {sentiment_class}" style="background-color: {"#82D853" if sentiment == "positive" else "#D85353"};"></div>'
132
+ border_color = "border: 2px solid #82D853;" if sentiment == "positive" else "border: 2px solid #D85353;"
133
+ card_content = f"""
134
+ <div class="card" style="{border_color}">
135
+ <div class="card-title">{user_name}</div>
136
+ <div class="card-content">
137
+ {comment}
138
+ {sentiment_circle}
139
+ </div>
140
+ </div>
141
+ """
142
+ st.markdown(card_content, unsafe_allow_html=True)
143
+ else:
144
+ st.error("No history available for this user.")