Marmelat commited on
Commit
e58e6ba
·
verified ·
1 Parent(s): 447e524

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Llama-3 8B Prompt Injection Detection (LoRA Fine-Tune)
3
+
4
+ This model is a fine-tuned version of **Meta-Llama-3-8B** designed to detect prompt injection attacks.
5
+ It has been trained using QLoRA (4-bit quantization) on a massive aggregation of public prompt injection datasets.
6
+
7
+ ## Model Details
8
+ - **Base Model:** meta-llama/Meta-Llama-3-8B
9
+ - **Task:** Binary Classification (SAFE / INJECTION)
10
+ - **Fine-Tuning Method:** LoRA (Low-Rank Adaptation)
11
+
12
+ ## Dataset
13
+
14
+ The model was trained on a deduplicated aggregation of multiple open-source datasets.
15
+
16
+ - **Total Unique Training Examples:** 478638
17
+
18
+ ### Data Sources
19
+ | Dataset Name | Original Rows |
20
+ |:-----------------------------------------------|----------------:|
21
+ | neuralchemy/Prompt-injection-dataset | 10674 |
22
+ | hendzh/PromptShield | 18909 |
23
+ | S-Labs/prompt-injection-dataset | 11089 |
24
+ | wambosec/prompt-injections-subtle | 839 |
25
+ | wambosec/prompt-injections | 5189 |
26
+ | beratcmn/turkish-prompt-injections | 546 |
27
+ | Antijection/prompt-injection-dataset-v1 | 5988 |
28
+ | guychuk/benign-malicious-prompt-classification | 464470 |
29
+ | m4vic/prompt-injection-dataset | 10674 |
30
+
31
+ ## Training Hyperparameters
32
+
33
+ - **Max Steps:** 500
34
+ - **Learning Rate:** 2e-4
35
+ - **Batch Size:** 4 (per device)
36
+ - **Gradient Accumulation:** 4
37
+ - **Precision:** bfloat16 (bf16)
38
+ - **LoRA Rank (r):** 16
39
+ - **LoRA Alpha:** 32
40
+ - **LoRA Dropout:** 0.05
41
+ - **Target Modules:** q_proj, k_proj, v_proj, o_proj
42
+
43
+ ## Training Results
44
+
45
+ ### Loss Curve
46
+ ![Loss Curve](loss_curve_massive.png)
47
+
48
+ ## Usage
49
+
50
+ ```python
51
+ import torch
52
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
53
+ from peft import PeftModel
54
+
55
+ model_id = "meta-llama/Meta-Llama-3-8B"
56
+ adapter_path = "path_to_saved_adapter" # e.g., llama3_injection_adapter_massive
57
+
58
+ # 1. Load Base Model
59
+ bnb_config = BitsAndBytesConfig(
60
+ load_in_4bit=True,
61
+ bnb_4bit_compute_dtype=torch.bfloat16,
62
+ bnb_4bit_use_double_quant=True,
63
+ bnb_4bit_quant_type="nf4"
64
+ )
65
+
66
+ base_model = AutoModelForCausalLM.from_pretrained(
67
+ model_id,
68
+ quantization_config=bnb_config,
69
+ device_map="auto"
70
+ )
71
+
72
+ # 2. Load Adapter
73
+ model = PeftModel.from_pretrained(base_model, adapter_path)
74
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
75
+
76
+ # 3. Inference
77
+ def predict(text):
78
+ prompt = (
79
+ f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>
80
+
81
+ "
82
+ f"Classify this prompt as SAFE or INJECTION.<|eot_id|>"
83
+ f"<|start_header_id|>user<|end_header_id|>
84
+
85
+ "
86
+ f"{text}<|eot_id|>"
87
+ f"<|start_header_id|>assistant<|end_header_id|>
88
+
89
+ "
90
+ )
91
+
92
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
93
+ outputs = model.generate(**inputs, max_new_tokens=10)
94
+ return tokenizer.decode(outputs[0], skip_special_tokens=True).split("assistant")[-1].strip()
95
+
96
+ print(predict("Write a poem about flowers."))
97
+ ```
adapter_config.json ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "alora_invocation_tokens": null,
3
+ "alpha_pattern": {},
4
+ "arrow_config": null,
5
+ "auto_mapping": null,
6
+ "base_model_name_or_path": "meta-llama/Meta-Llama-3-8B",
7
+ "bias": "none",
8
+ "corda_config": null,
9
+ "ensure_weight_tying": false,
10
+ "eva_config": null,
11
+ "exclude_modules": null,
12
+ "fan_in_fan_out": false,
13
+ "inference_mode": true,
14
+ "init_lora_weights": true,
15
+ "layer_replication": null,
16
+ "layers_pattern": null,
17
+ "layers_to_transform": null,
18
+ "loftq_config": {},
19
+ "lora_alpha": 32,
20
+ "lora_bias": false,
21
+ "lora_dropout": 0.05,
22
+ "megatron_config": null,
23
+ "megatron_core": "megatron.core",
24
+ "modules_to_save": null,
25
+ "peft_type": "LORA",
26
+ "peft_version": "0.18.1",
27
+ "qalora_group_size": 16,
28
+ "r": 16,
29
+ "rank_pattern": {},
30
+ "revision": null,
31
+ "target_modules": [
32
+ "q_proj",
33
+ "v_proj",
34
+ "k_proj",
35
+ "o_proj"
36
+ ],
37
+ "target_parameters": null,
38
+ "task_type": "CAUSAL_LM",
39
+ "trainable_token_indices": null,
40
+ "use_dora": false,
41
+ "use_qalora": false,
42
+ "use_rslora": false
43
+ }
adapter_model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:04c4e14d8c10a15daf5da55d4fab5f4616b650aa99c5f686c5c935823392138a
3
+ size 27297544
loss_curve_massive.png ADDED
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3c5cf44023714fb39b05e71e425f8d7b92805ff73f7988b083b8c87f0bf87393
3
+ size 17209961
tokenizer_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "backend": "tokenizers",
3
+ "bos_token": "<|begin_of_text|>",
4
+ "clean_up_tokenization_spaces": true,
5
+ "eos_token": "<|end_of_text|>",
6
+ "is_local": false,
7
+ "model_input_names": [
8
+ "input_ids",
9
+ "attention_mask"
10
+ ],
11
+ "model_max_length": 1000000000000000019884624838656,
12
+ "pad_token": "<|end_of_text|>",
13
+ "tokenizer_class": "TokenizersBackend"
14
+ }
training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d495d77d378b89866100df4dae6793ca6ef6adc4b788b8905461e97676554634
3
+ size 5585