Spaces:
Sleeping
Sleeping
add files
Browse files- __pycache__/utils.cpython-38.pyc +0 -0
- app.py +31 -0
- models/model.pkl +3 -0
- requirements.txt +3 -0
- static/style.css +77 -0
- templates/index.html +70 -0
- utils.py +30 -0
__pycache__/utils.cpython-38.pyc
ADDED
Binary file (760 Bytes). View file
|
|
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
from flask import Flask, render_template, request, jsonify
|
3 |
+
from utils import model_predict
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
@app.route("/", methods=["GET"])
|
8 |
+
def index():
|
9 |
+
"""
|
10 |
+
Serve the main HTML page.
|
11 |
+
"""
|
12 |
+
return render_template("index.html")
|
13 |
+
|
14 |
+
@app.route("/predict", methods=["POST"])
|
15 |
+
def predict():
|
16 |
+
"""
|
17 |
+
Handle POST requests for email classification.
|
18 |
+
"""
|
19 |
+
data = request.json
|
20 |
+
email_content = data.get("email", "").strip()
|
21 |
+
|
22 |
+
if not email_content:
|
23 |
+
return jsonify({"error": "Please enter some text to classify."}), 400
|
24 |
+
|
25 |
+
prediction = model_predict(email_content)
|
26 |
+
result = "SPAM" if prediction == 1 else "NOT SPAM"
|
27 |
+
|
28 |
+
return jsonify({"result": result})
|
29 |
+
|
30 |
+
if __name__ == "__main__":
|
31 |
+
app.run(debug=True)
|
models/model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e267bcbe204f9d95acebc0f8680e73f1c7d390b54d70162835b811c3691a36e7
|
3 |
+
size 1560707
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
flask==2.3.2
|
2 |
+
scikit-learn
|
3 |
+
numpy
|
static/style.css
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* General Styling */
|
2 |
+
body {
|
3 |
+
font-family: Arial, sans-serif;
|
4 |
+
background-color: #f4f4f9;
|
5 |
+
margin: 0;
|
6 |
+
padding: 0;
|
7 |
+
display: flex;
|
8 |
+
justify-content: center;
|
9 |
+
align-items: center;
|
10 |
+
height: 100vh;
|
11 |
+
}
|
12 |
+
|
13 |
+
.container {
|
14 |
+
background-color: #ffffff;
|
15 |
+
border-radius: 10px;
|
16 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
17 |
+
padding: 30px 50px;
|
18 |
+
width: 100%;
|
19 |
+
max-width: 500px;
|
20 |
+
text-align: center;
|
21 |
+
}
|
22 |
+
|
23 |
+
h1 {
|
24 |
+
color: #333333;
|
25 |
+
margin-bottom: 20px;
|
26 |
+
}
|
27 |
+
|
28 |
+
textarea {
|
29 |
+
width: 100%;
|
30 |
+
height: 150px;
|
31 |
+
padding: 10px;
|
32 |
+
font-size: 16px;
|
33 |
+
border: 1px solid #cccccc;
|
34 |
+
border-radius: 5px;
|
35 |
+
margin-bottom: 20px;
|
36 |
+
resize: none;
|
37 |
+
}
|
38 |
+
|
39 |
+
button {
|
40 |
+
background-color: #007bff;
|
41 |
+
color: #ffffff;
|
42 |
+
border: none;
|
43 |
+
padding: 10px 20px;
|
44 |
+
font-size: 16px;
|
45 |
+
border-radius: 5px;
|
46 |
+
cursor: pointer;
|
47 |
+
transition: background-color 0.3s ease;
|
48 |
+
}
|
49 |
+
|
50 |
+
button:hover {
|
51 |
+
background-color: #0056b3;
|
52 |
+
}
|
53 |
+
|
54 |
+
a.reset-link {
|
55 |
+
margin-left: 10px;
|
56 |
+
color: #007bff;
|
57 |
+
text-decoration: none;
|
58 |
+
font-size: 16px;
|
59 |
+
}
|
60 |
+
|
61 |
+
a.reset-link:hover {
|
62 |
+
text-decoration: underline;
|
63 |
+
}
|
64 |
+
|
65 |
+
.result {
|
66 |
+
margin-top: 20px;
|
67 |
+
font-size: 18px;
|
68 |
+
font-weight: bold;
|
69 |
+
}
|
70 |
+
|
71 |
+
.spam {
|
72 |
+
color: #ff5252; /* Red for spam */
|
73 |
+
}
|
74 |
+
|
75 |
+
.not-spam {
|
76 |
+
color: #28a745; /* Green for not spam */
|
77 |
+
}
|
templates/index.html
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!-- templates/index.html -->
|
2 |
+
<!DOCTYPE html>
|
3 |
+
<html lang="en">
|
4 |
+
<head>
|
5 |
+
<meta charset="UTF-8">
|
6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
7 |
+
<title>Email Spam Classifier</title>
|
8 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
9 |
+
</head>
|
10 |
+
<body>
|
11 |
+
<div class="container">
|
12 |
+
<h1>Email Spam Classifier</h1>
|
13 |
+
|
14 |
+
<!-- Form for entering email -->
|
15 |
+
<textarea id="emailInput" rows="6" cols="50" placeholder="Enter email content here..."></textarea>
|
16 |
+
<div>
|
17 |
+
<button id="checkButton">Check</button>
|
18 |
+
<button id="resetButton">Reset</button>
|
19 |
+
</div>
|
20 |
+
|
21 |
+
<!-- Display error message or prediction -->
|
22 |
+
<p id="outputMessage" class="message"></p>
|
23 |
+
</div>
|
24 |
+
|
25 |
+
<script>
|
26 |
+
// JavaScript to handle form submission and reset functionality
|
27 |
+
document.getElementById("checkButton").addEventListener("click", async () => {
|
28 |
+
const emailContent = document.getElementById("emailInput").value.trim();
|
29 |
+
const outputMessage = document.getElementById("outputMessage");
|
30 |
+
|
31 |
+
// Clear previous results
|
32 |
+
outputMessage.textContent = "";
|
33 |
+
|
34 |
+
if (!emailContent) {
|
35 |
+
outputMessage.textContent = "Please enter some text to classify.";
|
36 |
+
outputMessage.style.color = "red";
|
37 |
+
return;
|
38 |
+
}
|
39 |
+
|
40 |
+
try {
|
41 |
+
// Send email content to the Flask backend for prediction
|
42 |
+
const response = await fetch("/predict", {
|
43 |
+
method: "POST",
|
44 |
+
headers: { "Content-Type": "application/json" },
|
45 |
+
body: JSON.stringify({ email: emailContent }),
|
46 |
+
});
|
47 |
+
|
48 |
+
const result = await response.json();
|
49 |
+
|
50 |
+
if (response.ok) {
|
51 |
+
outputMessage.textContent = `This email is ${result.result}.`;
|
52 |
+
outputMessage.style.color = result.result === "SPAM" ? "red" : "green";
|
53 |
+
} else {
|
54 |
+
outputMessage.textContent = result.error || "An error occurred.";
|
55 |
+
outputMessage.style.color = "red";
|
56 |
+
}
|
57 |
+
} catch (error) {
|
58 |
+
outputMessage.textContent = "An error occurred while processing your request.";
|
59 |
+
outputMessage.style.color = "red";
|
60 |
+
}
|
61 |
+
});
|
62 |
+
|
63 |
+
// Reset button functionality
|
64 |
+
document.getElementById("resetButton").addEventListener("click", () => {
|
65 |
+
document.getElementById("emailInput").value = "";
|
66 |
+
document.getElementById("outputMessage").textContent = "";
|
67 |
+
});
|
68 |
+
</script>
|
69 |
+
</body>
|
70 |
+
</html>
|
utils.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# utils.py (Helper Functions)
|
2 |
+
import pickle
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
def load_model():
|
6 |
+
"""
|
7 |
+
Loads the trained model from file.
|
8 |
+
"""
|
9 |
+
with open("models/model.pkl", "rb") as file:
|
10 |
+
model = pickle.load(file) # Use pickle to load the model
|
11 |
+
return model
|
12 |
+
|
13 |
+
def model_predict(email):
|
14 |
+
"""
|
15 |
+
Predicts using the loaded model.
|
16 |
+
"""
|
17 |
+
model = load_model() # Load the model before predicting
|
18 |
+
prediction = model.predict([email]) # Use the predict method to make predictions
|
19 |
+
|
20 |
+
# If the email is spam, prediction should be 1, otherwise 0
|
21 |
+
# Convert the prediction to 1 or -1 as specified
|
22 |
+
prediction = 1 if prediction[0] == 1 else -1
|
23 |
+
|
24 |
+
return prediction
|
25 |
+
|
26 |
+
with open("models/model.pkl", "rb") as file:
|
27 |
+
model = pickle.load(file)
|
28 |
+
|
29 |
+
print("Model loaded successfully!")
|
30 |
+
|