Spaces:
Sleeping
Sleeping
Commit
·
5c58259
1
Parent(s):
34d9398
error check
Browse files- app.py +29 -7
- test_model.py +26 -0
app.py
CHANGED
@@ -22,10 +22,19 @@ def extract_features_in_parallel(urls):
|
|
22 |
|
23 |
# Load the CatBoost model for inference
|
24 |
def predict_with_catboost(features_df, model_path):
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Flask App Setup
|
31 |
app = Flask(__name__)
|
@@ -46,9 +55,22 @@ async def index():
|
|
46 |
# Convert the features to DataFrame (in case you need to do further processing)
|
47 |
features_df = pd.DataFrame([features])
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
predictions = predict_with_catboost(features_df, model_path)
|
53 |
|
54 |
# Determine if the URL is malicious or legitimate
|
|
|
22 |
|
23 |
# Load the CatBoost model for inference
|
24 |
def predict_with_catboost(features_df, model_path):
|
25 |
+
try:
|
26 |
+
model = CatBoostClassifier()
|
27 |
+
# Try loading without format specification first
|
28 |
+
try:
|
29 |
+
model.load_model(model_path)
|
30 |
+
except:
|
31 |
+
# If that fails, try with explicit format
|
32 |
+
model.load_model(model_path, format='binary')
|
33 |
+
predictions = model.predict(features_df)
|
34 |
+
return predictions
|
35 |
+
except Exception as e:
|
36 |
+
print(f"Error loading model: {str(e)}")
|
37 |
+
raise
|
38 |
|
39 |
# Flask App Setup
|
40 |
app = Flask(__name__)
|
|
|
55 |
# Convert the features to DataFrame (in case you need to do further processing)
|
56 |
features_df = pd.DataFrame([features])
|
57 |
|
58 |
+
# Try multiple possible model locations
|
59 |
+
possible_paths = [
|
60 |
+
os.path.join(os.getcwd(), "catboost_model.bin"),
|
61 |
+
"/app/catboost_model.bin", # Docker container path
|
62 |
+
"catboost_model.bin"
|
63 |
+
]
|
64 |
+
|
65 |
+
model_path = None
|
66 |
+
for path in possible_paths:
|
67 |
+
if os.path.exists(path):
|
68 |
+
model_path = path
|
69 |
+
break
|
70 |
+
|
71 |
+
if model_path is None:
|
72 |
+
raise FileNotFoundError("Model file not found in any expected location")
|
73 |
+
|
74 |
predictions = predict_with_catboost(features_df, model_path)
|
75 |
|
76 |
# Determine if the URL is malicious or legitimate
|
test_model.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from catboost import CatBoostClassifier
|
2 |
+
import os
|
3 |
+
|
4 |
+
def test_model_loading():
|
5 |
+
possible_paths = [
|
6 |
+
os.path.join(os.getcwd(), "catboost_model.bin"),
|
7 |
+
"/app/catboost_model.bin",
|
8 |
+
"catboost_model.bin"
|
9 |
+
]
|
10 |
+
|
11 |
+
for path in possible_paths:
|
12 |
+
if os.path.exists(path):
|
13 |
+
print(f"Found model at: {path}")
|
14 |
+
model = CatBoostClassifier()
|
15 |
+
try:
|
16 |
+
model.load_model(path)
|
17 |
+
print(f"Successfully loaded model from {path}")
|
18 |
+
return True
|
19 |
+
except Exception as e:
|
20 |
+
print(f"Failed to load from {path}: {str(e)}")
|
21 |
+
|
22 |
+
print("Model not found in any location")
|
23 |
+
return False
|
24 |
+
|
25 |
+
if __name__ == "__main__":
|
26 |
+
test_model_loading()
|