V2LinkDetection / app.py
ZealPyae's picture
Create app.py
80f46f9 verified
raw
history blame
2.07 kB
from flask import Flask, request, jsonify
import numpy as np
import joblib # to save and load the trained model
import re
from urllib.parse import urlparse
from tld import get_tld
from sklearn.ensemble import RandomForestClassifier
app = Flask(__name__)
# Load your trained model
model = joblib.load('random_forest_model.pkl') # save your trained model using joblib
# Define your feature extraction functions here...
def having_ip_address(url):
# Your implementation
pass
def abnormal_url(url):
# Your implementation
pass
def count_dot(url):
# Your implementation
pass
# Define other functions similarly...
def main(url):
status = []
status.append(having_ip_address(url))
status.append(abnormal_url(url))
status.append(count_dot(url))
status.append(count_www(url))
status.append(count_atrate(url))
status.append(no_of_dir(url))
status.append(no_of_embed(url))
status.append(shortening_service(url))
status.append(count_https(url))
status.append(count_http(url))
status.append(count_per(url))
status.append(count_ques(url))
status.append(count_hyphen(url))
status.append(count_equal(url))
status.append(url_length(url))
status.append(hostname_length(url))
status.append(suspicious_words(url))
status.append(digit_count(url))
status.append(letter_count(url))
status.append(fd_length(url))
tld = get_tld(url, fail_silently=True)
status.append(tld_length(tld))
return status
@app.route('/predict', methods=['POST'])
def predict():
data = request.json
url = data.get('url')
features = np.array(main(url)).reshape(1, -1)
prediction = model.predict(features)
if int(prediction[0]) == 0:
result = "SAFE"
elif int(prediction[0]) == 1:
result = "DEFACEMENT"
elif int(prediction[0]) == 2:
result = "PHISHING"
elif int(prediction[0]) == 3:
result = "MALWARE"
return jsonify({"prediction": result})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)