Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,12 +4,10 @@ import torch
|
|
4 |
import numpy as np
|
5 |
|
6 |
def analyze_email(email_body):
|
7 |
-
# Load models (ideally cached, but kept here for simplicity)
|
8 |
spam_pipeline = pipeline("text-classification", model="cybersectony/phishing-email-detection-distilbert_v2.4.1")
|
9 |
sentiment_model = AutoModelForSequenceClassification.from_pretrained("ISOM5240GP4/email_sentiment", num_labels=2)
|
10 |
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
11 |
|
12 |
-
# Step 1: Check if the email is spam
|
13 |
spam_result = spam_pipeline(email_body)
|
14 |
spam_label = spam_result[0]["label"]
|
15 |
spam_confidence = spam_result[0]["score"]
|
@@ -17,7 +15,6 @@ def analyze_email(email_body):
|
|
17 |
if spam_label == "LABEL_1":
|
18 |
return f"This is a spam email (Confidence: {spam_confidence:.2f}). No follow-up needed."
|
19 |
else:
|
20 |
-
# Step 2: Analyze sentiment for non-spam emails
|
21 |
inputs = tokenizer(email_body, padding=True, truncation=True, return_tensors='pt')
|
22 |
outputs = sentiment_model(**inputs)
|
23 |
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
@@ -77,12 +74,41 @@ Sarah
|
|
77 |
"""
|
78 |
negative_snippet = "Subject: Issue with Recent Delivery Dear Support, I received my package today, but..."
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
# Buttons with sample content (in columns)
|
81 |
col1, col2, col3 = st.columns(3)
|
82 |
with col1:
|
83 |
if st.button(spam_snippet, key="spam_sample"):
|
84 |
st.session_state.email_body = sample_spam
|
85 |
-
st.session_state.result = ""
|
86 |
st.rerun()
|
87 |
with col2:
|
88 |
if st.button(positive_snippet, key="positive_sample"):
|
@@ -95,27 +121,8 @@ Sarah
|
|
95 |
st.session_state.result = ""
|
96 |
st.rerun()
|
97 |
|
98 |
-
#
|
99 |
-
st.
|
100 |
-
<style>
|
101 |
-
.analyze-button {
|
102 |
-
background-color: #4CAF50; /* Green */
|
103 |
-
color: white;
|
104 |
-
padding: 10px 20px;
|
105 |
-
border: none;
|
106 |
-
border-radius: 5px;
|
107 |
-
cursor: pointer;
|
108 |
-
font-size: 16px;
|
109 |
-
display: block;
|
110 |
-
margin-top: 10px;
|
111 |
-
}
|
112 |
-
.analyze-button:hover {
|
113 |
-
background-color: #45a049;
|
114 |
-
}
|
115 |
-
</style>
|
116 |
-
""", unsafe_allow_html=True)
|
117 |
-
|
118 |
-
if st.button("Analyze Email", key="analyze", help="Click to analyze the email"):
|
119 |
if email_body:
|
120 |
st.session_state.result = analyze_email(email_body)
|
121 |
else:
|
|
|
4 |
import numpy as np
|
5 |
|
6 |
def analyze_email(email_body):
|
|
|
7 |
spam_pipeline = pipeline("text-classification", model="cybersectony/phishing-email-detection-distilbert_v2.4.1")
|
8 |
sentiment_model = AutoModelForSequenceClassification.from_pretrained("ISOM5240GP4/email_sentiment", num_labels=2)
|
9 |
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
10 |
|
|
|
11 |
spam_result = spam_pipeline(email_body)
|
12 |
spam_label = spam_result[0]["label"]
|
13 |
spam_confidence = spam_result[0]["score"]
|
|
|
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)
|
20 |
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
|
|
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"):
|
|
|
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:
|