CoolFace
Apppublic

Dunateo/Kelemia_Vulnerability_CWE_Classification

sourceHugging Facemitupdated 2y agoView on Hugging Face
1likes
app.py44 linesDownload Raw Back to root
1import gradio as gr2from transformers import AutoTokenizer, AutoModelForSequenceClassification3from huggingface_hub import hf_hub_download4import torch5import json6 7def predict(text):8    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)9 10    with torch.no_grad():11        outputs = model(**inputs)12 13    probs = torch.nn.functional.softmax(outputs.logits, dim=-1)14    predicted_class = torch.argmax(probs, dim=-1).item()15    return id2label[predicted_class], probs[0][predicted_class].item()16 17if __name__ == '__main__':18    model_path = "Dunateo/roberta-cwe-classifier-kelemia-v0.2"19 20    # init the model21    tokenizer = AutoTokenizer.from_pretrained(model_path)22    model = AutoModelForSequenceClassification.from_pretrained(model_path)23 24    # get the dict file25    label_dict_file = hf_hub_download(repo_id=model_path, filename="label_dict.json")26 27 28    with open(label_dict_file, "r") as f:29        content = f.read()30    label_dict = json.loads(content)31 32    global id2label33    id2label = {v: k for k, v in label_dict.items()}34 35    # gradio specific to create an IHM36    iface = gr.Interface(37        fn=predict,38        inputs=gr.Textbox(lines=5, label="Enter vulnerability description"),39        outputs=[gr.Label(label="Predicted CWE"), gr.Number(label="Confidence")],40        title="Vulnerability CWE Classification",41        description="Enter a vulnerability description to classify it into a CWE category."42    )43 44    iface.launch()