Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import json
|
| 3 |
-
from transformers import pipeline
|
| 4 |
import torch
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
torch.manual_seed(42)
|
| 9 |
-
random.seed(42)
|
| 10 |
-
np.random.seed(42)
|
| 11 |
-
|
| 12 |
-
torch.use_deterministic_algorithms(True)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
top_k=None
|
| 19 |
-
)
|
| 20 |
-
classifier.model.eval()
|
| 21 |
|
| 22 |
-
# Load child-to-parent mapping
|
| 23 |
with open("child_to_parent_mapping.json", "r") as f:
|
| 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 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
["Improved input validation to prevent XSS"],
|
| 66 |
-
]
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
import json
|
| 4 |
+
import base64
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
model_path = "CIRCL/cwe-parent-vulnerability-classification-roberta-base"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 10 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
| 12 |
with open("child_to_parent_mapping.json", "r") as f:
|
| 13 |
+
child_to_ancestor = json.load(f)
|
| 14 |
+
|
| 15 |
+
with open(f"{model_path}/config.json", "r") as f:
|
| 16 |
+
config = json.load(f)
|
| 17 |
+
id2label = config["id2label"]
|
| 18 |
+
|
| 19 |
+
# Fonction d'extraction pour simuler une entrée formatée
|
| 20 |
+
def extract_commit_text_hg_style(input_text):
|
| 21 |
+
# Ici, on pourrait simuler un vrai patch ou commit. Pour l’instant, on prend l’entrée brute.
|
| 22 |
+
return input_text.strip()
|
| 23 |
+
|
| 24 |
+
# Fonction Gradio de prédiction
|
| 25 |
+
def predict_ancestors(input_text):
|
| 26 |
+
text = extract_commit_text_hg_style(input_text)
|
| 27 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding="max_length", max_length=512)
|
| 28 |
+
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
outputs = model(**inputs)
|
| 31 |
+
logits = outputs.logits
|
| 32 |
+
probs = torch.softmax(logits, dim=-1)
|
| 33 |
+
|
| 34 |
+
topk = torch.topk(probs, k=5)
|
| 35 |
+
top_ids = topk.indices[0].tolist()
|
| 36 |
+
top_scores = topk.values[0].tolist()
|
| 37 |
+
|
| 38 |
+
results = []
|
| 39 |
+
for i, (idx, score) in enumerate(zip(top_ids, top_scores), 1):
|
| 40 |
+
cwe_child = id2label[str(idx)]
|
| 41 |
+
ancestor = child_to_ancestor.get(cwe_child, "N/A")
|
| 42 |
+
results.append(f"{i}. CWE-{cwe_child} (ancestor: CWE-{ancestor}) - {score:.4f}")
|
| 43 |
+
|
| 44 |
+
return results
|
| 45 |
+
|
| 46 |
+
# Interface Gradio
|
| 47 |
+
gr.Interface(
|
| 48 |
+
fn=predict_ancestors,
|
| 49 |
+
inputs=gr.Textbox(label="Commit message or patch (e.g., 'hg')"),
|
| 50 |
+
outputs=gr.outputs.Textbox(label="Top 5 Predicted CWE Ancestors"),
|
| 51 |
+
title="CWE Ancestor Predictor",
|
| 52 |
+
description="Entrez un message de commit ou un patch. Le modèle prédit les 5 CWE enfants les plus probables et affiche leurs ancêtres."
|
| 53 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|