Spaces:
Sleeping
Sleeping
Commit
·
34d9398
1
Parent(s):
0f56e7e
model load cbm
Browse files- app.py +1 -70
- verify_model.py +7 -0
app.py
CHANGED
@@ -23,7 +23,7 @@ def extract_features_in_parallel(urls):
|
|
23 |
# Load the CatBoost model for inference
|
24 |
def predict_with_catboost(features_df, model_path):
|
25 |
model = CatBoostClassifier()
|
26 |
-
model.load_model(model_path)
|
27 |
predictions = model.predict(features_df)
|
28 |
return predictions
|
29 |
|
@@ -67,72 +67,3 @@ async def index():
|
|
67 |
|
68 |
if __name__ == "__main__":
|
69 |
app.run(debug=False,host="0.0.0.0",port=7860)
|
70 |
-
|
71 |
-
'''
|
72 |
-
import asyncio
|
73 |
-
import pandas as pd
|
74 |
-
from concurrent.futures import ThreadPoolExecutor
|
75 |
-
from flask import Flask, request, render_template
|
76 |
-
from catboost import CatBoostClassifier
|
77 |
-
from url_process import extract_url_features, predict_urls # Import necessary functions
|
78 |
-
|
79 |
-
# Flask App Setup
|
80 |
-
app = Flask(__name__)
|
81 |
-
|
82 |
-
# Batch Processing: Ensures URLs are processed in manageable chunks
|
83 |
-
def process_urls_in_batches(urls, batch_size=10):
|
84 |
-
for i in range(0, len(urls), batch_size):
|
85 |
-
yield urls[i:i + batch_size]
|
86 |
-
|
87 |
-
# Async function for non-blocking DNS lookups and HTTP requests
|
88 |
-
async def async_extract_features(url):
|
89 |
-
features = await asyncio.to_thread(extract_url_features, url)
|
90 |
-
return features
|
91 |
-
|
92 |
-
# ThreadPoolExecutor for CPU-bound tasks like feature extraction
|
93 |
-
def extract_features_in_parallel(urls):
|
94 |
-
with ThreadPoolExecutor(max_workers=5) as executor:
|
95 |
-
return list(executor.map(extract_url_features, urls))
|
96 |
-
|
97 |
-
# Load the CatBoost model for inference
|
98 |
-
def predict_with_catboost(features_df, model_path):
|
99 |
-
model = CatBoostClassifier()
|
100 |
-
model.load_model(model_path)
|
101 |
-
predictions = model.predict(features_df)
|
102 |
-
return predictions
|
103 |
-
|
104 |
-
@app.route("/", methods=["GET", "POST"])
|
105 |
-
async def index():
|
106 |
-
result = None
|
107 |
-
url_features = None
|
108 |
-
|
109 |
-
if request.method == "POST":
|
110 |
-
# Get the URL input from the form
|
111 |
-
url = request.form["url"]
|
112 |
-
|
113 |
-
try:
|
114 |
-
# Asynchronously process the URL features
|
115 |
-
features = await async_extract_features(url)
|
116 |
-
|
117 |
-
# Convert the features to a DataFrame for further processing
|
118 |
-
features_df = pd.DataFrame([features])
|
119 |
-
|
120 |
-
# Perform prediction using the CatBoost model
|
121 |
-
model_path = "F:\\pyro guard\\model\\catboost_model.bin" # Specify your CatBoost model path
|
122 |
-
predictions = predict_with_catboost(features_df, model_path)
|
123 |
-
|
124 |
-
# Determine if the URL is malicious or legitimate
|
125 |
-
if predictions[0] == 1:
|
126 |
-
result = "Malicious"
|
127 |
-
else:
|
128 |
-
result = "Legitimate"
|
129 |
-
|
130 |
-
except Exception as e:
|
131 |
-
result = f"Error processing URL: {str(e)}"
|
132 |
-
|
133 |
-
return render_template("index.html", result=result, url_features=url_features)
|
134 |
-
|
135 |
-
if __name__ == "__main__":
|
136 |
-
app.run(debug=True)
|
137 |
-
|
138 |
-
'''
|
|
|
23 |
# Load the CatBoost model for inference
|
24 |
def predict_with_catboost(features_df, model_path):
|
25 |
model = CatBoostClassifier()
|
26 |
+
model.load_model(model_path, format='cbm') # Specify the format as 'cbm'
|
27 |
predictions = model.predict(features_df)
|
28 |
return predictions
|
29 |
|
|
|
67 |
|
68 |
if __name__ == "__main__":
|
69 |
app.run(debug=False,host="0.0.0.0",port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verify_model.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from catboost import CatBoostClassifier
|
2 |
+
import os
|
3 |
+
|
4 |
+
model_path = os.path.join(os.getcwd(), "catboost_model.bin")
|
5 |
+
model = CatBoostClassifier()
|
6 |
+
model.load_model(model_path, format='cbm')
|
7 |
+
print("Model loaded successfully")
|