Alvlt commited on
Commit
2128c90
·
1 Parent(s): c7264f7

fix bug categories

Browse files
Files changed (2) hide show
  1. models/categories.json +0 -0
  2. src/api/main.py +10 -6
models/categories.json ADDED
The diff for this file is too large to render. See raw diff
 
src/api/main.py CHANGED
@@ -1,13 +1,13 @@
1
  import os
2
  import sys
3
  sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
4
-
5
  import joblib
6
  import pandas as pd
7
  import requests
8
  from dotenv import load_dotenv
9
  from fastapi import FastAPI, HTTPException
10
- import numpy as np
11
  from src.data.features import extract_features
12
  from src.api.schemas import CVERequest, PredictionResponse
13
 
@@ -18,6 +18,13 @@ api_key = os.getenv("NVD_API_KEY")
18
  MODEL_PATH = os.path.join(os.path.dirname(__file__), "..", "..", "models", "model_final_xgboost_v2.pkl")
19
  model = joblib.load(MODEL_PATH)
20
 
 
 
 
 
 
 
 
21
  # We define our columns and the Threshold
22
  CATEGORICAL_COLUMNS = [
23
  "attack_vector", "attack_complexity", "privileges_required",
@@ -65,8 +72,6 @@ def predict(request: CVERequest):
65
  raise HTTPException(status_code=502, detail=f"Erreur NVD: {e}")
66
 
67
  features = extract_features(cve_raw)
68
- # We don't need to include the history
69
- # we take the description directly from the time of the CVE's release
70
  features["description_length_reconstructed"] = features["description_length"]
71
 
72
  feature_columns = model.feature_names_in_
@@ -74,8 +79,7 @@ def predict(request: CVERequest):
74
 
75
  for col in CATEGORICAL_COLUMNS:
76
  if col in X.columns:
77
- # We use NaN if unknown features
78
- X[col] = X[col].where(X[col].notna(), np.nan).astype("category")
79
 
80
  proba = model.predict_proba(X)[:, 1][0]
81
 
 
1
  import os
2
  import sys
3
  sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
4
+ import json
5
  import joblib
6
  import pandas as pd
7
  import requests
8
  from dotenv import load_dotenv
9
  from fastapi import FastAPI, HTTPException
10
+ import numpy as np
11
  from src.data.features import extract_features
12
  from src.api.schemas import CVERequest, PredictionResponse
13
 
 
18
  MODEL_PATH = os.path.join(os.path.dirname(__file__), "..", "..", "models", "model_final_xgboost_v2.pkl")
19
  model = joblib.load(MODEL_PATH)
20
 
21
+ # The categories created during training are specified
22
+ # Otherwise, the model fails to compare the "sellers" variable and crashes
23
+ CATEGORIES_PATH = os.path.join(os.path.dirname(__file__), "..", "..", "models", "categories.json")
24
+ with open(CATEGORIES_PATH) as f:
25
+ CATEGORIES = json.load(f)
26
+
27
+
28
  # We define our columns and the Threshold
29
  CATEGORICAL_COLUMNS = [
30
  "attack_vector", "attack_complexity", "privileges_required",
 
72
  raise HTTPException(status_code=502, detail=f"Erreur NVD: {e}")
73
 
74
  features = extract_features(cve_raw)
 
 
75
  features["description_length_reconstructed"] = features["description_length"]
76
 
77
  feature_columns = model.feature_names_in_
 
79
 
80
  for col in CATEGORICAL_COLUMNS:
81
  if col in X.columns:
82
+ X[col] = pd.Categorical(X[col], categories=CATEGORIES[col])
 
83
 
84
  proba = model.predict_proba(X)[:, 1][0]
85