Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -67,6 +67,7 @@ def preprocess(text):
|
|
| 67 |
text = ' '.join([lemmatizer.lemmatize(word) for word in text.split() if word not in stop_words]) # lemmatizācija
|
| 68 |
return text
|
| 69 |
|
|
|
|
| 70 |
# Classification function (single model)
|
| 71 |
def classify_email_single_model(text, model_name):
|
| 72 |
text = preprocess(text)
|
|
@@ -74,15 +75,22 @@ def classify_email_single_model(text, model_name):
|
|
| 74 |
with torch.no_grad():
|
| 75 |
outputs = models[model_name](**inputs)
|
| 76 |
prediction = torch.argmax(outputs.logits, dim=1).item()
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
|
|
|
|
| 79 |
# Classification function (all models together)
|
| 80 |
def classify_email(text):
|
| 81 |
votes = {"Safe": 0, "Spam": 0, "Phishing": 0}
|
|
|
|
| 82 |
|
| 83 |
for model_name in model_names:
|
| 84 |
-
|
|
|
|
| 85 |
votes[vote] += 1
|
|
|
|
| 86 |
|
| 87 |
|
| 88 |
response = ""
|
|
@@ -92,8 +100,14 @@ def classify_email(text):
|
|
| 92 |
if i != 3:
|
| 93 |
response += f"{label}: {vote_count} {vote_or_votes}, "
|
| 94 |
else:
|
| 95 |
-
response += f"{label}: {vote_count} {vote_or_votes}"
|
| 96 |
i += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
return response
|
| 99 |
|
|
|
|
| 67 |
text = ' '.join([lemmatizer.lemmatize(word) for word in text.split() if word not in stop_words]) # lemmatizācija
|
| 68 |
return text
|
| 69 |
|
| 70 |
+
|
| 71 |
# Classification function (single model)
|
| 72 |
def classify_email_single_model(text, model_name):
|
| 73 |
text = preprocess(text)
|
|
|
|
| 75 |
with torch.no_grad():
|
| 76 |
outputs = models[model_name](**inputs)
|
| 77 |
prediction = torch.argmax(outputs.logits, dim=1).item()
|
| 78 |
+
probs = F.softmax(logits, dim=1)
|
| 79 |
+
probs_percent = probs.cpu().numpy() * 100
|
| 80 |
+
response = {"prediction": labels[prediction], "probabilities": probs_percent}
|
| 81 |
+
return response
|
| 82 |
|
| 83 |
+
|
| 84 |
# Classification function (all models together)
|
| 85 |
def classify_email(text):
|
| 86 |
votes = {"Safe": 0, "Spam": 0, "Phishing": 0}
|
| 87 |
+
probabilities = {}
|
| 88 |
|
| 89 |
for model_name in model_names:
|
| 90 |
+
response = classify_email_single_model(text, model_name)
|
| 91 |
+
vote = response['prediction']
|
| 92 |
votes[vote] += 1
|
| 93 |
+
probabilities[model_name] = response['probabilities']
|
| 94 |
|
| 95 |
|
| 96 |
response = ""
|
|
|
|
| 100 |
if i != 3:
|
| 101 |
response += f"{label}: {vote_count} {vote_or_votes}, "
|
| 102 |
else:
|
| 103 |
+
response += f"{label}: {vote_count} {vote_or_votes}\n"
|
| 104 |
i += 1
|
| 105 |
+
|
| 106 |
+
for model_name in model_names:
|
| 107 |
+
response += f"{model_name}: "
|
| 108 |
+
for j, prob in enumerate(probabilities[model_name]):
|
| 109 |
+
response += f"{num_to_label[j]}: {prob:.2f}%"
|
| 110 |
+
response += "\n"
|
| 111 |
|
| 112 |
return response
|
| 113 |
|