Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
7 |
|
8 |
-
def
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
11 |
sorted_results = sorted(results, key=lambda x: x["score"], reverse=True)
|
12 |
-
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
17 |
outputs=gr.Label(num_top_classes=5),
|
18 |
-
title="CWE
|
19 |
-
description="
|
|
|
|
|
|
|
|
|
20 |
)
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Load the Hugging Face model for text classification
|
5 |
+
classifier = pipeline(
|
6 |
+
task="text-classification",
|
7 |
+
model="CIRCL/cwe-parent-vulnerability-classification-roberta-base",
|
8 |
+
return_all_scores=True
|
9 |
+
)
|
10 |
|
11 |
+
def predict_cwe(commit_message: str):
|
12 |
+
"""
|
13 |
+
Predict CWE(s) from a commit message using the model.
|
14 |
+
"""
|
15 |
+
results = classifier(commit_message)[0]
|
16 |
+
# Sort the results by score descending
|
17 |
sorted_results = sorted(results, key=lambda x: x["score"], reverse=True)
|
18 |
+
# Return top 5 predictions as a dictionary
|
19 |
+
return {item["label"]: round(float(item["score"]), 4) for item in sorted_results[:5]}
|
20 |
|
21 |
+
# Build the Gradio interface
|
22 |
+
demo = gr.Interface(
|
23 |
+
fn=predict_cwe,
|
24 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter your commit message here..."),
|
25 |
outputs=gr.Label(num_top_classes=5),
|
26 |
+
title="CWE Prediction from Commit Message",
|
27 |
+
description="Type a Git commit message and get the most likely CWE classes predicted by the model.",
|
28 |
+
examples=[
|
29 |
+
["Fixed buffer overflow in input parsing"],
|
30 |
+
["SQL injection possible in user login endpoint"]
|
31 |
+
]
|
32 |
)
|
33 |
|
34 |
+
if __name__ == "__main__":
|
35 |
+
demo.launch()
|
36 |
+
|