Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
return {
|
| 23 |
-
"Risk Skoru": optimized,
|
| 24 |
"Kategori": category,
|
| 25 |
-
"
|
| 26 |
}
|
| 27 |
|
| 28 |
-
|
| 29 |
-
fn=
|
| 30 |
inputs=[
|
| 31 |
-
gr.
|
| 32 |
-
gr.Text(label="CWE ID
|
| 33 |
-
gr.Textbox(label="
|
|
|
|
| 34 |
],
|
| 35 |
outputs="json",
|
| 36 |
-
title="
|
| 37 |
-
description="CVE
|
| 38 |
)
|
| 39 |
|
| 40 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
# Modeli yükle
|
| 6 |
+
with open("model/xgboost_optimized_v2.pkl", "rb") as f:
|
| 7 |
+
model_pkg = pickle.load(f)
|
| 8 |
+
|
| 9 |
+
xgb_improved = model_pkg["base_models"]["xgboost_improved"][0]
|
| 10 |
+
smart_align = model_pkg["enhancement_functions"]["smart_feature_alignment"]
|
| 11 |
+
ensemble_fn = model_pkg["enhancement_functions"]["ensemble_predict"]
|
| 12 |
+
unknown_boost_fn = model_pkg["enhancement_functions"]["unknown_category_boost"]
|
| 13 |
+
dynamic_adjust_fn = model_pkg["enhancement_functions"]["dynamic_risk_adjustment"]
|
| 14 |
+
confidence_fn = model_pkg["enhancement_functions"]["calculate_confidence"]
|
| 15 |
+
|
| 16 |
+
preprocessors = model_pkg["preprocessors"]
|
| 17 |
+
scaler = preprocessors["scaler"]
|
| 18 |
+
encoders = preprocessors["encoders"]
|
| 19 |
+
bert_extractor = preprocessors["bert_extractor"]
|
| 20 |
+
label_encoder = preprocessors["label_encoder"]
|
| 21 |
+
feature_names = preprocessors["scaler"].mean_.shape[0]
|
| 22 |
+
|
| 23 |
+
def predict(cve_id, cwe_id, description, cvss_score):
|
| 24 |
+
# Dummy features for now – Hugging Face Spaces does not allow GPU by default
|
| 25 |
+
numeric = np.zeros((1, feature_names))
|
| 26 |
+
bert_feats = np.zeros((1, 768)) # Dummy
|
| 27 |
+
x_input = np.hstack([numeric, bert_feats])
|
| 28 |
+
|
| 29 |
+
xgb_pred = xgb_improved.predict(x_input)[0]
|
| 30 |
+
ensemble_pred = ensemble_fn(cvss_score, xgb_pred)
|
| 31 |
+
final_pred = unknown_boost_fn(ensemble_pred, unknown_features=2, cvss_score=cvss_score)
|
| 32 |
+
optimized = dynamic_adjust_fn(final_pred, cvss_score, cwe_id, len(description))
|
| 33 |
+
confidence = confidence_fn(2, cvss_score)
|
| 34 |
+
|
| 35 |
+
category = "Düşük" if optimized <= 3 else "Orta" if optimized <= 7 else "Yüksek"
|
| 36 |
|
| 37 |
return {
|
| 38 |
+
"Tahmini Risk Skoru": round(optimized, 2),
|
| 39 |
"Kategori": category,
|
| 40 |
+
"Model Güveni": f"%{round(confidence*100, 1)}"
|
| 41 |
}
|
| 42 |
|
| 43 |
+
interface = gr.Interface(
|
| 44 |
+
fn=predict,
|
| 45 |
inputs=[
|
| 46 |
+
gr.Text(label="CVE ID"),
|
| 47 |
+
gr.Text(label="CWE ID"),
|
| 48 |
+
gr.Textbox(label="Açıklama"),
|
| 49 |
+
gr.Slider(minimum=0.0, maximum=10.0, step=0.1, label="CVSS Skoru")
|
| 50 |
],
|
| 51 |
outputs="json",
|
| 52 |
+
title="Risk Skoru Tahmin Modeli (XGBoost Optimized)",
|
| 53 |
+
description="CVE tanımlarına göre tahmini risk skoru ve kategori"
|
| 54 |
)
|
| 55 |
|
| 56 |
+
interface.launch()
|