Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,10 +12,58 @@ def main():
|
|
12 |
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
13 |
|
14 |
st.title("Email Analysis Tool")
|
15 |
-
st.write("Enter an email body below
|
16 |
|
17 |
-
|
|
|
|
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
if st.button("Analyze Email"):
|
20 |
if email_body:
|
21 |
# Step 1: Check if the email is spam
|
@@ -31,21 +79,4 @@ def main():
|
|
31 |
inputs = tokenizer(email_body, padding=True, truncation=True, return_tensors='pt')
|
32 |
outputs = sentiment_model(**inputs)
|
33 |
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
34 |
-
predictions = predictions.cpu().detach
|
35 |
-
sentiment_index = np.argmax(predictions)
|
36 |
-
sentiment_confidence = predictions[0][sentiment_index]
|
37 |
-
|
38 |
-
sentiment = "Positive" if sentiment_index == 1 else "Negative"
|
39 |
-
|
40 |
-
if sentiment == "Positive":
|
41 |
-
st.write(f"This email is not spam (Confidence: {spam_confidence:.2f}).")
|
42 |
-
st.write(f"Sentiment: {sentiment} (Confidence: {sentiment_confidence:.2f}). No follow-up needed.")
|
43 |
-
else:
|
44 |
-
st.write(f"This email is not spam (Confidence: {spam_confidence:.2f}).")
|
45 |
-
st.write(f"Sentiment: {sentiment} (Confidence: {sentiment_confidence:.2f}).")
|
46 |
-
st.write("**This email needs follow-up as it is not spam and has negative sentiment.**")
|
47 |
-
else:
|
48 |
-
st.write("Please enter an email body to analyze.")
|
49 |
-
|
50 |
-
if __name__ == "__main__":
|
51 |
-
main()
|
|
|
12 |
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
13 |
|
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
|
60 |
+
st.rerun()
|
61 |
+
with col3:
|
62 |
+
if st.button("Not Spam, Negative"):
|
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
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|