-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
135 lines (117 loc) · 4.91 KB
/
main.py
File metadata and controls
135 lines (117 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from pathlib import Path
from typing import cast
from pandas import DataFrame
from policy_gen.llm import LLM, Prompt
from datasets import DatasetReader
def process_datasets() -> tuple[dict[str, DataFrame], dict[str, DataFrame]]:
"""Process datasets for policy generation"""
dr = DatasetReader()
litroacp_path = Path(__file__).parent / "datasets" / "litroacp"
print(f"Loading datasets from: {litroacp_path}")
nl_datasets = dr.load_datasets(litroacp_path)
print(f"Loaded datasets: {list(nl_datasets.keys())}")
xacbench_path = Path(__file__).parent / "datasets" / "xacbench"
print(f"Loading XACBench datasets from: {xacbench_path}")
xacml_datasets = dr.load_datasets(xacbench_path)
print(f"Loaded XACBench datasets: {list(xacml_datasets.keys())}")
return nl_datasets, xacml_datasets
def generate_policies(
nl_datasets: dict[str, DataFrame],
xacml_datasets: dict[str, DataFrame],
):
"""
Generate policies using LLM based on NL datasets and XACML datasets
"""
"""Load system message template"""
system_msg_path = Path(__file__).parent / "policy_gen" / "llm" / "system_msg.txt"
system_msg = ""
with open(system_msg_path, "r") as f:
system_msg = f.read()
print(f"Loaded system message from: {system_msg_path}")
print(system_msg)
"""Generate policies for NL datasets"""
for nl_dataset_name, nl_dataset in nl_datasets.items():
print(
f"Processing NL dataset: {nl_dataset_name} with {len(nl_dataset)} records"
)
nl_dataset_processed = nl_dataset.drop(
["id", "entities", "relations", "Comments"], axis="columns", inplace=False
)
nl_dataset_processed.rename(columns={"text": "user_message"}, inplace=True)
nl_dataset_processed.insert(
nl_dataset_processed.shape[1],
"system_message",
system_msg,
)
nl_dataset_processed.insert(
nl_dataset_processed.shape[1],
"generated_datalog",
"",
)
print(nl_dataset_processed.head())
llm = LLM()
for index, row in nl_dataset_processed.iterrows():
prompt = Prompt(
system=row["system_message"],
user=row["user_message"],
)
output = llm.generate(prompt)
print(f"Generated policy for record {index}:\n{output}")
nl_dataset_processed.loc[cast(int, index), "generated_datalog"] = output
nl_datasets[nl_dataset_name] = nl_dataset_processed
"""Generate policies for XACML datasets"""
for xacml_dataset_name, xacml_dataset in xacml_datasets.items():
print(
f"Processing XACML dataset: {xacml_dataset_name} with {len(xacml_dataset)} records"
)
xacml_dataset_processed = xacml_dataset.drop([], axis="columns", inplace=False)
xacml_dataset_processed.rename(columns={"policy": "user_message"}, inplace=True)
xacml_dataset_processed.insert(
xacml_dataset_processed.shape[1],
"system_message",
system_msg,
)
xacml_dataset_processed.insert(
xacml_dataset_processed.shape[1],
"generated_datalog",
"",
)
print(xacml_dataset_processed.head())
llm = LLM()
for index, row in xacml_dataset_processed.iterrows():
prompt = Prompt(
system=row["system_message"],
user=row["user_message"],
)
output = llm.generate(prompt)
print(f"Generated policy for record {index}:\n{output}")
xacml_dataset_processed.loc[cast(int, index), "generated_datalog"] = output
xacml_datasets[xacml_dataset_name] = xacml_dataset_processed
# Save generated datasets to excel files
for nl_dataset_name, nl_dataset in nl_datasets.items():
output_path = f"_gen_outputs/nl/{nl_dataset_name}.xlsx"
nl_dataset.to_excel(output_path, index=False)
print(f"Saved generated NL dataset to: {output_path}")
for xacml_dataset_name, xacml_dataset in xacml_datasets.items():
output_path = f"_gen_outputs/xacml/{xacml_dataset_name}.xlsx"
xacml_dataset.to_excel(output_path, index=False)
print(f"Saved generated XACML dataset to: {output_path}")
return nl_datasets, xacml_datasets
def translate_policies():
"""Translate policies using tailored algorithms"""
pass
def main():
# Oh god! These are dirty variables ...
nl_datasets, xacml_datasets = process_datasets()
nl_gen_results, xacml_gen_results = generate_policies(nl_datasets, xacml_datasets)
print("NL Generated Results:")
for name, df in nl_gen_results.items():
print(f"Dataset: {name}")
print(df.head())
print("XACML Generated Results:")
for name, df in xacml_gen_results.items():
print(f"Dataset: {name}")
print(df.head())
translate_policies()
if __name__ == "__main__":
main()