|
|
from huggingface_hub import HfApi, hf_hub_url |
|
|
|
|
|
def get_hf_files_with_links(repo_id): |
|
|
""" |
|
|
获取Hugging Face仓库中的文件列表(文件名和完整链接),排除.gitattributes文件 |
|
|
|
|
|
Args: |
|
|
repo_id (str): Hugging Face仓库ID,格式为"用户名/仓库名" |
|
|
|
|
|
Returns: |
|
|
list: 包含字典的列表,每个字典包含'title'和'link'键 |
|
|
例如: [{'title': 'model.bin', 'link': 'https://huggingface.co/...'}, ...] |
|
|
|
|
|
Raises: |
|
|
ValueError: 当获取文件列表失败时抛出异常 |
|
|
""" |
|
|
try: |
|
|
|
|
|
api = HfApi() |
|
|
|
|
|
|
|
|
files = api.list_repo_files(repo_id=repo_id, repo_type="model") |
|
|
|
|
|
|
|
|
file_list = [] |
|
|
for file_path in files: |
|
|
|
|
|
if file_path != ".gitattributes": |
|
|
|
|
|
file_url = hf_hub_url(repo_id=repo_id, filename=file_path) |
|
|
file_list.append({ |
|
|
'title': file_path, |
|
|
'link': file_url |
|
|
}) |
|
|
|
|
|
return file_list |
|
|
|
|
|
except Exception as e: |
|
|
raise ValueError(f"获取文件列表失败: {str(e)}") |