ISOM5240GP4 commited on
Commit
48034cd
·
verified ·
1 Parent(s): 6f7d2fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -28
app.py CHANGED
@@ -13,7 +13,7 @@ def analyze_email(email_body):
13
  spam_confidence = spam_result[0]["score"]
14
 
15
  if spam_label == "LABEL_1":
16
- return f"This is a spam email (Confidence: {spam_confidence:.2f}). No follow-up needed."
17
  else:
18
  inputs = tokenizer(email_body, padding=True, truncation=True, return_tensors='pt')
19
  outputs = sentiment_model(**inputs)
@@ -24,12 +24,12 @@ def analyze_email(email_body):
24
  sentiment = "Positive" if sentiment_index == 1 else "Negative"
25
 
26
  if sentiment == "Positive":
27
- return (f"This email is not spam (Confidence: {spam_confidence:.2f}).\n"
28
- f"Sentiment: {sentiment} (Confidence: {sentiment_confidence:.2f}). No follow-up needed.")
29
  else:
30
- return (f"This email is not spam (Confidence: {spam_confidence:.2f}).\n"
31
- f"Sentiment: {sentiment} (Confidence: {sentiment_confidence:.2f}).\n"
32
- "**This email needs follow-up as it is not spam and has negative sentiment.**")
33
 
34
  def main():
35
  st.title("Email Analysis Tool")
@@ -40,6 +40,17 @@ def main():
40
  st.session_state.email_body = ""
41
  if "result" not in st.session_state:
42
  st.session_state.result = ""
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  # Text area for email input
45
  email_body = st.text_area("Email Body", value=st.session_state.email_body, height=200, key="email_input")
@@ -74,63 +85,115 @@ Sarah
74
  """
75
  negative_snippet = "Subject: Issue with Recent Delivery Dear Support, I received my package today, but..."
76
 
77
- # Custom CSS for buttons
78
  st.markdown("""
79
  <style>
80
- /* Style for sample buttons (smaller text) */
81
  div.stButton > button[kind="secondary"] {
82
- font-size: 12px; /* Smaller text */
83
- padding: 5px 10px; /* Smaller padding */
84
- background-color: #f0f0f0; /* Light gray background */
85
- color: #333333; /* Darker text */
86
  border: 1px solid #cccccc;
87
  border-radius: 3px;
88
  }
89
- /* Style for Analyze Email button (larger, colored) */
90
  div.stButton > button[kind="primary"] {
91
- background-color: #FF5733; /* Orange color */
92
  color: white;
93
- font-size: 18px; /* Larger text */
94
- padding: 12px 24px; /* Larger padding */
95
  border: none;
96
  border-radius: 5px;
97
- display: block;
98
- margin-top: 15px;
99
  }
