-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
41,501 additions
and
30 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import random | ||
|
||
import pandas as pd | ||
|
||
random.seed(123456) | ||
|
||
def create_doc(js): | ||
malicious_doc = "javascript perform malicious actions to trick users, steal data from users, \ | ||
or otherwise cause harm." | ||
benign_doc = "javascript perform normal, non-harmful actions" | ||
|
||
label = js["label"] | ||
# choose randomly between malicious and benign doc | ||
if random.random() < 0.5: | ||
doc = malicious_doc | ||
new_label = 1 if label == 1 else 0 | ||
else: | ||
doc = benign_doc | ||
new_label = 1 if label == 0 else 0 | ||
|
||
# js["label"] = new_label | ||
# js["doc"] = doc | ||
|
||
# return js | ||
return doc, new_label | ||
|
||
def modify_dataset(file_path, type:str): | ||
with open (file_path, "r") as f: | ||
# convert jsonl file to pandas | ||
df = pd.read_json(f, lines=True) | ||
|
||
df[["doc", "label"]] = df.apply(create_doc, axis=1, result_type="expand") | ||
df["idx"] = type + "_" + df.index.astype(str) | ||
new_path = file_path.replace(".jsonl", "_new.jsonl") | ||
with open(new_path, "w") as f: | ||
f.write(df.to_json(orient='records', lines=True, force_ascii=False)) | ||
|
||
|
||
if __name__ == "__main__": | ||
print("modifying test set") | ||
modify_dataset("data/exp/test_set.jsonl", "test") | ||
print("modifying valid set") | ||
modify_dataset("data/exp/valid_set.jsonl", "valid") | ||
print("modifying train set") | ||
modify_dataset("data/exp/train_set.jsonl", "train") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
python lib/codebert-bimodal/run_classifier.py \ | ||
--model_type roberta \ | ||
--do_train \ | ||
--do_eval \ | ||
--eval_all_checkpoints \ | ||
--train_file train_set_new.jsonl \ | ||
--dev_file valid_set_new.jsonl \ | ||
--max_seq_length 200 \ | ||
--per_gpu_train_batch_size 16 \ | ||
--per_gpu_eval_batch_size 16 \ | ||
--learning_rate 1e-5 \ | ||
--num_train_epochs 20 \ | ||
--gradient_accumulation_steps 1 \ | ||
--warmup_steps 1000 \ | ||
--evaluate_during_training \ | ||
--data_dir ./data/exp \ | ||
--output_dir ./models \ | ||
--encoder_name_or_path microsoft/codebert-base \ | ||
--seed 123456 2>&1 | tee train.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.