-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
67 lines (51 loc) · 1.67 KB
/
app.py
File metadata and controls
67 lines (51 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask, request, render_template
import joblib
import numpy as np
import os
app = Flask(__name__)
# ----------------------------
# Load model safely (Render compatible)
# ----------------------------
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_PATH = os.path.join(BASE_DIR, "best_phishing_model.pkl")
model = joblib.load(MODEL_PATH)
# ----------------------------
# Feature Extraction (TEMP placeholder)
# ----------------------------
def extract_features(url_string):
n_features = model.n_features_in_
return np.random.rand(1, n_features)
# ----------------------------
# Routes
# ----------------------------
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
try:
url = request.form['url']
features = extract_features(url)
prediction = model.predict(features)[0]
probability = model.predict_proba(features)[0][1]
result = "PHISHING" if prediction == 1 else "LEGITIMATE"
color = "red" if prediction == 1 else "green"
return render_template(
'index.html',
prediction_text=f'Result: {result}',
prob_text=f'Confidence: {probability*100:.2f}%',
result_color=color
)
except Exception as e:
return render_template(
'index.html',
prediction_text="Error occurred",
prob_text=str(e),
result_color="black"
)
# ----------------------------
# Render requires this
# ----------------------------
if __name__ == "__main__":
port = int(os.environ.get("PORT", 10000))
app.run(host="0.0.0.0", port=port)