Spaces:
Running
Running
File size: 7,870 Bytes
8819832 bc1cc5a 8819832 bc1cc5a 8819832 bc1cc5a 8819832 bc1cc5a 8819832 bc1cc5a 8819832 bc1cc5a 8819832 bc1cc5a 8819832 bc1cc5a 8819832 bc1cc5a 8819832 |
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
"""
Utility functions for the AIBOM Generator.
"""
import json
import logging
import os
import re
import uuid
from typing import Dict, List, Optional, Any, Union
logger = logging.getLogger(__name__)
def setup_logging(level=logging.INFO):
"""Set up logging configuration."""
logging.basicConfig(
level=level,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
def ensure_directory(directory_path):
"""Ensure that a directory exists, creating it if necessary."""
if not os.path.exists(directory_path):
os.makedirs(directory_path)
return directory_path
def generate_uuid():
"""Generate a UUID for the AIBOM serialNumber."""
return str(uuid.uuid4())
def normalize_license_id(license_text):
"""
Normalize a license string to a SPDX license identifier if possible.
Args:
license_text: The license text to normalize
Returns:
SPDX license identifier or the original text if no match
"""
# Common license mappings
license_mappings = {
"mit": "MIT",
"apache": "Apache-2.0",
"apache 2": "Apache-2.0",
"apache 2.0": "Apache-2.0",
"apache-2": "Apache-2.0",
"apache-2.0": "Apache-2.0",
"gpl": "GPL-3.0-only",
"gpl-3": "GPL-3.0-only",
"gpl-3.0": "GPL-3.0-only",
"gpl3": "GPL-3.0-only",
"gpl v3": "GPL-3.0-only",
"gpl-2": "GPL-2.0-only",
"gpl-2.0": "GPL-2.0-only",
"gpl2": "GPL-2.0-only",
"gpl v2": "GPL-2.0-only",
"lgpl": "LGPL-3.0-only",
"lgpl-3": "LGPL-3.0-only",
"lgpl-3.0": "LGPL-3.0-only",
"bsd": "BSD-3-Clause",
"bsd-3": "BSD-3-Clause",
"bsd-3-clause": "BSD-3-Clause",
"bsd-2": "BSD-2-Clause",
"bsd-2-clause": "BSD-2-Clause",
"cc": "CC-BY-4.0",
"cc-by": "CC-BY-4.0",
"cc-by-4.0": "CC-BY-4.0",
"cc-by-sa": "CC-BY-SA-4.0",
"cc-by-sa-4.0": "CC-BY-SA-4.0",
"cc-by-nc": "CC-BY-NC-4.0",
"cc-by-nc-4.0": "CC-BY-NC-4.0",
"cc0": "CC0-1.0",
"cc0-1.0": "CC0-1.0",
"public domain": "CC0-1.0",
"unlicense": "Unlicense",
"proprietary": "NONE",
"commercial": "NONE",
}
if not license_text:
return None
# Normalize to lowercase and remove punctuation
normalized = re.sub(r'[^\w\s-]', '', license_text.lower())
# Check for direct matches
if normalized in license_mappings:
return license_mappings[normalized]
# Check for partial matches
for key, value in license_mappings.items():
if key in normalized:
return value
# Return original if no match
return license_text
def calculate_completeness_score(aibom: Dict[str, Any]) -> Dict[str, Any]:
"""
Calculate a completeness score for the AIBOM.
Args:
aibom: The AIBOM dictionary
Returns:
Dictionary containing:
- total_score: overall completeness score (0-100)
- section_scores: points earned per section
- field_checklist: dictionary showing presence (β) or absence (β) of key fields
"""
score = 0
max_score = 100
section_scores = {}
field_checklist = {}
# Define scoring weights for different sections
weights = {
"required_fields": 20,
"metadata": 20,
"component_basic": 20,
"component_model_card": 30,
"external_references": 10,
}
# Required Fields
required_fields = ["bomFormat", "specVersion", "serialNumber", "version"]
required_present = [field for field in required_fields if field in aibom]
required_score = (len(required_present) / len(required_fields)) * weights["required_fields"]
section_scores["required_fields"] = round(required_score)
for field in required_fields:
field_checklist[field] = "β" if field in required_present else "β"
# Metadata Fields
metadata_score = 0
if "metadata" in aibom:
metadata_fields = ["timestamp", "tools", "authors", "component"]
present = [field for field in metadata_fields if field in aibom["metadata"]]
metadata_score = (len(present) / len(metadata_fields)) * weights["metadata"]
for field in metadata_fields:
field_checklist[f"metadata.{field}"] = "β" if field in present else "β"
section_scores["metadata"] = round(metadata_score)
# Component Basic Info
component_score = 0
component = aibom.get("components", [{}])[0]
component_fields = ["type", "name", "bom-ref", "purl", "description", "licenses"]
present = [field for field in component_fields if field in component]
component_score = (len(present) / len(component_fields)) * weights["component_basic"]
section_scores["component_basic"] = round(component_score)
for field in component_fields:
field_checklist[f"component.{field}"] = "β" if field in present else "β"
# Model Card Section
model_card_score = 0
model_card_fields = ["modelParameters", "quantitativeAnalysis", "considerations"]
if "modelCard" in component:
model_card = component["modelCard"]
present = [field for field in model_card_fields if field in model_card]
model_card_score = (len(present) / len(model_card_fields)) * weights["component_model_card"]
for field in model_card_fields:
field_checklist[f"modelCard.{field}"] = "β" if field in present else "β"
else:
for field in model_card_fields:
field_checklist[f"modelCard.{field}"] = "β"
section_scores["component_model_card"] = round(model_card_score)
# External References
ext_score = weights["external_references"] if aibom.get("externalReferences") else 0
section_scores["external_references"] = round(ext_score)
field_checklist["externalReferences"] = "β" if ext_score else "β"
# Final total score
total_score = round(sum(section_scores.values()))
return {
"total_score": total_score,
"section_scores": section_scores,
"field_checklist": field_checklist
}
def merge_metadata(primary: Dict[str, Any], secondary: Dict[str, Any]) -> Dict[str, Any]:
"""
Merge two metadata dictionaries, giving priority to the primary dictionary.
Args:
primary: Primary metadata dictionary
secondary: Secondary metadata dictionary
Returns:
Merged metadata dictionary
"""
result = secondary.copy()
for key, value in primary.items():
if value is not None:
if key in result and isinstance(value, dict) and isinstance(result[key], dict):
result[key] = merge_metadata(value, result[key])
else:
result[key] = value
return result
def extract_model_id_parts(model_id: str) -> Dict[str, str]:
"""
Extract parts from a Hugging Face model ID.
Args:
model_id: Hugging Face model ID (e.g., "google/bert-base-uncased")
Returns:
Dictionary with parts (owner, name)
"""
parts = model_id.split("/")
if len(parts) == 1:
return {
"owner": None,
"name": parts[0],
}
else:
return {
"owner": parts[0],
"name": "/".join(parts[1:]),
}
def create_purl(model_id: str) -> str:
"""
Create a Package URL (purl) for a Hugging Face model.
Args:
model_id: Hugging Face model ID
Returns:
Package URL string
"""
parts = extract_model_id_parts(model_id)
if parts["owner"]:
return f"pkg:huggingface/{parts['owner']}/{parts['name']}"
else:
return f"pkg:huggingface/{parts['name']}"
|