Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ def analyze_email(email_body):
|
|
| 10 |
sentiment_model = AutoModelForSequenceClassification.from_pretrained("ISOM5240GP4/email_sentiment", num_labels=2)
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
| 12 |
|
|
|
|
| 13 |
if not email_body.strip():
|
| 14 |
return "error", "Email body is empty. Please provide an email to analyze."
|
| 15 |
|
|
@@ -24,11 +25,15 @@ def analyze_email(email_body):
|
|
| 24 |
else:
|
| 25 |
# Step 2: Analyze sentiment for non-spam emails
|
| 26 |
inputs = tokenizer(email_body, padding=True, truncation=True, return_tensors='pt')
|
|
|
|
|
|
|
| 27 |
outputs = sentiment_model(**inputs)
|
| 28 |
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 29 |
predictions = predictions.cpu().detach().numpy()
|
| 30 |
sentiment_index = np.argmax(predictions)
|
| 31 |
sentiment_confidence = predictions[0][sentiment_index]
|
|
|
|
|
|
|
| 32 |
sentiment = "Positive" if sentiment_index == 1 else "Negative"
|
| 33 |
|
| 34 |
if sentiment == "Positive":
|
|
|
|
| 10 |
sentiment_model = AutoModelForSequenceClassification.from_pretrained("ISOM5240GP4/email_sentiment", num_labels=2)
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
| 12 |
|
| 13 |
+
# this is to handle if user does not input anything
|
| 14 |
if not email_body.strip():
|
| 15 |
return "error", "Email body is empty. Please provide an email to analyze."
|
| 16 |
|
|
|
|
| 25 |
else:
|
| 26 |
# Step 2: Analyze sentiment for non-spam emails
|
| 27 |
inputs = tokenizer(email_body, padding=True, truncation=True, return_tensors='pt')
|
| 28 |
+
# Pass the tokenized inputs to the sentiment model
|
| 29 |
+
# **inputs unpacks the dictionary of inputs to the model
|
| 30 |
outputs = sentiment_model(**inputs)
|
| 31 |
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 32 |
predictions = predictions.cpu().detach().numpy()
|
| 33 |
sentiment_index = np.argmax(predictions)
|
| 34 |
sentiment_confidence = predictions[0][sentiment_index]
|
| 35 |
+
# Convert numeric prediction to human-readable sentiment
|
| 36 |
+
# If index is 1, it's Positive, otherwise Negative
|
| 37 |
sentiment = "Positive" if sentiment_index == 1 else "Negative"
|
| 38 |
|
| 39 |
if sentiment == "Positive":
|