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