Monkey-patch the `kernels` library to route HF API calls to ModelScope. Only `kernels.utils._get_hf_api` is replaced; every download, file check and ref listing performed by `kernels` goes through `_MsKernelApi`, so the kernel loading/variant/lock logic stays untouched.
()
| 518 | |
| 519 | |
| 520 | def _patch_kernels(): |
| 521 | """Monkey-patch the `kernels` library to route HF API calls to ModelScope. |
| 522 | |
| 523 | Only `kernels.utils._get_hf_api` is replaced; every download, file check |
| 524 | and ref listing performed by `kernels` goes through `_MsKernelApi`, so |
| 525 | the kernel loading/variant/lock logic stays untouched. |
| 526 | """ |
| 527 | try: |
| 528 | from kernels import utils as kernels_utils |
| 529 | from kernels.utils import _get_hf_api |
| 530 | except ImportError: |
| 531 | return |
| 532 | if hasattr(kernels_utils, '_get_hf_api_origin'): |
| 533 | return |
| 534 | kernels_utils._get_hf_api_origin = kernels_utils._get_hf_api |
| 535 | kernels_utils._get_hf_api = lambda user_agent=None: _MsKernelApi() |
| 536 | |
| 537 | |
| 538 | def _unpatch_kernels(): |
searching dependent graphs…