def update_model_card_json(self, model_card: ModelCard) -> None: """Validates the model card and updates the JSON file in MCT assets. If model_card.schema_version is not provided, it will assign the latest schema version to the `model_card`, and validate it. Args: model_card: The updated model card that users want to write back. Raises: Error: when the given model_card is invalid w.r.t. the schema. """ if not model_card.schema_version: model_card.schema_version = validation.get_latest_schema_version() validation.validate_json_schema(model_card.to_dict(), model_card.schema_version) self._write_file(self._mcta_json_file, model_card.to_json())
def _update_from_v1_to_v2(json_dict: Dict[Text, Any]) -> Dict[Text, Any]: """Updates a Model Card JSON v0.0.1 string to v0.0.2. Args: json_dict: A dictionary representing a Model Card v0.0.1 JSON object. Returns: The input Model Card, converted to a v0.0.2 JSON string. Raises: JSONDecodeError: If `json_dict` is not a valid JSON string. ValidationError: If `json_dict` does not follow the model card JSON v0.0.1 schema. """ # Validate input args schema validation.validate_json_schema(json_dict, "0.0.1") # Update schema version json_dict["schema_version"] = validation.get_latest_schema_version() # Update model_details if json_dict["model_details"]["license"]: json_dict["model_details"]["licenses"] = [{ "custom_text": json_dict["model_details"].pop("license") }] if json_dict["model_details"]["references"]: json_dict["model_details"]["references"] = [{ "reference": reference } for reference in json_dict["model_details"]["references"]] if json_dict["model_details"]["citation"]: json_dict["model_details"]["citations"] = [{ "citation": json_dict["model_details"].pop("citation") }] # Update model_parameters if "model_parameters" in json_dict and "data" in json_dict[ "model_parameters"]: new_data = [] if "train" in json_dict["model_parameters"]["data"]: old_train_data = json_dict["model_parameters"]["data"]["train"] if "name" not in old_train_data: old_train_data["name"] = "Training Set" new_data.append(old_train_data) if "eval" in json_dict["model_parameters"]["data"]: old_eval_data = json_dict["model_parameters"]["data"]["eval"] if "name" not in old_eval_data: old_eval_data["name"] = "Validation Set" new_data.append(old_eval_data) json_dict["model_parameters"]["data"] = new_data # Update considerations if "considerations" in json_dict and "use_cases" in json_dict[ "considerations"]: json_dict["considerations"]["use_cases"] = [{ "description": use_case } for use_case in json_dict["considerations"]["use_cases"]] if "considerations" in json_dict and "users" in json_dict["considerations"]: json_dict["considerations"]["users"] = [{ "description": user } for user in json_dict["considerations"]["users"]] if "considerations" in json_dict and "limitations" in json_dict[ "considerations"]: json_dict["considerations"]["limitations"] = [{ "description": limitation } for limitation in json_dict["considerations"]["limitations"]] if "considerations" in json_dict and "tradeoffs" in json_dict[ "considerations"]: json_dict["considerations"]["tradeoffs"] = [{ "description": limitation } for limitation in json_dict["considerations"]["tradeoffs"]] return json_dict
def to_json(self) -> Text: """Write ModelCard to JSON.""" model_card_dict = self.to_dict() model_card_dict[ _SCHEMA_VERSION_STRING] = validation.get_latest_schema_version() return json_lib.dumps(model_card_dict, indent=2)