generated from MinBZK/python-project-template
-
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
1 parent
d6175ac
commit fb5b855
Showing
19 changed files
with
22,614 additions
and
2,146 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -16,7 +16,6 @@ class TaskRegistryRepository: | |
|
||
def __init__(self, client: TaskRegistryAPIClient) -> None: | ||
self.client = client | ||
self._urn_cache: dict[TaskType, list[str]] = {} | ||
|
||
async def fetch_tasks(self, task_type: TaskType, urns: str | Sequence[str] | None = None) -> list[dict[str, Any]]: | ||
""" | ||
|
@@ -41,10 +40,8 @@ async def _fetch_valid_urns(self, task_type: TaskType) -> list[str]: | |
""" | ||
Fetches all valid URNs for the given task type. | ||
""" | ||
if task_type not in self._urn_cache: | ||
content_list = await self.client.get_list_of_task(task_type) | ||
self._urn_cache[task_type] = [content.urn for content in content_list.root] | ||
return self._urn_cache[task_type] | ||
content_list = await self.client.get_list_of_task(task_type) | ||
return [content.urn for content in content_list.root] | ||
|
||
async def _fetch_tasks_by_urns(self, task_type: TaskType, urns: Sequence[str]) -> list[dict[str, Any]]: | ||
""" | ||
|
@@ -55,12 +52,18 @@ async def _fetch_tasks_by_urns(self, task_type: TaskType, urns: Sequence[str]) - | |
results = await asyncio.gather(*get_tasks, return_exceptions=True) | ||
|
||
tasks: list[dict[str, Any]] = [] | ||
for result in results: | ||
failed_urns: list[str] = [] | ||
for urn, result in zip(urns, results, strict=True): | ||
if isinstance(result, dict): | ||
tasks.append(result) | ||
elif isinstance(result, AMTNotFound): | ||
logger.warning(f"Cannot find {task_type.value}") | ||
else: | ||
failed_urns.append(urn) | ||
elif isinstance(result, Exception): | ||
raise result | ||
|
||
if failed_urns: | ||
# Sonar cloud does not like displaying the failed urns, so the warning is now | ||
Check notice Code scanning / SonarCloud Logging should not be vulnerable to injection attacks Low
Change this code to not log user-controlled data. See more on SonarQube Cloud
|
||
# generic without specification of the urns. | ||
logger.warning("Cannot find all tasks") | ||
|
||
return tasks |
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
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
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 |
---|---|---|
@@ -1,26 +1,86 @@ | ||
import logging | ||
|
||
from amt.schema.measure import MeasureTask | ||
from amt.schema.requirement import RequirementTask | ||
from amt.schema.requirement import Requirement, RequirementTask | ||
from amt.schema.system_card import AiActProfile | ||
from amt.services.measures import create_measures_service | ||
from amt.services.requirements import create_requirements_service | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_requirements(ai_act_profile: AiActProfile) -> list[RequirementTask]: | ||
requirements: list[RequirementTask] = [] | ||
def is_requirement_applicable(requirement: Requirement, ai_act_profile: AiActProfile) -> bool: | ||
""" | ||
Determine if a specific requirement is applicable to a given AI Act profile. | ||
return requirements | ||
Evaluation Criteria: | ||
- Always applicable requirements automatically return True. | ||
- For the 'role' attribute, handles compound values like | ||
"gebruiksverantwoordelijke + aanbieder". | ||
- For the 'systemic_risk' attribute, handles the old name 'publication_category'. | ||
- A requirement is applicable if all specified attributes match or have no | ||
specific restrictions. | ||
""" | ||
if requirement.always_applicable == 1: | ||
return True | ||
|
||
# We can assume the ai_act_profile field always contains exactly 1 element. | ||
requirement_profile = requirement.ai_act_profile[0] | ||
comparison_attrs = ( | ||
"type", | ||
"risk_category", | ||
"type", | ||
"open_source", | ||
"systemic_risk", | ||
"transparency_obligations", | ||
) | ||
|
||
def get_requirements_and_measures( | ||
for attr in comparison_attrs: | ||
requirement_attr_values = getattr(requirement_profile, attr, []) | ||
|
||
if not requirement_attr_values: | ||
continue | ||
|
||
input_value = _parse_attribute_values(attr, ai_act_profile) | ||
|
||
if not input_value & {attr_value.value for attr_value in requirement_attr_values}: | ||
return False | ||
|
||
return True | ||
|
||
|
||
async def get_requirements_and_measures( | ||
ai_act_profile: AiActProfile, | ||
) -> tuple[ | ||
list[RequirementTask], | ||
list[MeasureTask], | ||
]: | ||
# TODO (Robbert): the body of this method will be added later (another ticket) | ||
measure_card: list[MeasureTask] = [] | ||
requirements_card: list[RequirementTask] = [] | ||
|
||
return requirements_card, measure_card | ||
) -> tuple[list[RequirementTask], list[MeasureTask]]: | ||
requirements_service = create_requirements_service() | ||
measure_service = create_measures_service() | ||
all_requirements = await requirements_service.fetch_requirements() | ||
|
||
applicable_requirements: list[RequirementTask] = [] | ||
applicable_measures: list[MeasureTask] = [] | ||
measure_urns: set[str] = set() | ||
|
||
for requirement in all_requirements: | ||
if is_requirement_applicable(requirement, ai_act_profile): | ||
applicable_requirements.append(RequirementTask(urn=requirement.urn, version=requirement.schema_version)) | ||
|
||
for measure_urn in requirement.links: | ||
if measure_urn not in measure_urns: | ||
measure = await measure_service.fetch_measures(measure_urn) | ||
applicable_measures.append(MeasureTask(urn=measure_urn, version=measure[0].schema_version)) | ||
measure_urns.add(measure_urn) | ||
|
||
return applicable_requirements, applicable_measures | ||
|
||
|
||
def _parse_attribute_values(attr: str, ai_act_profile: AiActProfile) -> set[str]: | ||
""" | ||
Helper function needed in `is_requirement_applicable`, handling special case for 'role' | ||
and 'publication_category'. | ||
""" | ||
if attr == "role": | ||
return {s.strip() for s in getattr(ai_act_profile, attr, "").split("+")} | ||
if attr == "risk_category": | ||
return {getattr(ai_act_profile, "publication_category", "")} | ||
|
||
return {getattr(ai_act_profile, attr, "")} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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.