Spaces:
Build error
Build error
Commit
·
944cd11
1
Parent(s):
1d415c1
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
import transformers
|
|
|
3 |
import torch
|
4 |
import json
|
5 |
|
6 |
# load all models
|
|
|
|
|
|
|
7 |
pragformer = transformers.AutoModel.from_pretrained("Pragformer/PragFormer", trust_remote_code=True)
|
8 |
pragformer_private = transformers.AutoModel.from_pretrained("Pragformer/PragFormer_private", trust_remote_code=True)
|
9 |
pragformer_reduction = transformers.AutoModel.from_pretrained("Pragformer/PragFormer_reduction", trust_remote_code=True)
|
@@ -12,72 +16,87 @@ pragformer_reduction = transformers.AutoModel.from_pretrained("Pragformer/PragFo
|
|
12 |
#Event Listeners
|
13 |
with_omp_str = 'Should contain a parallel work-sharing loop construct'
|
14 |
without_omp_str = 'Should not contain a parallel work-sharing loop construct'
|
|
|
15 |
|
16 |
tokenizer = transformers.AutoTokenizer.from_pretrained('NTUYG/DeepSCC-RoBERTa')
|
17 |
|
|
|
18 |
with open('c_data.json', 'r') as f:
|
19 |
data = json.load(f)
|
20 |
|
21 |
def fill_code(code_pth):
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
|
27 |
def predict(code_txt):
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
y_hat = torch.argmax(pred).item()
|
38 |
-
return with_omp_str if y_hat==1 else without_omp_str, torch.nn.Softmax(dim=1)(pred).squeeze()[y_hat].item()
|
39 |
|
|
|
|
|
40 |
|
41 |
|
42 |
def is_private(code_txt):
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
def is_reduction(code_txt
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
# Define GUI
|
83 |
|
@@ -98,8 +117,10 @@ with gr.Blocks() as pragformer_gui:
|
|
98 |
sample_btn = gr.Button("Sample")
|
99 |
|
100 |
pragma = gr.Textbox(label="Original parallelization classification (if any)")
|
|
|
|
|
|
|
101 |
|
102 |
-
code_in = gr.Textbox(lines=5, label="Write some code and see if it should contain a parallel work-sharing loop construct")
|
103 |
submit_btn = gr.Button("Submit")
|
104 |
with gr.Column():
|
105 |
gr.Markdown("## Results")
|
@@ -112,6 +133,8 @@ with gr.Blocks() as pragformer_gui:
|
|
112 |
private = gr.Textbox(label="Data-sharing attribute clause- private", visible=False)
|
113 |
reduction = gr.Textbox(label="Data-sharing attribute clause- reduction", visible=False)
|
114 |
|
|
|
|
|
115 |
submit_btn.click(fn=predict, inputs=code_in, outputs=[label_out, confidence_out])
|
116 |
submit_btn.click(fn=is_private, inputs=code_in, outputs=private)
|
117 |
submit_btn.click(fn=is_reduction, inputs=code_in, outputs=reduction)
|
|
|
1 |
import gradio as gr
|
2 |
import transformers
|
3 |
+
from simpletransformers.classification import ClassificationModel, ClassificationArgs
|
4 |
import torch
|
5 |
import json
|
6 |
|
7 |
# load all models
|
8 |
+
deep_scc_model_args = ClassificationArgs(num_train_epochs=10,max_seq_length=300,use_multiprocessing=False)
|
9 |
+
deep_scc_model = ClassificationModel("roberta", "NTUYG/DeepSCC-RoBERTa", num_labels=19, args=deep_scc_model_args, use_cuda=False)
|
10 |
+
|
11 |
pragformer = transformers.AutoModel.from_pretrained("Pragformer/PragFormer", trust_remote_code=True)
|
12 |
pragformer_private = transformers.AutoModel.from_pretrained("Pragformer/PragFormer_private", trust_remote_code=True)
|
13 |
pragformer_reduction = transformers.AutoModel.from_pretrained("Pragformer/PragFormer_reduction", trust_remote_code=True)
|
|
|
16 |
#Event Listeners
|
17 |
with_omp_str = 'Should contain a parallel work-sharing loop construct'
|
18 |
without_omp_str = 'Should not contain a parallel work-sharing loop construct'
|
19 |
+
name_file = ['bash', 'c', 'c#', 'c++','css', 'haskell', 'java', 'javascript', 'lua', 'objective-c', 'perl', 'php', 'python','r','ruby', 'scala', 'sql', 'swift', 'vb.net']
|
20 |
|
21 |
tokenizer = transformers.AutoTokenizer.from_pretrained('NTUYG/DeepSCC-RoBERTa')
|
22 |
|
23 |
+
|
24 |
with open('c_data.json', 'r') as f:
|
25 |
data = json.load(f)
|
26 |
|
27 |
def fill_code(code_pth):
|
28 |
+
pragma = data[code_pth]['pragma']
|
29 |
+
code = data[code_pth]['code']
|
30 |
+
return 'None' if len(pragma)==0 else pragma, code
|
31 |
|
32 |
|
33 |
def predict(code_txt):
|
34 |
+
code = code_txt.lstrip().rstrip()
|
35 |
+
tokenized = tokenizer.batch_encode_plus(
|
36 |
+
[code],
|
37 |
+
max_length = 150,
|
38 |
+
pad_to_max_length = True,
|
39 |
+
truncation = True
|
40 |
+
)
|
41 |
+
pred = pragformer(torch.tensor(tokenized['input_ids']), torch.tensor(tokenized['attention_mask']))
|
|
|
|
|
|
|
42 |
|
43 |
+
y_hat = torch.argmax(pred).item()
|
44 |
+
return with_omp_str if y_hat==1 else without_omp_str, torch.nn.Softmax(dim=1)(pred).squeeze()[y_hat].item()
|
45 |
|
46 |
|
47 |
def is_private(code_txt):
|
48 |
+
if predict(code_txt)[0] == without_omp_str:
|
49 |
+
return gr.update(visible=False)
|
50 |
+
|
51 |
+
code = code_txt.lstrip().rstrip()
|
52 |
+
tokenized = tokenizer.batch_encode_plus(
|
53 |
+
[code],
|
54 |
+
max_length = 150,
|
55 |
+
pad_to_max_length = True,
|
56 |
+
truncation = True
|
57 |
+
)
|
58 |
+
pred = pragformer_private(torch.tensor(tokenized['input_ids']), torch.tensor(tokenized['attention_mask']))
|
59 |
+
|
60 |
+
y_hat = torch.argmax(pred).item()
|
61 |
+
# if y_hat == 0:
|
62 |
+
# return gr.update(visible=False)
|
63 |
+
# else:
|
64 |
+
return gr.update(value=f"Should {'not' if y_hat==0 else ''} contain private with confidence: {torch.nn.Softmax(dim=1)(pred).squeeze()[y_hat].item()}", visible=True)
|
65 |
+
|
66 |
+
|
67 |
+
def is_reduction(code_txt):
|
68 |
+
if predict(code_txt)[0] == without_omp_str:
|
69 |
+
return gr.update(visible=False)
|
70 |
+
|
71 |
+
code = code_txt.lstrip().rstrip()
|
72 |
+
tokenized = tokenizer.batch_encode_plus(
|
73 |
+
[code],
|
74 |
+
max_length = 150,
|
75 |
+
pad_to_max_length = True,
|
76 |
+
truncation = True
|
77 |
+
)
|
78 |
+
pred = pragformer_reduction(torch.tensor(tokenized['input_ids']), torch.tensor(tokenized['attention_mask']))
|
79 |
+
|
80 |
+
y_hat = torch.argmax(pred).item()
|
81 |
+
# if y_hat == 0:
|
82 |
+
# return gr.update(visible=False)
|
83 |
+
# else:
|
84 |
+
return gr.update(value=f"Should {'not' if y_hat==0 else ''} contain reduction with confidence: {torch.nn.Softmax(dim=1)(pred).squeeze()[y_hat].item()}", visible=True)
|
85 |
+
|
86 |
+
|
87 |
+
def lang_predict(code_txt):
|
88 |
+
res = {}
|
89 |
+
code = code_txt.replace('\n',' ').replace('\r',' ')
|
90 |
+
predictions, raw_outputs = deep_scc_model.predict([code])
|
91 |
+
# preds = [name_file[predictions[i]] for i in range(5)]
|
92 |
+
softmax_vals = torch.nn.Softmax(dim=1)(torch.tensor(raw_outputs))
|
93 |
+
top5 = torch.topk(softmax_vals, 5)
|
94 |
+
|
95 |
+
for lang_idx, conf in zip(top5.indices.flatten(), top5.values.flatten()):
|
96 |
+
res[name_file[lang_idx.item()]] = conf.item()
|
97 |
+
|
98 |
+
return '\n'.join([f" {'V ' if k=='c' else 'X'}{k}: {v}" for k,v in res.items()])
|
99 |
+
|
100 |
|
101 |
# Define GUI
|
102 |
|
|
|
117 |
sample_btn = gr.Button("Sample")
|
118 |
|
119 |
pragma = gr.Textbox(label="Original parallelization classification (if any)")
|
120 |
+
with gr.Row():
|
121 |
+
code_in = gr.Textbox(lines=5, label="Write some C code and see if it should contain a parallel work-sharing loop construct")
|
122 |
+
lang_pred = gr.Textbox(lines=5, label="DeepScc programming language prediction")
|
123 |
|
|
|
124 |
submit_btn = gr.Button("Submit")
|
125 |
with gr.Column():
|
126 |
gr.Markdown("## Results")
|
|
|
133 |
private = gr.Textbox(label="Data-sharing attribute clause- private", visible=False)
|
134 |
reduction = gr.Textbox(label="Data-sharing attribute clause- reduction", visible=False)
|
135 |
|
136 |
+
code_in.change(fn=lang_predict, inputs=code_in, outputs=lang_pred)
|
137 |
+
|
138 |
submit_btn.click(fn=predict, inputs=code_in, outputs=[label_out, confidence_out])
|
139 |
submit_btn.click(fn=is_private, inputs=code_in, outputs=private)
|
140 |
submit_btn.click(fn=is_reduction, inputs=code_in, outputs=reduction)
|