Yehia3A commited on
Commit
e08a210
·
1 Parent(s): d2c6e36

Upload folder using huggingface_hub

Browse files
README.md CHANGED
@@ -1,140 +1,202 @@
1
  ---
2
- license: apache-2.0
3
- language:
4
- - en
5
- base_model:
6
- - facebook/opt-1.3b
7
- tags:
8
- - text-generation
9
- - peft
10
- - aws
11
- - lora
12
- - security
13
- - iam
14
- - fine-tuned
15
  ---
16
- This is a fine-tuned version of the facebook/opt-1.3b model designed to analyze and rewrite risky AWS IAM policies into more secure, specific policies based on the Principle of Least Privilege.
17
-
18
- The model takes a potentially risky IAM policy (often with wildcards like "*" or "s3:*") as input and generates a secure version with specific permissions, resources, and optional conditions. This model can be used as a component in a larger security scanning or remediation pipeline.
19
-
20
- Model Details
21
- Base Model: facebook/opt-1.3b
22
-
23
- Task: Text Generation (text-generation)
24
-
25
- Fine-tuning Method: Parameter-Efficient Fine-Tuning (PEFT) using LoRA (Low-Rank Adaptation)
26
-
27
- Libraries: The model was trained using transformers, datasets, peft, and accelerate
28
-
29
- Training Data
30
- The model was fine-tuned on a custom dataset of 100000 JSON pairs, with the input being a risky policy and the output being a securely rewritten version. The dataset was generated programmatically within a Kaggle Notebook and contained a mix of policies with specific actions and wildcards for both actions and resources.
31
-
32
- How to Use 💻
33
- To use this model, you must first load the base model, then attach the PEFT adapter weights.
34
 
35
- Dependencies:
36
- Install the necessary libraries:
37
 
38
- Bash
39
 
40
- pip install transformers torch peft accelerate
41
- Inference Code:
42
- The following Python script demonstrates how to load the model and perform a rewrite on a sample policy.
43
 
44
- Python
45
 
46
- import json
47
- import torch
48
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
49
- from peft import PeftModel, LoraConfig, TaskType
50
 
51
- # Set device
52
- device = 0 if torch.cuda.is_available() else -1
53
 
54
- # Load base model and tokenizer
55
- base_model_id = "facebook/opt-1.3b"
56
- checkpoint_path = "Yehia3A/secure-policy-rewriter"
57
 
58
- print("Loading model...")
59
- tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)
60
- model = AutoModelForCausalLM.from_pretrained(
61
- base_model_id,
62
- torch_dtype=torch.float16,
63
- trust_remote_code=True
64
- )
65
 
66
- # Load LoRA adapter
67
- lora_config = LoraConfig(
68
- task_type=TaskType.CAUSAL_LM,
69
- r=16,
70
- lora_alpha=32,
71
- target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
72
- inference_mode=True
73
- )
74
- model = PeftModel.from_pretrained(model, checkpoint_path, config=lora_config)
75
- model.eval()
76
 
77
- # Create a text generation pipeline
78
- llm = pipeline(
79
- "text-generation",
80
- model=model,
81
- tokenizer=tokenizer,
82
- device=device,
83
- pad_token_id=tokenizer.eos_token_id
84
- )
85
 
86
- def rewrite_policy(policy_json: dict) -> str:
87
- # Use a few-shot prompt to guide the model
88
- prompt = f"""Rewrite risky IAM policies to be secure. Replace wildcards with specific permissions and add conditions.
89
 
90
- Input: {json.dumps(policy_json, indent=2)}
91
 
92
- Output:"""
93
-
94
- result = llm(prompt, max_new_tokens=300, temperature=0.05, do_sample=True, pad_token_id=tokenizer.eos_token_id)
95
- result_text = result[0]["generated_text"].strip()
96
-
97
- # Simple JSON extraction from the output
98
- try:
99
- start_index = result_text.find("{")
100
- end_index = result_text.rfind("}") + 1
101
- if start_index != -1 and end_index != -1:
102
- json_str = result_text[start_index:end_index]
103
- return json.dumps(json.loads(json_str), indent=2)
104
- except:
105
- return "{}"
106
- return "{}"
107
 
 
108
 
109
- # Example risky policy
110
- risky_policy = {
111
- "Version": "2012-10-17",
112
- "Statement": [{
113
- "Effect": "Allow",
114
- "Action": "*",
115
- "Resource": "*"
116
- }]
117
- }
118
 
