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()