retr04error commited on
Commit
61fcfcf
1 Parent(s): 1a49556
Files changed (2) hide show
  1. app.py +76 -4
  2. requirements.txt +68 -0
app.py CHANGED
@@ -1,7 +1,79 @@
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch.nn.functional import softmax
3
+ from transformers import GPT2Tokenizer
4
+ import os
5
+ import requests
6
+ import tempfile
7
  import gradio as gr
8
 
9
+ # Model loading and prediction function
10
+ def check_vulnerabilities(solidity_file_path, model_directory='models/', device='cuda'):
11
+ device = 'cuda' if torch.cuda.is_available() and device == 'cuda' else 'cpu'
12
+ tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
13
+ tokenizer.pad_token = tokenizer.eos_token
14
 
15
+ with open(solidity_file_path, 'r', encoding='utf-8') as f:
16
+ test_code = f.read()
17
+
18
+ X = tokenizer([test_code], padding=True, truncation=True, return_tensors="pt")
19
+ dic_name = {
20
+ 'reentrancy': 'Reentrancy Vulnerability',
21
+ 'timestamp': 'Timestamp Dependency Vulnerability',
22
+ 'delegatecall': 'Delegate Call Vulnerability',
23
+ 'integeroverflow': 'Integer Overflow Vulnerability',
24
+ }
25
+
26
+ dic01 = {0: 'The vulnerability does not exist', 1: 'The vulnerability exists'}
27
+ results = {}
28
+
29
+ for model_name in os.listdir(model_directory):
30
+ vulnerability_name = dic_name[model_name.split('_')[0]]
31
+ cp_file = os.path.join(model_directory, model_name)
32
+ model = torch.load(cp_file, map_location=device)
33
+ X = X.to(device)
34
+ model.to(device)
35
+ model.eval()
36
+ pred = softmax(model(**X).logits, dim=1)[0]
37
+ results[vulnerability_name] = {
38
+ 'result': dic01[int(pred.argmax(0))],
39
+ 'confidence': pred.max().item()
40
+ }
41
+
42
+ return results
43
+
44
+ # Gradio interface function
45
+ def check_vulnerabilities_interface(solidity_file_url, file_id):
46
+ model_directory = 'models/'
47
+ device = 'cuda'
48
+
49
+ try:
50
+ response = requests.get(solidity_file_url)
51
+ response.raise_for_status()
52
+
53
+ with tempfile.NamedTemporaryFile(delete=False, suffix=f"_{file_id}.sol") as temp_file:
54
+ temp_file.write(response.content)
55
+ temp_file.flush()
56
+ results = check_vulnerabilities(temp_file.name, model_directory, device)
57
+ os.remove(temp_file.name)
58
+ return results
59
+
60
+ except requests.exceptions.RequestException as e:
61
+ return {'error': f'Error fetching file: {e}'}
62
+ except Exception as e:
63
+ return {'error': str(e)}
64
+
65
+ # Set up the Gradio interface
66
+ interface = gr.Interface(
67
+ fn=check_vulnerabilities_interface,
68
+ inputs=[
69
+ gr.inputs.Textbox(label="Solidity File URL", placeholder="Enter URL here..."),
70
+ gr.inputs.Textbox(label="File ID", placeholder="Enter file ID here...")
71
+ ],
72
+ outputs="json",
73
+ title="Solidity Vulnerability Checker",
74
+ description="Enter the URL of a Solidity file and a file ID to check for vulnerabilities."
75
+ )
76
+
77
+ # Run the interface
78
+ if __name__ == "__main__":
79
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ appdirs==1.4.4
2
+ asttokens==2.4.1
3
+ certifi==2024.2.2
4
+ charset-normalizer==3.3.2
5
+ comm==0.2.2
6
+ debugpy==1.8.1
7
+ decorator==5.1.1
8
+ exceptiongroup==1.2.0
9
+ executing==2.0.1
10
+ filelock==3.13.4
11
+ fsspec==2024.3.1
12
+ huggingface-hub==0.22.2
13
+ idna==3.7
14
+ ipykernel==6.29.4
15
+ ipython==8.23.0
16
+ jedi==0.19.1
17
+ Jinja2==3.1.3
18
+ jupyter_client==8.6.1
19
+ jupyter_core==5.7.2
20
+ Mako==1.3.3
21
+ MarkupSafe==2.1.5
22
+ matplotlib-inline==0.1.6
23
+ mpmath==1.3.0
24
+ nest-asyncio==1.6.0
25
+ networkx==3.3
26
+ numpy==1.26.4
27
+ nvidia-cublas-cu12==12.1.3.1
28
+ nvidia-cuda-cupti-cu12==12.1.105
29
+ nvidia-cuda-nvrtc-cu12==12.1.105
30
+ nvidia-cuda-runtime-cu12==12.1.105
31
+ nvidia-cudnn-cu12==8.9.2.26
32
+ nvidia-cufft-cu12==11.0.2.54
33
+ nvidia-curand-cu12==10.3.2.106
34
+ nvidia-cusolver-cu12==11.4.5.107
35
+ nvidia-cusparse-cu12==12.1.0.106
36
+ nvidia-nccl-cu12==2.19.3
37
+ nvidia-nvjitlink-cu12==12.4.127
38
+ nvidia-nvtx-cu12==12.1.105
39
+ packaging==24.0
40
+ parso==0.8.4
41
+ pexpect==4.9.0
42
+ platformdirs==4.2.0
43
+ prompt-toolkit==3.0.43
44
+ psutil==5.9.8
45
+ ptyprocess==0.7.0
46
+ pure-eval==0.2.2
47
+ pycuda==2024.1
48
+ Pygments==2.17.2
49
+ python-dateutil==2.9.0.post0
50
+ pytools==2024.1.1
51
+ PyYAML==6.0.1
52
+ pyzmq==25.1.2
53
+ regex==2023.12.25
54
+ requests==2.31.0
55
+ safetensors==0.4.2
56
+ six==1.16.0
57
+ stack-data==0.6.3
58
+ sympy==1.12
59
+ tokenizers==0.15.2
60
+ torch==2.2.2
61
+ tornado==6.4
62
+ tqdm==4.66.2
63
+ traitlets==5.14.2
64
+ transformers==4.39.3
65
+ triton==2.2.0
66
+ typing_extensions==4.11.0
67
+ urllib3==2.2.1
68
+ wcwidth==0.2.13