119
- # Rewrite the policy
120
- rewritten_policy = rewrite_policy(risky_policy)
121
- print("Rewritten Secure Policy:")
122
- print(rewritten_policy)
123
-
124
- Files
125
- This repository contains the necessary files to run the fine-tuned model:
126
-
127
- adapter_config.json: Configuration for the PEFT adapter.
128
-
129
- adapter_model.safetensors: The fine-tuned weights of the PEFT adapter.
130
-
131
- tokenizer_config.json: The tokenizer configuration.
132
-
133
- tokenizer.json: The tokenizer model file.
134
-
135
- special_tokens_map.json: A mapping of special tokens.
136
-
137
- merges.txt: A vocabulary file for the tokenizer.
138
-
139
- Disclaimers
140
- This model is a proof-of-concept for educational and research purposes. It is not intended for use in production environments without further validation, testing, and security checks. It was trained on a small, synthetic dataset and may not generalize well to all real-world scenarios.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ base_model: facebook/opt-1.3b
3
+ library_name: peft
 
 
 
 
 
 
 
 
 
 
 
4
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ # Model Card for Model ID
 
7
 
8
+ <!-- Provide a quick summary of what the model is/does. -->
9
 
 
 
 
10
 
 
11
 
12
+ ## Model Details
 
 
 
13
 
14
+ ### Model Description
 
15
 
16
+ <!-- Provide a longer summary of what this model is. -->
 
 
17
 
 
 
 
 
 
 
 
18
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ - **Developed by:** [More Information Needed]
21
+ - **Funded by [optional]:** [More Information Needed]
22
+ - **Shared by [optional]:** [More Information Needed]
23
+ - **Model type:** [More Information Needed]
24
+ - **Language(s) (NLP):** [More Information Needed]
25
+ - **License:** [More Information Needed]
26
+ - **Finetuned from model [optional]:** [More Information Needed]
 
27
 
28
+ ### Model Sources [optional]
 
 
29
 
30
+ <!-- Provide the basic links for the model. -->
31
 
32
+ - **Repository:** [More Information Needed]
33
+ - **Paper [optional]:** [More Information Needed]
34
+ - **Demo [optional]:** [More Information Needed]
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ ## Uses
37
 
38
+ <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
 
 
 
 
 
 
 
 
39
 
