-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
56 lines (47 loc) · 1.56 KB
/
app.py
File metadata and controls
56 lines (47 loc) · 1.56 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
from flask import Flask, request, jsonify
import pandas as pd
import joblib
from urllib.parse import urlparse
import re
app = Flask(__name__)
# 모델과 feature 리스트 불러오기
model = joblib.load("url_model_new.pkl")
features = [
'hostname_length', 'count_dir', 'count-www',
'url_length', 'fd_length', 'count-', 'count.',
'tld_length', 'count-digits', 'count='
]
def extract_features(url):
def fd_length(url):
try:
return len(urlparse(url).path.split('/')[1])
except:
return 0
def get_tld_length(url):
try:
return len(url.split('.')[-1])
except:
return -1
return {
'hostname_length': len(urlparse(url).netloc),
'count_dir': urlparse(url).path.count('/'),
'count-www': url.count('www'),
'url_length': len(url),
'fd_length': fd_length(url),
'count-': url.count('-'),
'count.': url.count('.'),
'tld_length': get_tld_length(url),
'count-digits': sum(c.isdigit() for c in url),
'count=': url.count('=')
}
@app.route('/api/v1/ai/verify', methods=['POST'])
def verify():
data = request.get_json()
url = data.get("url", "")
if not url:
return jsonify({"error": "No URL provided"}), 400
feature_data = pd.DataFrame([extract_features(url)])[features]
prediction = model.predict(feature_data)[0]
return jsonify({"result": int(prediction)})
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=True)