100
  div.stButton > button[kind="primary"]:hover {
101
- background-color: #E74C3C; /* Darker orange on hover */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  }
103
  </style>
104
  """, unsafe_allow_html=True)
105
 
106
- # Buttons with sample content (in columns)
107
  col1, col2, col3 = st.columns(3)
108
  with col1:
109
  if st.button(spam_snippet, key="spam_sample"):
110
  st.session_state.email_body = sample_spam
111
  st.session_state.result = ""
 
112
  st.rerun()
113
  with col2:
114
  if st.button(positive_snippet, key="positive_sample"):
115
  st.session_state.email_body = sample_not_spam_positive
116
  st.session_state.result = ""
 
117
  st.rerun()
118
  with col3:
119
  if st.button(negative_snippet, key="negative_sample"):
120
  st.session_state.email_body = sample_not_spam_negative
121
  st.session_state.result = ""
 
122
  st.rerun()
123
 
124
- # Analyze Email button (distinct style)
125
- if st.button("Analyze Email", key="analyze", type="primary"):
126
- if email_body:
127
- st.session_state.result = analyze_email(email_body)
128
- else:
129
- st.session_state.result = "Please enter an email body or select a sample to analyze."
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
- # Display result
132
  if st.session_state.result:
133
- st.write(st.session_state.result)
 
 
 
 
 
 
 
134
 
135
  if __name__ == "__main__":
136
  main()
 
13
  spam_confidence = spam_result[0]["score"]
14
 
15
  if spam_label == "LABEL_1":
16
+ return "spam", f"This is a spam email (Confidence: {spam_confidence:.2f}). No follow-up needed."
17
  else:
18
  inputs = tokenizer(email_body, padding=True, truncation=True, return_tensors='pt')
19
  outputs = sentiment_model(**inputs)
 
24
  sentiment = "Positive" if sentiment_index == 1 else "Negative"
25
 
26
  if sentiment == "Positive":
27
+ return "positive", (f"This email is not spam (Confidence: {spam_confidence:.2f}).\n"
28
+ f"Sentiment: {sentiment} (Confidence: {sentiment_confidence:.2f}). No follow-up needed.")
29
  else:
30
+ return "negative", (f"This email is not spam (Confidence: {spam_confidence:.2f}).\n"
31
+ f"Sentiment: {sentiment} (Confidence: {sentiment_confidence:.2f}).\n"
32
+ "**This email needs follow-up as it is not spam and has negative sentiment.**")
33
 
34
  def main():
35
  st.title("Email Analysis Tool")
 
40
  st.session_state.email_body = ""
41
  if "result" not in st.session_state:
42
  st.session_state.result = ""
43
+ if "result_type" not in st.session_state:
44
+ st.session_state.result_type = ""
45
+
46
+ # Collapsible instructions
47
+ with st.expander("How to Use", expanded=False):
48
+ st.write("""
49
+ - Type or paste an email into the text box.
50
+ - Alternatively, click one of the sample buttons to load a predefined email.
51
+ - Press 'Analyze Email' to check if it’s spam and analyze its sentiment.
52
+ - Use 'Clear' to reset the input and result.
53
+ """)
54
 
55
  # Text area for email input
56
  email_body = st.text_area("Email Body", value=st.session_state.email_body, height=200, key="email_input")
 
85
  """
86
  negative_snippet = "Subject: Issue with Recent Delivery Dear Support, I received my package today, but..."
87
 
88
+ # Custom CSS for buttons and result boxes
89
  st.markdown("""
90
  <style>
91
+ /* Sample buttons (smaller text) */
92
  div.stButton > button[kind="secondary"] {
93
+ font-size: 12px;
94
+ padding: 5px 10px;
95
+ background-color: #f0f0f0;
96
+ color: #333333;
97
  border: 1px solid #cccccc;
98
  border-radius: 3px;
99
  }
100
+ /* Analyze Email button (larger, orange) */
101
  div.stButton > button[kind="primary"] {
102
+ background-color: #FF5733;
103
  color: white;
104
+ font-size: 18px;
105
+ padding: 12px 24px;
106
  border: none;
107
  border-radius: 5px;
108
+ margin-right: 10px;
 
109
  }
110
  div.stButton > button[kind="primary"]:hover {
111
+ background-color: #E74C3C;
112
+ }
113
+ /* Clear button (gray) */
114
+ div.stButton > button[kind="secondary"][key="clear"] {
115
+ background-color: #d3d3d3;
116
+ color: #333333;
117
+ font-size: 16px;
118
+ padding: 10px 20px;
119
+ border: none;
120
+ border-radius: 5px;
121
+ }
122
+ div.stButton > button[kind="secondary"][key="clear"]:hover {
123
+ background-color: #b0b0b0;
124
+ }
125
+ /* Result boxes */
126
+ .spam-result {
127
+ background-color: #ffcccc;
128
+ padding: 10px;
129
+ border-radius: 5px;
130
+ border: 1px solid #ff9999;
131
+ }
132
+ .positive-result {
133
+ background-color: #ccffcc;
134
+ padding: 10px;
135
+ border-radius: 5px;
136
+ border: 1px solid #99cc99;
137
+ }
138
+ .negative-result {
139
+ background-color: #fff3cc;
140
+ padding: 10px;
141
+ border-radius: 5px;
142
+ border: 1px solid #ffcc66;
143
  }
144
  </style>
145
  """, unsafe_allow_html=True)
146
 
147
+ # Sample buttons (in columns)
148
  col1, col2, col3 = st.columns(3)
149
  with col1:
150
  if st.button(spam_snippet, key="spam_sample"):
151
  st.session_state.email_body = sample_spam
152
  st.session_state.result = ""
153
+ st.session_state.result_type = ""
154
  st.rerun()
155
  with col2:
156
  if st.button(positive_snippet, key="positive_sample"):
157
  st.session_state.email_body = sample_not_spam_positive
158
  st.session_state.result = ""
159
+ st.session_state.result_type = ""
160
  st.rerun()
161
  with col3:
162
  if st.button(negative_snippet, key="negative_sample"):
163
  st.session_state.email_body = sample_not_spam_negative
164
  st.session_state.result = ""
165
+ st.session_state.result_type = ""
166
  st.rerun()
167
 
168
+ # Analyze and Clear buttons (in a row)
169
+ col_analyze, col_clear = st.columns([1, 1])
170
+ with col_analyze:
171
+ if st.button("Analyze Email", key="analyze", type="primary"):
172
+ if email_body:
173
+ with st.spinner("Analyzing email..."):
174
+ result_type, result = analyze_email(email_body)
175
+ st.session_state.result = result
176
+ st.session_state.result_type = result_type
177
+ else:
178
+ st.session_state.result = "Please enter an email body or select a sample to analyze."
179
+ st.session_state.result_type = ""
180
+ with col_clear:
181
+ if st.button("Clear", key="clear"):
182
+ st.session_state.email_body = ""
183
+ st.session_state.result = ""
184
+ st.session_state.result_type = ""
185
+ st.rerun()
186
 
187
+ # Display result with styled box
188
  if st.session_state.result:
189
+ if st.session_state.result_type == "spam":
190
+ st.markdown(f'<div class="spam-result">{st.session_state.result}</div>', unsafe_allow_html=True)
191
+ elif st.session_state.result_type == "positive":
192
+ st.markdown(f'<div class="positive-result">{st.session_state.result}</div>', unsafe_allow_html=True)
193
+ elif st.session_state.result_type == "negative":
194
+ st.markdown(f'<div class="negative-result">{st.session_state.result}</div>', unsafe_allow_html=True)
195
+ else:
196
+ st.write(st.session_state.result) # For error messages
197
 
198
  if __name__ == "__main__":
199
  main()