11#!/usr/bin/env python
2- # Copyright (c) 2024 Oracle and/or its affiliates.
2+ # Copyright (c) 2025 Oracle and/or its affiliates.
33# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
44import os
55import pathlib
@@ -160,7 +160,7 @@ def create(
160160 target_compartment = compartment_id or COMPARTMENT_OCID
161161
162162 if service_model .compartment_id != ODSC_MODEL_COMPARTMENT_OCID :
163- logger .debug (
163+ logger .info (
164164 f"Aqua Model { model_id } already exists in user's compartment."
165165 "Skipped copying."
166166 )
@@ -191,8 +191,8 @@ def create(
191191 # TODO: decide what kwargs will be needed.
192192 .create (model_by_reference = True , ** kwargs )
193193 )
194- logger .debug (
195- f"Aqua Model { custom_model .id } created with the service model { model_id } "
194+ logger .info (
195+ f"Aqua Model { custom_model .id } created with the service model { model_id } . "
196196 )
197197
198198 # tracks unique models that were created in the user compartment
@@ -223,11 +223,16 @@ def get(self, model_id: str, load_model_card: Optional[bool] = True) -> "AquaMod
223223
224224 cached_item = self ._service_model_details_cache .get (model_id )
225225 if cached_item :
226+ logger .info (f"Fetching model details for model { model_id } from cache." )
226227 return cached_item
227228
229+ logger .info (f"Fetching model details for model { model_id } ." )
228230 ds_model = DataScienceModel .from_id (model_id )
229231 if not self ._if_show (ds_model ):
230- raise AquaRuntimeError (f"Target model `{ ds_model .id } `is not Aqua model." )
232+ raise AquaRuntimeError (
233+ f"Target model `{ ds_model .id } `is not an Aqua model as it does not contain "
234+ f"{ Tags .AQUA_TAG } tag."
235+ )
231236
232237 is_fine_tuned_model = bool (
233238 ds_model .freeform_tags
@@ -246,16 +251,21 @@ def get(self, model_id: str, load_model_card: Optional[bool] = True) -> "AquaMod
246251 ds_model .custom_metadata_list ._to_oci_metadata ()
247252 )
248253 if artifact_path != UNKNOWN :
254+ model_card_path = (
255+ f"{ artifact_path .rstrip ('/' )} /config/{ README } "
256+ if is_verified_type
257+ else f"{ artifact_path .rstrip ('/' )} /{ README } "
258+ )
249259 model_card = str (
250260 read_file (
251- file_path = (
252- f"{ artifact_path .rstrip ('/' )} /config/{ README } "
253- if is_verified_type
254- else f"{ artifact_path .rstrip ('/' )} /{ README } "
255- ),
261+ file_path = model_card_path ,
256262 auth = default_signer (),
257263 )
258264 )
265+ if not model_card :
266+ logger .warn (
267+ f"Model card for { model_id } is empty or could not be loaded from { model_card_path } ."
268+ )
259269
260270 inference_container = ds_model .custom_metadata_list .get (
261271 ModelCustomMetadataFields .DEPLOYMENT_CONTAINER ,
@@ -301,9 +311,10 @@ def get(self, model_id: str, load_model_card: Optional[bool] = True) -> "AquaMod
301311 try :
302312 jobrun_ocid = ds_model .provenance_metadata .training_id
303313 jobrun = self .ds_client .get_job_run (jobrun_ocid ).data
304- except Exception :
314+ except Exception as e :
305315 logger .debug (
306316 f"Missing jobrun information in the provenance metadata of the given model { model_id } ."
317+ f"\n Error: { str (e )} "
307318 )
308319 jobrun = None
309320
@@ -312,15 +323,21 @@ def get(self, model_id: str, load_model_card: Optional[bool] = True) -> "AquaMod
312323 FineTuningCustomMetadata .FT_SOURCE
313324 ).value
314325 except ValueError as e :
315- logger .debug (str (e ))
326+ logger .debug (
327+ f"Custom metadata is missing { FineTuningCustomMetadata .FT_SOURCE } key for "
328+ f"model { model_id } .\n Error: { str (e )} "
329+ )
316330 source_id = UNKNOWN
317331
318332 try :
319333 source_name = ds_model .custom_metadata_list .get (
320334 FineTuningCustomMetadata .FT_SOURCE_NAME
321335 ).value
322336 except ValueError as e :
323- logger .debug (str (e ))
337+ logger .debug (
338+ f"Custom metadata is missing { FineTuningCustomMetadata .FT_SOURCE_NAME } key for "
339+ f"model { model_id } .\n Error: { str (e )} "
340+ )
324341 source_name = UNKNOWN
325342
326343 source_identifier = _build_resource_identifier (
@@ -370,6 +387,7 @@ def delete_model(self, model_id):
370387 Tags .AQUA_FINE_TUNED_MODEL_TAG , None
371388 )
372389 if is_registered_model or is_fine_tuned_model :
390+ logger .info (f"Deleting model { model_id } ." )
373391 return ds_model .delete ()
374392 else :
375393 raise AquaRuntimeError (
@@ -447,6 +465,7 @@ def edit_registered_model(self, id, inference_container, enable_finetuning, task
447465 freeform_tags = freeform_tags ,
448466 )
449467 AquaApp ().update_model (id , update_model_details )
468+ logger .info (f"Updated model details for the model { id } ." )
450469 else :
451470 raise AquaRuntimeError (
452471 f"Failed to edit model:{ id } . Only registered unverified models can be edited."
@@ -706,7 +725,7 @@ def list(
706725 )
707726
708727 logger .info (
709- f"Fetch { len (models )} model in compartment_id={ compartment_id or ODSC_MODEL_COMPARTMENT_OCID } ."
728+ f"Fetched { len (models )} model in compartment_id={ compartment_id or ODSC_MODEL_COMPARTMENT_OCID } ."
710729 )
711730
712731 aqua_models = []
@@ -736,10 +755,12 @@ def clear_model_list_cache(
736755 dict with the key used, and True if cache has the key that needs to be deleted.
737756 """
738757 res = {}
739- logger .info ("Clearing _service_models_cache" )
740758 with self ._cache_lock :
741759 if ODSC_MODEL_COMPARTMENT_OCID in self ._service_models_cache :
742760 self ._service_models_cache .pop (key = ODSC_MODEL_COMPARTMENT_OCID )
761+ logger .info (
762+ f"Cleared models cache for service compartment { ODSC_MODEL_COMPARTMENT_OCID } ."
763+ )
743764 res = {
744765 "key" : {
745766 "compartment_id" : ODSC_MODEL_COMPARTMENT_OCID ,
@@ -756,10 +777,10 @@ def clear_model_details_cache(self, model_id):
756777 dict with the key used, and True if cache has the key that needs to be deleted.
757778 """
758779 res = {}
759- logger .info (f"Clearing _service_model_details_cache for { model_id } " )
760780 with self ._cache_lock :
761781 if model_id in self ._service_model_details_cache :
762782 self ._service_model_details_cache .pop (key = model_id )
783+ logger .info (f"Clearing model details cache for model { model_id } ." )
763784 res = {"key" : {"model_id" : model_id }, "cache_deleted" : True }
764785
765786 return res
@@ -844,7 +865,8 @@ def _create_model_catalog_entry(
844865 metadata = ModelCustomMetadata ()
845866 if not inference_container :
846867 raise AquaRuntimeError (
847- f"Require Inference container information. Model: { model_name } does not have associated inference container defaults. Check docs for more information on how to pass inference container."
868+ f"Require Inference container information. Model: { model_name } does not have associated inference "
869+ f"container defaults. Check docs for more information on how to pass inference container."
848870 )
849871 metadata .add (
850872 key = AQUA_DEPLOYMENT_CONTAINER_METADATA_NAME ,
@@ -920,7 +942,7 @@ def _create_model_catalog_entry(
920942 artifact_path = metadata .get (MODEL_BY_REFERENCE_OSS_PATH_KEY ).value
921943 logger .info (
922944 f"Found model artifact in the service bucket. "
923- f"Using artifact from service bucket instead of { os_path } "
945+ f"Using artifact from service bucket instead of { os_path } . "
924946 )
925947
926948 # todo: implement generic copy_folder method
@@ -952,7 +974,7 @@ def _create_model_catalog_entry(
952974 .with_freeform_tags (** tags )
953975 .with_defined_tags (** (defined_tags or {}))
954976 ).create (model_by_reference = True )
955- logger .debug (model )
977+ logger .debug (f"Created model catalog entry for the model: \n { model } " )
956978 return model
957979
958980 @staticmethod
@@ -986,6 +1008,9 @@ def get_model_files(os_path: str, model_format: ModelFormat) -> List[str]:
9861008 model_files .extend (
9871009 list_os_files_with_extension (oss_path = os_path , extension = ".gguf" )
9881010 )
1011+ logger .debug (
1012+ f"Fetched { len (model_files )} model files from { os_path } for model format { model_format } ."
1013+ )
9891014 return model_files
9901015
9911016 @staticmethod
@@ -1028,6 +1053,9 @@ def get_hf_model_files(model_name: str, model_format: ModelFormat) -> List[str]:
10281053 elif extension == model_format .value :
10291054 model_files .append (model_sibling .rfilename )
10301055
1056+ logger .debug (
1057+ f"Fetched { len (model_files )} model files for the model { model_name } for model format { model_format } ."
1058+ )
10311059 return model_files
10321060
10331061 def _validate_model (
@@ -1118,6 +1146,9 @@ def _validate_model(
11181146 }
11191147 validation_result .tags = hf_tags
11201148 except Exception :
1149+ logger .debug (
1150+ f"Could not process tags from Hugging Face model details for model { model_name } ."
1151+ )
11211152 pass
11221153
11231154 validation_result .model_formats = model_formats
@@ -1329,13 +1360,19 @@ def _download_model_from_hf(
13291360 local_dir = os .path .join (os .path .expanduser ("~" ), "cached-model" )
13301361 local_dir = os .path .join (local_dir , model_name )
13311362 os .makedirs (local_dir , exist_ok = True )
1363+ logger .debug (
1364+ f"Downloading artifacts from Hugging Face to local directory { local_dir } ."
1365+ )
13321366 snapshot_download (
13331367 repo_id = model_name ,
13341368 local_dir = local_dir ,
13351369 allow_patterns = allow_patterns ,
13361370 ignore_patterns = ignore_patterns ,
13371371 )
13381372 # Upload to object storage and skip .cache/huggingface/ folder
1373+ logger .debug (
1374+ f"Uploading local artifacts from local directory { local_dir } to { os_path } ."
1375+ )
13391376 model_artifact_path = upload_folder (
13401377 os_path = os_path ,
13411378 local_dir = local_dir ,
@@ -1379,6 +1416,7 @@ def register(
13791416 import_model_details .model .startswith ("ocid" )
13801417 and "datasciencemodel" in import_model_details .model
13811418 ):
1419+ logger .info (f"Fetching details for model { import_model_details .model } ." )
13821420 verified_model = DataScienceModel .from_id (import_model_details .model )
13831421 else :
13841422 # If users passes model name, check if there is model with the same name in the service model catalog. If it is there, then use that model
@@ -1501,7 +1539,7 @@ def _rqs(self, compartment_id: str, model_type="FT", **kwargs):
15011539 elif model_type == ModelType .BASE :
15021540 filter_tag = Tags .BASE_MODEL_CUSTOM
15031541 else :
1504- raise ValueError (
1542+ raise AquaValueError (
15051543 f"Model of type { model_type } is unknown. The values should be in { ModelType .values ()} "
15061544 )
15071545
@@ -1541,7 +1579,10 @@ def load_license(self, model_id: str) -> AquaModelLicense:
15411579 oci_model = self .ds_client .get_model (model_id ).data
15421580 artifact_path = get_artifact_path (oci_model .custom_metadata_list )
15431581 if not artifact_path :
1544- raise AquaRuntimeError ("Failed to get artifact path from custom metadata." )
1582+ raise AquaRuntimeError (
1583+ f"License could not be loaded. Failed to get artifact path from custom metadata for"
1584+ f"the model { model_id } ."
1585+ )
15451586
15461587 content = str (
15471588 read_file (
@@ -1572,6 +1613,9 @@ def _find_matching_aqua_model(self, model_id: str) -> Optional[str]:
15721613
15731614 for aqua_model_summary in aqua_model_list :
15741615 if aqua_model_summary .name .lower () == model_id_lower :
1616+ logger .info (
1617+ f"Found matching verified model id { aqua_model_summary .id } for the model { model_id } "
1618+ )
15751619 return aqua_model_summary .id
15761620
15771621 return None
0 commit comments