Dunateo
commited on
Commit
·
95f0274
1
Parent(s):
d3c4a33
Initial Commir
Browse files- app.py +38 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
import torch
|
5 |
+
import json
|
6 |
+
|
7 |
+
def 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 label_dict[str(predicted_class)], probs[0][predicted_class].item()
|
16 |
+
|
17 |
+
if __name__ == '__main__':
|
18 |
+
model_path = "Dunateo/roberta-cwe-classifier-kelemia-v0.2"
|
19 |
+
|
20 |
+
# init the model
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
22 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
23 |
+
|
24 |
+
# get the dict file
|
25 |
+
label_dict_file = hf_hub_download(repo_id=model_path, filename="label_dict.json")
|
26 |
+
with open(label_dict_file, "r") as f:
|
27 |
+
label_dict = json.load(f)
|
28 |
+
|
29 |
+
# gradio specific to create an IHM
|
30 |
+
iface = gr.Interface(
|
31 |
+
fn=predict,
|
32 |
+
inputs=gr.Textbox(lines=5, label="Enter vulnerability description"),
|
33 |
+
outputs=[gr.Label(label="Predicted CWE"), gr.Number(label="Confidence")],
|
34 |
+
title="Vulnerability CWE Classification",
|
35 |
+
description="Enter a vulnerability description to classify it into a CWE category."
|
36 |
+
)
|
37 |
+
|
38 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
gradio
|