ISOM5240GP4 commited on
Commit
b4fad3e
·
verified ·
1 Parent(s): 67c7b80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -34
app.py CHANGED
@@ -14,46 +14,21 @@ def main():
14
  st.title("Email Analysis Tool")
15
  st.write("Enter an email body below or select a sample to analyze its spam status and sentiment.")
16
 
17
- # Initialize session state for the text area
18
  if "email_body" not in st.session_state:
19
  st.session_state.email_body = ""
20
 
21
- # Text area for email input
22
  email_body = st.text_area("Email Body", value=st.session_state.email_body, height=200, key="email_input")
23
 
24
  # Sample emails
25
- sample_spam = """
26
- Subject: Urgent: Verify Your Account Now!
27
- Dear Customer,
28
- We have detected unusual activity on your account. To prevent suspension, please verify your login details immediately by clicking the link below:
29
- [Click Here to Verify](http://totally-legit-site.com/verify)
30
- Failure to verify within 24 hours will result in your account being locked. This is for your security.
31
- Best regards,
32
- The Security Team
33
- """
34
 
35
- sample_not_spam_positive = """
36
- Subject: Great News About Your Project!
37
- Hi Team,
38
- I just wanted to let you know that the project is progressing wonderfully! Everyone’s efforts are paying off, and we’re ahead of schedule. Keep up the fantastic work!
39
- Best,
40
- Alex
41
- """
42
-
43
- sample_not_spam_negative = """
44
- Subject: Issue with Recent Delivery
45
- Dear Support,
46
- I received my package today, but it was damaged, and two items were missing. This is really frustrating—please let me know how we can resolve this as soon as possible.
47
- Thanks,
48
- Sarah
49
- """
50
-
51
- # Buttons for sample emails (in columns for better layout)
52
  col1, col2, col3 = st.columns(3)
53
  with col1:
54
  if st.button("Spam Email"):
55
  st.session_state.email_body = sample_spam
56
- st.rerun() # Rerun to update the text area
57
  with col2:
58
  if st.button("Not Spam, Positive"):
59
  st.session_state.email_body = sample_not_spam_positive
@@ -63,20 +38,32 @@ Sarah
63
  st.session_state.email_body = sample_not_spam_negative
64
  st.rerun()
65
 
66
- # Button to trigger analysis
67
  if st.button("Analyze Email"):
68
  if email_body:
69
- # Step 1: Check if the email is spam
70
  spam_result = spam_pipeline(email_body)
71
  spam_label = spam_result[0]["label"]
72
  spam_confidence = spam_result[0]["score"]
73
 
74
- # Check if label is 'LABEL_1' for spam
75
  if spam_label == "LABEL_1":
76
  st.write(f"This is a spam email (Confidence: {spam_confidence:.2f}). No follow-up needed.")
77
  else:
78
- # Step 2: Analyze sentiment for non-spam emails
79
  inputs = tokenizer(email_body, padding=True, truncation=True, return_tensors='pt')
80
  outputs = sentiment_model(**inputs)
81
  predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
82
- predictions = predictions.cpu().detach
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  st.title("Email Analysis Tool")
15
  st.write("Enter an email body below or select a sample to analyze its spam status and sentiment.")
16
 
 
17
  if "email_body" not in st.session_state:
18
  st.session_state.email_body = ""
19
 
 
20
  email_body = st.text_area("Email Body", value=st.session_state.email_body, height=200, key="email_input")
21
 
22
  # Sample emails
23
+ sample_spam = """[Spam email content]"""
24
+ sample_not_spam_positive = """[Positive email content]"""
25
+ sample_not_spam_negative = """[Negative email content]"""
 
 
 
 
 
 
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  col1, col2, col3 = st.columns(3)
28
  with col1:
29
  if st.button("Spam Email"):
30
  st.session_state.email_body = sample_spam
31
+ st.rerun()
32
  with col2:
33
  if st.button("Not Spam, Positive"):
34
  st.session_state.email_body = sample_not_spam_positive
 
38
  st.session_state.email_body = sample_not_spam_negative
39
  st.rerun()
40
 
 
41
  if st.button("Analyze Email"):
42
  if email_body:
 
43
  spam_result = spam_pipeline(email_body)
44
  spam_label = spam_result[0]["label"]
45
  spam_confidence = spam_result[0]["score"]
46
 
 
47
  if spam_label == "LABEL_1":
48
  st.write(f"This is a spam email (Confidence: {spam_confidence:.2f}). No follow-up needed.")
49
  else:
 
50
  inputs = tokenizer(email_body, padding=True, truncation=True, return_tensors='pt')
51
  outputs = sentiment_model(**inputs)
52
  predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
53
+ predictions = predictions.cpu().detach().numpy()
54
+ sentiment_index = np.argmax(predictions)
55
+ sentiment_confidence = predictions[0][sentiment_index]
56
+
57
+ sentiment = "Positive" if sentiment_index == 1 else "Negative"
58
+ if sentiment == "Positive":
59
+ st.write(f"This email is not spam (Confidence: {spam_confidence:.2f}).")
60
+ st.write(f"Sentiment: {sentiment} (Confidence: {sentiment_confidence:.2f}). No follow-up needed.")
61
+ else:
62
+ st.write(f"This email is not spam (Confidence: {spam_confidence:.2f}).")
63
+ st.write(f"Sentiment: {sentiment} (Confidence: {sentiment_confidence:.2f}).")
64
+ st.write("**This email needs follow-up as it is not spam and has negative sentiment.**")
65
+ else:
66
+ st.write("Please enter an email body or select a sample to analyze.")
67
+
68
+ if __name__ == "__main__":
69
+ main()