Spaces:
Sleeping
Sleeping
qqubb
commited on
Commit
·
f4fb0fb
1
Parent(s):
458daef
updates and improvements to code
Browse files- __pycache__/compliance_analysis.cpython-310.pyc +0 -0
- __pycache__/utils.cpython-310.pyc +0 -0
- compliance_analysis.py +72 -46
- project_cc.yaml +2 -2
- run.py +8 -13
- utils.py +20 -21
__pycache__/compliance_analysis.cpython-310.pyc
CHANGED
Binary files a/__pycache__/compliance_analysis.cpython-310.pyc and b/__pycache__/compliance_analysis.cpython-310.pyc differ
|
|
__pycache__/utils.cpython-310.pyc
CHANGED
Binary files a/__pycache__/utils.cpython-310.pyc and b/__pycache__/utils.cpython-310.pyc differ
|
|
compliance_analysis.py
CHANGED
@@ -3,7 +3,14 @@ from utils import set_operator_role_and_location, set_eu_market_status, check_wi
|
|
3 |
|
4 |
# Create some variables we will use throughout our analysis
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
"ai_project_type": {
|
8 |
"ai_system": False,
|
9 |
"gpai_model": False,
|
@@ -24,61 +31,75 @@ dispositive_variables = {
|
|
24 |
"data_cc_pass": False,
|
25 |
"model_cc_pass": False,
|
26 |
"msg": []
|
27 |
-
}
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
def check_overall_compliance(dispositive_variables, cc_files):
|
33 |
-
|
34 |
|
|
|
35 |
# check intended purposes
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# for each model_cc and data_cc - run analysis with ref to project_cc
|
39 |
|
|
|
|
|
40 |
# dispositive_variables = run_compliance_analysis_on_data(dispositive_variables, data_cc_yaml)
|
41 |
# dispositive_variables = run_compliance_analysis_on_model(dispositive_variables, model_cc_yaml)
|
42 |
-
|
43 |
-
dispositive_variables = run_compliance_analysis_on_project(dispositive_variables, project_cc_yaml)
|
44 |
|
45 |
return dispositive_variables
|
46 |
|
47 |
-
def run_compliance_analysis_on_project(project_cc_yaml):
|
48 |
|
49 |
# Project Type
|
50 |
if project_cc_yaml['ai_system']['ai_system']['value']:
|
51 |
dispositive_variables['ai_project_type']['ai_system'] = True
|
52 |
if project_cc_yaml['gpai_model']['gpai_model']['value']:
|
53 |
dispositive_variables['ai_project_type']['gpai_model'] = True
|
54 |
-
if
|
55 |
dispositive_variables['msg'] = "Your project cannot be both an AI system and a GPAI model. Please revise your Project CC accordingly."
|
56 |
return dispositive_variables
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
|
|
|
|
64 |
if project_cc_yaml['gpai_model_systematic_risk']['evaluation'] or project_cc_yaml['gpai_model_systematic_risk']['flops']:
|
65 |
-
|
66 |
|
67 |
# Operator Type
|
68 |
-
set_operator_role_and_location(dispositive_variables, project_cc_yaml)
|
69 |
-
set_eu_market_status(dispositive_variables, project_cc_yaml)
|
70 |
|
71 |
# Check if project is within scope of the Compliance Cards project. If not, inform user.
|
72 |
-
if check_within_scope_cc(
|
73 |
-
msg
|
74 |
else:
|
75 |
-
msg
|
76 |
|
77 |
# Check if the project is within scope of the Act. If it's not, the analysis is over.
|
78 |
-
if check_within_scope_act(
|
79 |
-
msg
|
80 |
else:
|
81 |
-
msg
|
|
|
|
|
82 |
|
83 |
# TODO: reactivate the prohibited practices check below
|
84 |
|
@@ -92,7 +113,7 @@ def run_compliance_analysis_on_project(project_cc_yaml):
|
|
92 |
|
93 |
# If project is high-risk AI system, check that is has met all the requirements for such systems:
|
94 |
|
95 |
-
if
|
96 |
|
97 |
# Do this by examining the Project CC
|
98 |
|
@@ -122,7 +143,7 @@ def run_compliance_analysis_on_project(project_cc_yaml):
|
|
122 |
# TODO
|
123 |
# # If the project is a GPAI model, check that is has met all the requirements for such systems:
|
124 |
|
125 |
-
if gpai_model:
|
126 |
|
127 |
# # If the project is a GPAI model with systematic risk, check that is has additionally met all the requirements for such systems:
|
128 |
|
@@ -224,7 +245,7 @@ def run_compliance_analysis_on_model(dispositive_variables, model_cc_yaml):
|
|
224 |
|
225 |
return dispositive_variables
|
226 |
|
227 |
-
def check_intended_purpose(dispositive_variables,
|
228 |
|
229 |
# We want to run this function for everything classified as a high_risk_ai_system
|
230 |
# We also need to run it for all
|
@@ -245,32 +266,37 @@ def check_intended_purpose(dispositive_variables, cc_files):
|
|
245 |
# "judicial"]
|
246 |
|
247 |
project_intended_purposes = []
|
248 |
-
for key in
|
249 |
-
if
|
250 |
project_intended_purposes.append(key)
|
251 |
|
252 |
# For each Data CC, put the intended uses in a set and then make sure the Project's intended use is in the set
|
253 |
|
254 |
msg = ''
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
|
|
|
|
|
|
259 |
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
|
264 |
# Now do the exact same thing for all models
|
265 |
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
|
|
|
|
270 |
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
|
275 |
# TODO return list of intended purpose
|
276 |
|
|
|
3 |
|
4 |
# Create some variables we will use throughout our analysis
|
5 |
|
6 |
+
|
7 |
+
|
8 |
+
# TODO tells the user where the compliance analysis failed
|
9 |
+
# TODO cite article from yaml file as explanation
|
10 |
+
|
11 |
+
def check_overall_compliance(cards):
|
12 |
+
|
13 |
+
dispositive_variables = {
|
14 |
"ai_project_type": {
|
15 |
"ai_system": False,
|
16 |
"gpai_model": False,
|
|
|
31 |
"data_cc_pass": False,
|
32 |
"model_cc_pass": False,
|
33 |
"msg": []
|
34 |
+
}
|
35 |
+
|
36 |
+
with open(cards['project_file'], 'r') as project_filepath:
|
37 |
+
project_cc = yaml.safe_load(project_filepath.read())
|
|
|
|
|
|
|
38 |
|
39 |
+
|
40 |
# check intended purposes
|
41 |
+
for card in cards['data_file']:
|
42 |
+
dispositive_variables = check_intended_purpose(dispositive_variables, project_cc, card)
|
43 |
+
|
44 |
+
for card in cards['model_file']:
|
45 |
+
dispositive_variables = check_intended_purpose(dispositive_variables, project_cc, card)
|
46 |
+
|
47 |
|
48 |
# for each model_cc and data_cc - run analysis with ref to project_cc
|
49 |
|
50 |
+
dispositive_variables = run_compliance_analysis_on_project(dispositive_variables, project_cc)
|
51 |
+
|
52 |
# dispositive_variables = run_compliance_analysis_on_data(dispositive_variables, data_cc_yaml)
|
53 |
# dispositive_variables = run_compliance_analysis_on_model(dispositive_variables, model_cc_yaml)
|
|
|
|
|
54 |
|
55 |
return dispositive_variables
|
56 |
|
57 |
+
def run_compliance_analysis_on_project(dispositive_variables, project_cc_yaml):
|
58 |
|
59 |
# Project Type
|
60 |
if project_cc_yaml['ai_system']['ai_system']['value']:
|
61 |
dispositive_variables['ai_project_type']['ai_system'] = True
|
62 |
if project_cc_yaml['gpai_model']['gpai_model']['value']:
|
63 |
dispositive_variables['ai_project_type']['gpai_model'] = True
|
64 |
+
if project_cc_yaml['ai_system']['ai_system']['value'] == True and project_cc_yaml['gpai_model']['gpai_model']['value'] == True:
|
65 |
dispositive_variables['msg'] = "Your project cannot be both an AI system and a GPAI model. Please revise your Project CC accordingly."
|
66 |
return dispositive_variables
|
67 |
|
68 |
+
# TODO - move file_exceptions to separate section in project_cc?
|
69 |
+
# if dispositive_variables['ai_project_type']['ai_system'] == True:
|
70 |
+
# for value in project_cc_yaml['high_risk_ai_system']:
|
71 |
+
|
72 |
+
# if value and sum(map(bool, [
|
73 |
+
# project_cc_yaml['high_risk_ai_system']['filter_exception_rights'],
|
74 |
+
# project_cc_yaml['high_risk_ai_system']['filter_exception_narrow'],
|
75 |
+
# project_cc_yaml['high_risk_ai_system']['filter_exception_human'],
|
76 |
+
# project_cc_yaml['high_risk_ai_system']['filter_exception_deviation'],
|
77 |
+
# project_cc_yaml['high_risk_ai_system']['filter_exception_prep']])
|
78 |
+
# ) < 1:
|
79 |
|
80 |
+
# dispositive_variables['ai_project_type']["high_risk_ai_system"] = True
|
81 |
+
|
82 |
+
if dispositive_variables['ai_project_type']['gpai_model'] == True:
|
83 |
if project_cc_yaml['gpai_model_systematic_risk']['evaluation'] or project_cc_yaml['gpai_model_systematic_risk']['flops']:
|
84 |
+
dispositive_variables['ai_project_type']["gpai_model_systematic_risk"] = True
|
85 |
|
86 |
# Operator Type
|
87 |
+
dispositive_variables = set_operator_role_and_location(dispositive_variables, project_cc_yaml)
|
88 |
+
dispositive_variables = set_eu_market_status(dispositive_variables, project_cc_yaml)
|
89 |
|
90 |
# Check if project is within scope of the Compliance Cards project. If not, inform user.
|
91 |
+
if check_within_scope_cc(project_cc_yaml):
|
92 |
+
dispositive_variables['msg'].append("Project is within the scope of the Compliance Cards system. Let's continue...")
|
93 |
else:
|
94 |
+
dispositive_variables['msg'].append("Project is not within the scope of the initial version of the Compliance Cards system.")
|
95 |
|
96 |
# Check if the project is within scope of the Act. If it's not, the analysis is over.
|
97 |
+
if check_within_scope_act(project_cc_yaml):
|
98 |
+
dispositive_variables['msg'].append("Project is within the scope of Act. Let's continue...")
|
99 |
else:
|
100 |
+
dispositive_variables['msg'].append("Project is not within the scope of what is regulated by the Act.")
|
101 |
+
|
102 |
+
|
103 |
|
104 |
# TODO: reactivate the prohibited practices check below
|
105 |
|
|
|
113 |
|
114 |
# If project is high-risk AI system, check that is has met all the requirements for such systems:
|
115 |
|
116 |
+
if dispositive_variables['ai_project_type']["high_risk_ai_system"]:
|
117 |
|
118 |
# Do this by examining the Project CC
|
119 |
|
|
|
143 |
# TODO
|
144 |
# # If the project is a GPAI model, check that is has met all the requirements for such systems:
|
145 |
|
146 |
+
if dispositive_variables['ai_project_type']["gpai_model"]:
|
147 |
|
148 |
# # If the project is a GPAI model with systematic risk, check that is has additionally met all the requirements for such systems:
|
149 |
|
|
|
245 |
|
246 |
return dispositive_variables
|
247 |
|
248 |
+
def check_intended_purpose(dispositive_variables, project_cc, other_cc):
|
249 |
|
250 |
# We want to run this function for everything classified as a high_risk_ai_system
|
251 |
# We also need to run it for all
|
|
|
266 |
# "judicial"]
|
267 |
|
268 |
project_intended_purposes = []
|
269 |
+
for key in project_cc['high_risk_ai_system']:
|
270 |
+
if project_cc['high_risk_ai_system'][f'{key}']['value']:
|
271 |
project_intended_purposes.append(key)
|
272 |
|
273 |
# For each Data CC, put the intended uses in a set and then make sure the Project's intended use is in the set
|
274 |
|
275 |
msg = ''
|
276 |
+
|
277 |
+
if other_cc['card_type'] == 'data':
|
278 |
+
data_cc = other_cc
|
279 |
+
dataset_intended_purposes = []
|
280 |
+
for key in data_cc['high_risk_ai_system']:
|
281 |
+
if data_cc['high_risk_ai_system'][f'{key}']['value']:
|
282 |
+
dataset_intended_purposes.append(key)
|
283 |
|
284 |
+
for purpose in project_intended_purposes:
|
285 |
+
if purpose not in dataset_intended_purposes:
|
286 |
+
msg = f"You are not compliant because {purpose} is not a valid purpose"
|
287 |
|
288 |
# Now do the exact same thing for all models
|
289 |
|
290 |
+
if other_cc['card_type'] == 'model':
|
291 |
+
model_cc = other_cc
|
292 |
+
model_intended_purposes = []
|
293 |
+
for key in model_cc['high_risk_ai_system']:
|
294 |
+
if model_cc['high_risk_ai_system'][f'{key}']['value']:
|
295 |
+
model_intended_purposes.append(key)
|
296 |
|
297 |
+
for purpose in project_intended_purposes:
|
298 |
+
if purpose not in model_intended_purposes:
|
299 |
+
msg = f"You are not compliant because {purpose} is not a valid purpose"
|
300 |
|
301 |
# TODO return list of intended purpose
|
302 |
|
project_cc.yaml
CHANGED
@@ -30,13 +30,13 @@ ai_system:
|
|
30 |
ai_system:
|
31 |
article: 'Art. 3(1)'
|
32 |
verbose: 'AI project is a machine-based system that is designed to operate with varying levels of autonomy and that may exhibit adaptiveness after deployment, and that, for explicit or implicit objectives, infers, from the input it receives, how to generate outputs such as predictions, content, recommendations, or decisions that can influence physical or virtual environments'
|
33 |
-
value: !!bool
|
34 |
|
35 |
gpai_model:
|
36 |
gpai_model:
|
37 |
article: 'Art. 3(63)'
|
38 |
verbose: 'AI project is an AI model, including where such an AI model is trained with a large amount of data using self-supervision at scale, that displays significant generality and is capable of competently performing a wide range of distinct tasks regardless of the way the model is placed on the market and that can be integrated into a variety of downstream systems or applications, except AI models that are used for research, development or prototyping activities before they are placed on the market'
|
39 |
-
value: !!bool
|
40 |
|
41 |
# Information related to whether or not the project, if an AI system, is a high-risk AI system
|
42 |
|
|
|
30 |
ai_system:
|
31 |
article: 'Art. 3(1)'
|
32 |
verbose: 'AI project is a machine-based system that is designed to operate with varying levels of autonomy and that may exhibit adaptiveness after deployment, and that, for explicit or implicit objectives, infers, from the input it receives, how to generate outputs such as predictions, content, recommendations, or decisions that can influence physical or virtual environments'
|
33 |
+
value: !!bool false
|
34 |
|
35 |
gpai_model:
|
36 |
gpai_model:
|
37 |
article: 'Art. 3(63)'
|
38 |
verbose: 'AI project is an AI model, including where such an AI model is trained with a large amount of data using self-supervision at scale, that displays significant generality and is capable of competently performing a wide range of distinct tasks regardless of the way the model is placed on the market and that can be integrated into a variety of downstream systems or applications, except AI models that are used for research, development or prototyping activities before they are placed on the market'
|
39 |
+
value: !!bool false
|
40 |
|
41 |
# Information related to whether or not the project, if an AI system, is a high-risk AI system
|
42 |
|
run.py
CHANGED
@@ -2,7 +2,7 @@ import yaml
|
|
2 |
import json
|
3 |
from pathlib import Path
|
4 |
import pandas as pd
|
5 |
-
from compliance_analysis import
|
6 |
|
7 |
pd.set_option('display.max_columns', None)
|
8 |
pd.set_option('display.max_rows', None)
|
@@ -50,7 +50,6 @@ def gather_cards(files):
|
|
50 |
return cards
|
51 |
|
52 |
cards = gather_cards(files)
|
53 |
-
print(cards)
|
54 |
|
55 |
# def load_data(files):
|
56 |
# cards = []
|
@@ -58,7 +57,7 @@ print(cards)
|
|
58 |
# with open(file, 'r') as f:
|
59 |
# if Path(f.name).name == "project_cc.yaml":
|
60 |
# content = f.read()
|
61 |
-
#
|
62 |
# data = project_cc_yaml
|
63 |
# card_type = "project"
|
64 |
# cards.append((card_type, data))
|
@@ -74,15 +73,11 @@ print(cards)
|
|
74 |
# cards.append((card_type, data))
|
75 |
# return cards
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
|
83 |
-
|
84 |
-
# data = card[1]
|
85 |
-
# yaml_data = yaml.dump(data, sort_keys=False)
|
86 |
-
# process_files(yaml_data)
|
87 |
-
# print(process_files(yaml_data))
|
88 |
|
|
|
2 |
import json
|
3 |
from pathlib import Path
|
4 |
import pandas as pd
|
5 |
+
from compliance_analysis import check_overall_compliance
|
6 |
|
7 |
pd.set_option('display.max_columns', None)
|
8 |
pd.set_option('display.max_rows', None)
|
|
|
50 |
return cards
|
51 |
|
52 |
cards = gather_cards(files)
|
|
|
53 |
|
54 |
# def load_data(files):
|
55 |
# cards = []
|
|
|
57 |
# with open(file, 'r') as f:
|
58 |
# if Path(f.name).name == "project_cc.yaml":
|
59 |
# content = f.read()
|
60 |
+
# pcrojet_cc_yaml = yaml.safe_load(content)
|
61 |
# data = project_cc_yaml
|
62 |
# card_type = "project"
|
63 |
# cards.append((card_type, data))
|
|
|
73 |
# cards.append((card_type, data))
|
74 |
# return cards
|
75 |
|
76 |
+
def compliance_analysis(cards):
|
77 |
+
results = []
|
78 |
+
dispositive_variables = check_overall_compliance(cards)
|
79 |
+
results.append(dispositive_variables)#['msg'])
|
80 |
+
return results
|
81 |
|
82 |
+
print(json.dumps(compliance_analysis(cards), indent=4,))
|
|
|
|
|
|
|
|
|
83 |
|
utils.py
CHANGED
@@ -6,18 +6,18 @@ import yaml
|
|
6 |
def set_operator_role_and_location(dispositive_variables, project_cc_yaml):
|
7 |
operators = 0
|
8 |
|
9 |
-
ai_system =
|
10 |
-
gpai_model =
|
11 |
-
|
12 |
-
for var in
|
13 |
-
if project_cc_yaml['
|
14 |
-
dispositive_variables['
|
15 |
operators += 1
|
16 |
|
17 |
if ai_system and gpai_model:
|
18 |
-
msg = ("Your project cannot be both an AI system and a GPAI model. Please revise your Project CC accordingly.")
|
19 |
if operators != 1:
|
20 |
-
msg = ("Please specify exactly one operator role.")
|
21 |
|
22 |
return dispositive_variables
|
23 |
|
@@ -28,34 +28,33 @@ def set_eu_market_status(dispositive_variables, project_cc_yaml):
|
|
28 |
if project_cc_yaml['eu_market_status']['put_into_service']['value']:
|
29 |
dispositive_variables['eu_market_status']["put_into_service"] = True
|
30 |
|
31 |
-
if project_cc_yaml['
|
32 |
-
dispositive_variables['
|
33 |
|
34 |
return dispositive_variables
|
35 |
|
36 |
|
37 |
-
def check_within_scope_cc(
|
38 |
|
39 |
# Check that the person filling out the form (the operator) is in fact a provider;
|
40 |
-
|
41 |
-
if dispositive_variables['operator_details']['provider']:
|
42 |
return True
|
43 |
else:
|
44 |
print("The initial versiton of the Compliance Cards System is for provider-side compliance analyses only.")
|
45 |
return False
|
46 |
|
47 |
-
def check_within_scope_act(
|
48 |
|
49 |
# Check that the project is within the scope of the Act
|
50 |
|
51 |
-
ai_system =
|
52 |
-
gpai_model =
|
53 |
|
54 |
-
placed_on_market =
|
55 |
-
put_into_service =
|
56 |
|
57 |
-
eu_located =
|
58 |
-
output_used =
|
59 |
|
60 |
if not check_excepted(project_cc_yaml):
|
61 |
if ((ai_system and (placed_on_market or put_into_service)) or (gpai_model and placed_on_market)): # Article 2.1(a)
|
@@ -74,7 +73,7 @@ def check_excepted(project_cc_yaml):
|
|
74 |
return False
|
75 |
|
76 |
# TODO update function
|
77 |
-
def check_prohibited(
|
78 |
|
79 |
ai_system = project_variables['ai_project_type']['ai_system']
|
80 |
|
|
|
6 |
def set_operator_role_and_location(dispositive_variables, project_cc_yaml):
|
7 |
operators = 0
|
8 |
|
9 |
+
ai_system = project_cc_yaml['ai_system']['ai_system']['value']
|
10 |
+
gpai_model = project_cc_yaml['gpai_model']['gpai_model']['value']
|
11 |
+
|
12 |
+
for var in project_cc_yaml['operator_details']:
|
13 |
+
if project_cc_yaml['operator_details'][f'{var}']['value'] == True:
|
14 |
+
dispositive_variables['operator_details'][f'{var}'] = True
|
15 |
operators += 1
|
16 |
|
17 |
if ai_system and gpai_model:
|
18 |
+
dispositive_variables['msg'] = ("Your project cannot be both an AI system and a GPAI model. Please revise your Project CC accordingly.")
|
19 |
if operators != 1:
|
20 |
+
dispositive_variables['msg'] = ("Please specify exactly one operator role.")
|
21 |
|
22 |
return dispositive_variables
|
23 |
|
|
|
28 |
if project_cc_yaml['eu_market_status']['put_into_service']['value']:
|
29 |
dispositive_variables['eu_market_status']["put_into_service"] = True
|
30 |
|
31 |
+
if project_cc_yaml['operator_details']['output_used']['value']:
|
32 |
+
dispositive_variables['operator_details']["output_used"] = True
|
33 |
|
34 |
return dispositive_variables
|
35 |
|
36 |
|
37 |
+
def check_within_scope_cc(project_cc_yaml):
|
38 |
|
39 |
# Check that the person filling out the form (the operator) is in fact a provider;
|
40 |
+
if project_cc_yaml['operator_details']['provider']['value']:
|
|
|
41 |
return True
|
42 |
else:
|
43 |
print("The initial versiton of the Compliance Cards System is for provider-side compliance analyses only.")
|
44 |
return False
|
45 |
|
46 |
+
def check_within_scope_act(project_cc_yaml):
|
47 |
|
48 |
# Check that the project is within the scope of the Act
|
49 |
|
50 |
+
ai_system = project_cc_yaml['ai_system']['ai_system']
|
51 |
+
gpai_model = project_cc_yaml['gpai_model']['gpai_model']
|
52 |
|
53 |
+
placed_on_market = project_cc_yaml['eu_market_status']['placed_on_market']
|
54 |
+
put_into_service = project_cc_yaml['eu_market_status']['put_into_service']
|
55 |
|
56 |
+
eu_located = project_cc_yaml['operator_details']['eu_located']
|
57 |
+
output_used = project_cc_yaml['operator_details']['output_used']
|
58 |
|
59 |
if not check_excepted(project_cc_yaml):
|
60 |
if ((ai_system and (placed_on_market or put_into_service)) or (gpai_model and placed_on_market)): # Article 2.1(a)
|
|
|
73 |
return False
|
74 |
|
75 |
# TODO update function
|
76 |
+
def check_prohibited(project_cc_yaml):
|
77 |
|
78 |
ai_system = project_variables['ai_project_type']['ai_system']
|
79 |
|