40
+ ### Direct Use
41
+
42
+ <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
43
+
44
+ [More Information Needed]
45
+
46
+ ### Downstream Use [optional]
47
+
48
+ <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
49
+
50
+ [More Information Needed]
51
+
52
+ ### Out-of-Scope Use
53
+
54
+ <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
55
+
56
+ [More Information Needed]
57
+
58
+ ## Bias, Risks, and Limitations
59
+
60
+ <!-- This section is meant to convey both technical and sociotechnical limitations. -->
61
+
62
+ [More Information Needed]
63
+
64
+ ### Recommendations
65
+
66
+ <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
67
+
68
+ Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
69
+
70
+ ## How to Get Started with the Model
71
+
72
+ Use the code below to get started with the model.
73
+
74
+ [More Information Needed]
75
+
76
+ ## Training Details
77
+
78
+ ### Training Data
79
+
80
+ <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
81
+
82
+ [More Information Needed]
83
+
84
+ ### Training Procedure
85
+
86
+ <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
87
+
88
+ #### Preprocessing [optional]
89
+
90
+ [More Information Needed]
91
+
92
+
93
+ #### Training Hyperparameters
94
+
95
+ - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
96
+
97
+ #### Speeds, Sizes, Times [optional]
98
+
99
+ <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
100
+
101
+ [More Information Needed]
102
+
103
+ ## Evaluation
104
+
105
+ <!-- This section describes the evaluation protocols and provides the results. -->
106
+
107
+ ### Testing Data, Factors & Metrics
108
+
109
+ #### Testing Data
110
+
111
+ <!-- This should link to a Dataset Card if possible. -->
112
+
113
+ [More Information Needed]
114
+
115
+ #### Factors
116
+
117
+ <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
118
+
119
+ [More Information Needed]
120
+
121
+ #### Metrics
122
+
123
+ <!-- These are the evaluation metrics being used, ideally with a description of why. -->
124
+
125
+ [More Information Needed]
126
+
127
+ ### Results
128
+
129
+ [More Information Needed]
130
+
131
+ #### Summary
132
+
133
+
134
+
135
+ ## Model Examination [optional]
136
+
137
+ <!-- Relevant interpretability work for the model goes here -->
138
+
139
+ [More Information Needed]
140
+
141
+ ## Environmental Impact
142
+
143
+ <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
144
+
145
+ Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
146
+
147
+ - **Hardware Type:** [More Information Needed]
148
+ - **Hours used:** [More Information Needed]
149
+ - **Cloud Provider:** [More Information Needed]
150
+ - **Compute Region:** [More Information Needed]
151
+ - **Carbon Emitted:** [More Information Needed]
152
+
153
+ ## Technical Specifications [optional]
154
+
155
+ ### Model Architecture and Objective
156
+
157
+ [More Information Needed]
158
+
159
+ ### Compute Infrastructure
160
+
161
+ [More Information Needed]
162
+
163
+ #### Hardware
164
+
165
+ [More Information Needed]
166
+
167
+ #### Software
168
+
169
+ [More Information Needed]
170
+
171
+ ## Citation [optional]
172
+
173
+ <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
174
+
175
+ **BibTeX:**
176
+
177
+ [More Information Needed]
178
+
179
+ **APA:**
180
+
181
+ [More Information Needed]
182
+
183
+ ## Glossary [optional]
184
+
185
+ <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
186
+
187
+ [More Information Needed]
188
+
189
+ ## More Information [optional]
190
+
191
+ [More Information Needed]
192
+
193
+ ## Model Card Authors [optional]
194
+
195
+ [More Information Needed]
196
+
197
+ ## Model Card Contact
198
+
199
+ [More Information Needed]
200
+ ### Framework versions
201
+
202
+ - PEFT 0.15.2
adapter_config.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "alpha_pattern": {},
3
+ "auto_mapping": null,
4
+ "base_model_name_or_path": "facebook/opt-1.3b",
5
+ "bias": "none",
6
+ "corda_config": null,
7
+ "eva_config": null,
8
+ "exclude_modules": null,
9
+ "fan_in_fan_out": false,
10
+ "inference_mode": true,
11
+ "init_lora_weights": true,
12
+ "layer_replication": null,
13
+ "layers_pattern": null,
14
+ "layers_to_transform": null,
15
+ "loftq_config": {},
16
+ "lora_alpha": 32,
17
+ "lora_bias": false,
18
+ "lora_dropout": 0.05,
19
+ "megatron_config": null,
20
+ "megatron_core": "megatron.core",
21
+ "modules_to_save": null,
22
+ "peft_type": "LORA",
23
+ "r": 8,
24
+ "rank_pattern": {},
25
+ "revision": null,
26
+ "target_modules": [
27
+ "q_proj",
28
+ "v_proj",
29
+ "o_proj",
30
+ "k_proj"
31
+ ],
32
+ "task_type": "CAUSAL_LM",
33
+ "trainable_token_indices": null,
34
+ "use_dora": false,
35
+ "use_rslora": false
36
+ }
adapter_model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:15fd86ad174ed787a7e33e06b68764b9b120a542f3278a5207c99bae58a99709
3
+ size 9457288
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
special_tokens_map.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "</s>",
4
+ "lstrip": false,
5
+ "normalized": true,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "</s>",
11
+ "lstrip": false,
12
+ "normalized": true,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": "<pad>",
17
+ "unk_token": {
18
+ "content": "</s>",
19
+ "lstrip": false,
20
+ "normalized": true,
21
+ "rstrip": false,
22
+ "single_word": false
23
+ }
24
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": true,
3
+ "add_prefix_space": false,
4
+ "added_tokens_decoder": {
5
+ "1": {
6
+ "content": "<pad>",
7
+ "lstrip": false,
8
+ "normalized": true,
9
+ "rstrip": false,
10
+ "single_word": false,
11
+ "special": true
12
+ },
13
+ "2": {
14
+ "content": "</s>",
15
+ "lstrip": false,
16
+ "normalized": true,
17
+ "rstrip": false,
18
+ "single_word": false,
19
+ "special": true
20
+ }
21
+ },
22
+ "bos_token": "</s>",
23
+ "clean_up_tokenization_spaces": false,
24
+ "eos_token": "</s>",
25
+ "errors": "replace",
26
+ "extra_special_tokens": {},
27
+ "model_max_length": 1000000000000000019884624838656,
28
+ "pad_token": "<pad>",
29
+ "tokenizer_class": "GPT2Tokenizer",
30
+ "unk_token": "</s>"
31
+ }
vocab.json ADDED
The diff for this file is too large to render. See raw diff