20
20
from typing import Dict , Optional , Union
21
21
22
22
import openvino
23
- from huggingface_hub import HfApi , hf_hub_download
24
- from huggingface_hub .constants import HUGGINGFACE_HUB_CACHE
23
+ from huggingface_hub import HfApi , hf_hub_download , try_to_load_from_cache
24
+ from huggingface_hub .constants import HF_HUB_OFFLINE , HUGGINGFACE_HUB_CACHE
25
25
from openvino import Core , convert_model
26
26
from openvino ._offline_transformations import apply_moc_transformations , compress_model_transformation
27
27
from transformers import AutoConfig , GenerationConfig , PretrainedConfig
@@ -608,7 +608,7 @@ def from_pretrained(
608
608
)
609
609
610
610
if export is None :
611
- export = cls ._check_export_status (model_id , revision , subfolder )
611
+ export = cls ._check_export_status (model_id , revision , subfolder , cache_dir , local_files_only or HF_HUB_OFFLINE )
612
612
613
613
if not export and trust_remote_code :
614
614
logger .warning (
@@ -633,7 +633,14 @@ def from_pretrained(
633
633
)
634
634
635
635
@classmethod
636
- def _check_export_status (cls , model_id : Union [str , Path ], revision : Optional [str ] = None , subfolder : str = "" ):
636
+ def _check_export_status (
637
+ cls ,
638
+ model_id : Union [str , Path ],
639
+ revision : Optional [str ] = None ,
640
+ subfolder : str = "" ,
641
+ cache_dir : str = HUGGINGFACE_HUB_CACHE ,
642
+ local_files_only : bool = HF_HUB_OFFLINE ,
643
+ ):
637
644
model_dir = Path (model_id )
638
645
if subfolder is not None :
639
646
model_dir = model_dir / subfolder
@@ -642,17 +649,32 @@ def _check_export_status(cls, model_id: Union[str, Path], revision: Optional[str
642
649
not (model_dir / OV_XML_FILE_NAME ).exists ()
643
650
or not (model_dir / OV_XML_FILE_NAME .replace (".xml" , ".bin" )).exists ()
644
651
)
645
-
646
- hf_api = HfApi ()
647
- try :
648
- model_info = hf_api .model_info (model_id , revision = revision or "main" )
649
- normalized_subfolder = None if subfolder is None else Path (subfolder ).as_posix ()
650
- model_files = [
651
- file .rfilename
652
- for file in model_info .siblings
653
- if normalized_subfolder is None or file .rfilename .startswith (normalized_subfolder )
654
- ]
655
- ov_model_path = OV_XML_FILE_NAME if subfolder is None else f"{ normalized_subfolder } /{ OV_XML_FILE_NAME } "
656
- return ov_model_path not in model_files or ov_model_path .replace (".xml" , ".bin" ) not in model_files
657
- except Exception :
658
- return True
652
+ normalized_subfolder = None if subfolder is None else Path (subfolder ).as_posix ()
653
+ ov_model_path = OV_XML_FILE_NAME if subfolder is None else f"{ normalized_subfolder } /{ OV_XML_FILE_NAME } "
654
+ cache_model_path = try_to_load_from_cache (
655
+ model_id , ov_model_path , cache_dir = cache_dir , revision = revision or "main" , repo_type = "model"
656
+ )
657
+ cache_bin_path = try_to_load_from_cache (
658
+ model_id ,
659
+ ov_model_path .replace (".xml" , ".bin" ),
660
+ cache_dir = cache_dir ,
661
+ revision = revision or "main" ,
662
+ repo_type = "model" ,
663
+ )
664
+ cache_status = cache_bin_path is not None and cache_model_path is not None
665
+
666
+ if not cache_status and not local_files_only :
667
+ hf_api = HfApi ()
668
+ try :
669
+ model_info = hf_api .model_info (model_id , revision = revision or "main" )
670
+ model_files = [
671
+ file .rfilename
672
+ for file in model_info .siblings
673
+ if normalized_subfolder is None or file .rfilename .startswith (normalized_subfolder )
674
+ ]
675
+ ov_model_path = OV_XML_FILE_NAME if subfolder is None else f"{ normalized_subfolder } /{ OV_XML_FILE_NAME } "
676
+ return ov_model_path not in model_files or ov_model_path .replace (".xml" , ".bin" ) not in model_files
677
+ except Exception :
678
+ return True
679
+
680
+ return not cache_status
0 commit comments