utils.py 1.03 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
4
from os import PathLike
from pathlib import Path
5
from typing import List, Optional, Union
6
7


8
9
10
11
def is_s3(model_or_path: str) -> bool:
    return model_or_path.lower().startswith('s3://')


12
13
14
15
16
17
18
19
20
21
22
def check_gguf_file(model: Union[str, PathLike]) -> bool:
    """Check if the file is a GGUF model."""
    model = Path(model)
    if not model.is_file():
        return False
    elif model.suffix == ".gguf":
        return True

    with open(model, "rb") as f:
        header = f.read(4)
    return header == b"GGUF"
23
24
25
26
27
28
29
30
31
32


def modelscope_list_repo_files(
    repo_id: str,
    revision: Optional[str] = None,
    token: Union[str, bool, None] = None,
) -> List[str]:
    """List files in a modelscope repo."""
    from modelscope.hub.api import HubApi
    api = HubApi()
33
    api.login(token)
34
35
36
37
38
39
40
    # same as huggingface_hub.list_repo_files
    files = [
        file['Path'] for file in api.get_model_files(
            model_id=repo_id, revision=revision, recursive=True)
        if file['Type'] == 'blob'
    ]
    return files