Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import numpy as np
|
| 3 |
+
import joblib # to save and load the trained model
|
| 4 |
+
import re
|
| 5 |
+
from urllib.parse import urlparse
|
| 6 |
+
from tld import get_tld
|
| 7 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
# Load your trained model
|
| 12 |
+
model = joblib.load('random_forest_model.pkl') # save your trained model using joblib
|
| 13 |
+
|
| 14 |
+
# Define your feature extraction functions here...
|
| 15 |
+
|
| 16 |
+
def having_ip_address(url):
|
| 17 |
+
# Your implementation
|
| 18 |
+
pass
|
| 19 |
+
|
| 20 |
+
def abnormal_url(url):
|
| 21 |
+
# Your implementation
|
| 22 |
+
pass
|
| 23 |
+
|
| 24 |
+
def count_dot(url):
|
| 25 |
+
# Your implementation
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
# Define other functions similarly...
|
| 29 |
+
|
| 30 |
+
def main(url):
|
| 31 |
+
status = []
|
| 32 |
+
status.append(having_ip_address(url))
|
| 33 |
+
status.append(abnormal_url(url))
|
| 34 |
+
status.append(count_dot(url))
|
| 35 |
+
status.append(count_www(url))
|
| 36 |
+
status.append(count_atrate(url))
|
| 37 |
+
status.append(no_of_dir(url))
|
| 38 |
+
status.append(no_of_embed(url))
|
| 39 |
+
status.append(shortening_service(url))
|
| 40 |
+
status.append(count_https(url))
|
| 41 |
+
status.append(count_http(url))
|
| 42 |
+
status.append(count_per(url))
|
| 43 |
+
status.append(count_ques(url))
|
| 44 |
+
status.append(count_hyphen(url))
|
| 45 |
+
status.append(count_equal(url))
|
| 46 |
+
status.append(url_length(url))
|
| 47 |
+
status.append(hostname_length(url))
|
| 48 |
+
status.append(suspicious_words(url))
|
| 49 |
+
status.append(digit_count(url))
|
| 50 |
+
status.append(letter_count(url))
|
| 51 |
+
status.append(fd_length(url))
|
| 52 |
+
tld = get_tld(url, fail_silently=True)
|
| 53 |
+
status.append(tld_length(tld))
|
| 54 |
+
return status
|
| 55 |
+
|
| 56 |
+
@app.route('/predict', methods=['POST'])
|
| 57 |
+
def predict():
|
| 58 |
+
data = request.json
|
| 59 |
+
url = data.get('url')
|
| 60 |
+
features = np.array(main(url)).reshape(1, -1)
|
| 61 |
+
prediction = model.predict(features)
|
| 62 |
+
|
| 63 |
+
if int(prediction[0]) == 0:
|
| 64 |
+
result = "SAFE"
|
| 65 |
+
elif int(prediction[0]) == 1:
|
| 66 |
+
result = "DEFACEMENT"
|
| 67 |
+
elif int(prediction[0]) == 2:
|
| 68 |
+
result = "PHISHING"
|
| 69 |
+
elif int(prediction[0]) == 3:
|
| 70 |
+
result = "MALWARE"
|
| 71 |
+
|
| 72 |
+
return jsonify({"prediction": result})
|
| 73 |
+
|
| 74 |
+
if __name__ == '__main__':
|
| 75 |
+
app.run(host='0.0.0.0', port=8000)
|