ISOM5240GP4 commited on
Commit
9739fd6
·
verified ·
1 Parent(s): c70917b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -1,10 +1,15 @@
1
  import streamlit as st
2
- from transformers import pipeline
 
 
3
 
4
  def main():
5
- # Load the models
6
  spam_pipeline = pipeline("text-classification", model="cybersectony/phishing-email-detection-distilbert_v2.4.1")
7
- sentiment_pipeline = pipeline("text-classification", model="ISOM5240GP4/email_sentiment")
 
 
 
8
 
9
  # Title and description
10
  st.title("Email Analysis Tool")
@@ -21,21 +26,27 @@ def main():
21
  spam_label = spam_result[0]["label"]
22
  spam_confidence = spam_result[0]["score"]
23
 
24
- # If it's spam, display result and stop
25
- if spam_label == "POSITIVE": # Assuming "POSITIVE" means spam/phishing (check model docs)
26
  st.write(f"This is a spam email (Confidence: {spam_confidence:.2f}). No follow-up needed.")
27
  else:
28
- # Step 2: If not spam, analyze sentiment
29
- sentiment_result = sentiment_pipeline(email_body)
30
- sentiment_label = sentiment_result[0]["label"]
31
- sentiment_confidence = sentiment_result[0]["score"]
 
 
 
 
 
 
32
 
33
- if sentiment_label == "POSITIVE":
34
  st.write(f"This email is not spam (Confidence: {spam_confidence:.2f}).")
35
- st.write(f"Sentiment: Positive (Confidence: {sentiment_confidence:.2f}). No follow-up needed.")
36
- else: # Assuming "NEGATIVE" for negative sentiment
37
  st.write(f"This email is not spam (Confidence: {spam_confidence:.2f}).")
38
- st.write(f"Sentiment: Negative (Confidence: {sentiment_confidence:.2f}).")
39
  st.write("**This email needs follow-up as it is not spam and has negative sentiment.**")
40
  else:
41
  st.write("Please enter an email body to analyze.")
 
1
  import streamlit as st
2
+ from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
3
+ import torch
4
+ import numpy as np
5
 
6
  def main():
7
+ # Load the spam detection pipeline
8
  spam_pipeline = pipeline("text-classification", model="cybersectony/phishing-email-detection-distilbert_v2.4.1")
9
+
10
+ # Load the sentiment model and tokenizer directly
11
+ sentiment_model = AutoModelForSequenceClassification.from_pretrained("ISOM5240GP4/email_sentiment", num_labels=2)
12
+ tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
13
 
14
  # Title and description
15
  st.title("Email Analysis Tool")
 
26
  spam_label = spam_result[0]["label"]
27
  spam_confidence = spam_result[0]["score"]
28
 
29
+ # Assuming "POSITIVE" means spam/phishing (adjust if incorrect)
30
+ if spam_label == "POSITIVE":
31
  st.write(f"This is a spam email (Confidence: {spam_confidence:.2f}). No follow-up needed.")
32
  else:
33
+ # Step 2: Analyze sentiment for non-spam emails
34
+ inputs = tokenizer(email_body, padding=True, truncation=True, return_tensors='pt')
35
+ outputs = sentiment_model(**inputs)
36
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
37
+ predictions = predictions.cpu().detach().numpy()
38
+ sentiment_index = np.argmax(predictions)
39
+ sentiment_confidence = predictions[0][sentiment_index]
40
+
41
+ # Map index to sentiment (1 = positive, 0 = negative)
42
+ sentiment = "Positive" if sentiment_index == 1 else "Negative"
43
 
44
+ if sentiment == "Positive":
45
  st.write(f"This email is not spam (Confidence: {spam_confidence:.2f}).")
46
+ st.write(f"Sentiment: {sentiment} (Confidence: {sentiment_confidence:.2f}). No follow-up needed.")
47
+ else: # Negative sentiment
48
  st.write(f"This email is not spam (Confidence: {spam_confidence:.2f}).")
49
+ st.write(f"Sentiment: {sentiment} (Confidence: {sentiment_confidence:.2f}).")
50
  st.write("**This email needs follow-up as it is not spam and has negative sentiment.**")
51
  else:
52
  st.write("Please enter an email body to analyze.")