Spaces:
Build error
Build error
Upload hf_utils.py
Browse files- hf_utils.py +37 -0
hf_utils.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import get_hf_file_metadata, hf_hub_url, hf_hub_download, scan_cache_dir, whoami, list_models
|
2 |
+
|
3 |
+
|
4 |
+
def get_my_model_names(token):
|
5 |
+
|
6 |
+
try:
|
7 |
+
author = whoami(token=token)
|
8 |
+
model_infos = list_models(author=author["name"], use_auth_token=token)
|
9 |
+
return [model.modelId for model in model_infos], None
|
10 |
+
|
11 |
+
except Exception as e:
|
12 |
+
return [], e
|
13 |
+
|
14 |
+
def download_file(repo_id: str, filename: str, token: str):
|
15 |
+
"""Download a file from a repo on the Hugging Face Hub.
|
16 |
+
|
17 |
+
Returns:
|
18 |
+
file_path (:obj:`str`): The path to the downloaded file.
|
19 |
+
revision (:obj:`str`): The commit hash of the file.
|
20 |
+
"""
|
21 |
+
|
22 |
+
md = get_hf_file_metadata(hf_hub_url(repo_id=repo_id, filename=filename), token=token)
|
23 |
+
revision = md.commit_hash
|
24 |
+
|
25 |
+
file_path = hf_hub_download(repo_id=repo_id, filename=filename, revision=revision, token=token)
|
26 |
+
|
27 |
+
return file_path, revision
|
28 |
+
|
29 |
+
def delete_file(revision: str):
|
30 |
+
"""Delete a file from local cache.
|
31 |
+
|
32 |
+
Args:
|
33 |
+
revision (:obj:`str`): The commit hash of the file.
|
34 |
+
Returns:
|
35 |
+
None
|
36 |
+
"""
|
37 |
+
scan_cache_dir().delete_revisions(revision).execute()
|