def model_config_model_name_get(ModelName): # noqa: E501 """Obtain an example model configuration. Submit a model name and receive a model configuration for the given model. # noqa: E501 :param model_name: The name of a model. :type model_name: str :rtype: ModelConfig """ # get model try: api_instance = mint_client.ModelApi( mint_client.ApiClient(configuration)) model = api_instance.get_model(ModelName, username=username) versions = [v['id'] for v in model.has_software_version] # for each model version, obtain configuration ids configuration_ids = [] api_instance = mint_client.ModelversionApi( mint_client.ApiClient(configuration)) for v in versions: version = api_instance.get_model_version(v, username=username) c_ids = [c.id for c in version.has_configuration] configuration_ids.extend(c_ids) # get configurations configurations = [] api_instance = mint_client.ModelconfigurationApi( mint_client.ApiClient(configuration)) for _id in configuration_ids: config = api_instance.get_model_configuraton(_id, username=username) configurations.append({ 'name': ModelName, 'config': config.to_dict() }) return configurations except ApiException as e: return "Exception when calling MINT: %s\n" % e
def model_parameters_model_name_post(ModelName): # noqa: E501 """Obtain information about a model's parameters. Submit a model name and receive information about the parameters used by this model. Specific parameters are used on a per-configuration basis. # noqa: E501 :param model_name: The name of a model. :type model_name: str :rtype: List[Parameter] """ # Obtain all parameters associated with *any* configuration for # the given model configs = model_config_model_name_get(ModelName) parameter_ids = set() for c in configs: for param in c['config'].get('has_parameter', []): parameter_ids.add(param['id']) try: api_instance = mint_client.ParameterApi( mint_client.ApiClient(configuration)) # List All Parameters api_response = api_instance.get_parameters(username=username) parameters = [] for param in api_response: if param.id in parameter_ids: parameter = { 'id': param.id, 'description': param.description, 'label': param.label, # need to format the following strings as they have been converted # to arrays by MINT due to duplicate entry attempts 'data_type': util.format_stringed_array(param.has_data_type), 'default_value': util.format_stringed_array(param.has_default_value) } # TODO: need to implement a lookup to grab the standard name # `param.type` is an array of URIs, but does not conform # to how we should present `standard_name`s based on # the MaaS API spec #'standard_name': param.type} parameters.append(parameter) return parameters except ApiException as e: print("Exception when calling ParameterApi->get_parameters: %s\n" % e)
def _get_model(ModelName, MINTconfiguration, MINTusername): configuration = MINTconfiguration username = MINTusername try: api_instance = mint_client.ModelApi( mint_client.ApiClient(configuration)) api_response = api_instance.get_model(ModelName, username=username) model = { 'name': api_response.id, 'description': api_response.description, 'maintainer': '', 'category': api_response.has_model_category, 'versions': [v['id'] for v in api_response.has_software_version] } return model except ApiException as e: return "Exception when calling ModelApi->get_model: %s\n" % e
def list_models_post(): # noqa: E501 """Obtain a list of current models Request a list of currently available models. # noqa: E501 :rtype: List[str] """ api_instance = mint_client.ModelApi(mint_client.ApiClient(configuration)) try: api_response = api_instance.get_models(username=username) models = [m.id for m in api_response] # obtain model info: models_infos = [] for m in models: models_infos.append(model_info_model_name_get(m)) return models_infos except ApiException as e: return "Exception when calling ModelApi->get_models: %s\n" % e
def _find_model_by_dataset_id(dataset_id, MINTconfiguration, MINTuser): print(f"Searching for models associated with the dataset {dataset_id}") configuration = MINTconfiguration username = MINTuser # Step 1: Obtain all MINT Model configurations # from these configurations, extract the input/output dataset IDs # Step 1 output: a set of Configuration IDs associated with the dataset_id api_instance = mint_client.ModelconfigurationApi( mint_client.ApiClient(configuration)) try: # List modelconfiguration api_response = api_instance.get_model_configurations(username=username) dataset_configs = set() for config in api_response: for io in config.has_input: _id = io.id if _id == dataset_id: dataset_configs.add(config.id) for io in config.has_output: _id = io.id if _id == dataset_id: dataset_configs.add(config.id) print( f"Found {len(dataset_configs)} Model Configuration associated with {dataset_id}" ) except ApiException as e: print( "Exception when calling ModelconfigurationApi->get_model_configurations: %s\n" % e) # Step 2: Obtain all Model Versions and their associated Model Configuration IDs # Step 2 output: a set of Model Version IDs associated with the dataset_id api_instance = mint_client.ModelversionApi( mint_client.ApiClient(configuration)) try: # List All ModelVersions version_ids = set() api_response = api_instance.get_model_versions(username=username) for v in api_response: c_ = v.has_configuration for conf in c_: if conf.id in dataset_configs: # this is a config associated with the dataset_id # so add the version to version_ids version_ids.add(v.id) print( f"Found {len(version_ids)} Model Version associated with {dataset_id}" ) except ApiException as e: print( "Exception when calling ModelversionApi->get_model_versions: %s\n" % e) # Step 3: Obtain all Models and check if their versions match the Version IDs of interest # Step 3 output: api_instance = mint_client.ModelApi(mint_client.ApiClient(configuration)) try: # List All models api_response = api_instance.get_models(username=username) models = set() for m in api_response: for v in m.has_software_version: if v['id'] in version_ids: # this is a model associated with the dataset_id # obtain its name models.add(m.label) print(f"Found {len(models)} Models associated with {dataset_id}") except ApiException as e: print("Exception when calling ModelApi->get_models: %s\n" % e) try: models_output = [] for m in list(models): m_ = _get_model(m, configuration, username) models_output.append(m_) return models_output except Exception as e: print(f"Exception when processing model info: {e}")
def model_io_post(): # noqa: E501 """Obtain information on a given model's inputs or outputs. Submit a model name and receive information about the input or output files required by this model. # noqa: E501 Note that this includes all inputs and all outputs for a given model, irrespective of the configuration. In the future this could be subset to just a specific configuration. :param io_request: The name of a model and an IO type. :type io_request: dict | bytes :rtype: List[IOFile] """ if connexion.request.is_json: io_request = IORequest.from_dict( connexion.request.get_json()) # noqa: E501 name_ = io_request.name type_ = io_request.iotype try: # get model configuration ids api_instance = mint_client.ModelApi( mint_client.ApiClient(configuration)) model = api_instance.get_model(name_, username=username) versions = [v['id'] for v in model.has_software_version] # for each model version, obtain configuration ids configuration_ids = [] api_instance = mint_client.ModelversionApi( mint_client.ApiClient(configuration)) for v in versions: version = api_instance.get_model_version(v, username=username) c_ids = [c.id for c in version.has_configuration] configuration_ids.extend(c_ids) # get IO api_instance = mint_client.ModelconfigurationApi( mint_client.ApiClient(configuration)) inputs_outputs = [] if type_ == 'input': for config_id in configuration_ids: response = requests.get( f"https://api.models.mint.isi.edu/v0.0.2/modelconfiguration/{config_id}/inputs?username=modelservice" ) api_response = response.json() for io in api_response: io_ = util._parse_io(io, url, request_headers) if io_: inputs_outputs.append(io_) elif type_ == 'output': outputs = [] for config_id in configuration_ids: response = requests.get( f"https://api.models.mint.isi.edu/v0.0.2/modelconfiguration/{config_id}/outputs?username=modelservice" ) api_response = response.json() for io in api_response: io_ = util._parse_io(io, url, request_headers) if io_: inputs_outputs.append(io_) return inputs_outputs except ApiException as e: return "Exception when calling MINT API: %s\n" % e
# # try: # # Create user # api_instance.create_user(user) # except ApiException as e: # print("Exception when calling UserApi->create_user: %s\n" % e) try: # Logs user into the system configuration.access_token = api_instance.login_user(username, password) print("Log in success! Token: %s\n" % configuration.access_token) except ApiException as e: print("Exception when calling UserApi->login_user: %s\n" % e) # create model api_instance = mint_client.ModelApi(mint_client.ApiClient(configuration)) # create an instance of the API class model = { "description": "SSCYP is built upon deep learning of past harvest data and satellite images of a certain area, it is able to predict the yield of a given crop in a given region based on its current satellite images of at least 6 days of time span. The current model is trained for South Sudan and pre-trained on Ethiopia, the prediction can be of the same region, or it can be of neighboring countries or regions with similar underlying distributions.", "hasDocumentation": [ "https://cloud.docker.com/repository/docker/minyinsri/crop-yield-transfer-learning-cpu" ], "hasModelCategory": [ "Agriculture" ], "hasSoftwareVersion": [ { "id": "SSCYP_1.0" } ], "id": "SSCYP",