Upload hf_account_collateral.py with huggingface_hub
Browse files- hf_account_collateral.py +23 -14
hf_account_collateral.py
CHANGED
|
@@ -15,7 +15,6 @@ from pathlib import Path
|
|
| 15 |
from typing import Dict, List
|
| 16 |
|
| 17 |
import requests
|
| 18 |
-
from huggingface_hub import HfApi
|
| 19 |
|
| 20 |
|
| 21 |
TEXT_EXTENSIONS = {
|
|
@@ -27,6 +26,7 @@ TEXT_EXTENSIONS = {
|
|
| 27 |
MAX_TEXT_FILE_BYTES = int(os.environ.get("HF_COLLATERAL_MAX_TEXT_FILE_BYTES", "1000000"))
|
| 28 |
FILE_FETCH_TIMEOUT = int(os.environ.get("HF_COLLATERAL_FILE_TIMEOUT_SECONDS", "10"))
|
| 29 |
REPO_TREE_TIMEOUT = int(os.environ.get("HF_COLLATERAL_TREE_TIMEOUT_SECONDS", "10"))
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
def _sha256_text(value: str) -> str:
|
|
@@ -84,31 +84,40 @@ def _repo_tree_files(repo_id: str, repo_type: str) -> List[str]:
|
|
| 84 |
return sorted(files)
|
| 85 |
|
| 86 |
|
| 87 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
records = []
|
| 89 |
-
for repo_type,
|
| 90 |
-
("model",
|
| 91 |
-
("dataset",
|
| 92 |
-
("space",
|
| 93 |
):
|
| 94 |
-
for item in
|
| 95 |
-
repo_id =
|
| 96 |
if not repo_id:
|
| 97 |
continue
|
| 98 |
records.append({
|
| 99 |
"repo_type": repo_type,
|
| 100 |
"repo_id": repo_id,
|
| 101 |
-
"likes":
|
| 102 |
-
"downloads":
|
| 103 |
-
"last_modified": str(
|
| 104 |
-
"sdk":
|
| 105 |
})
|
| 106 |
return sorted(records, key=lambda item: (item["repo_type"], item["repo_id"]))
|
| 107 |
|
| 108 |
|
| 109 |
def scan_hf_account_collateral(owner: str) -> Dict:
|
| 110 |
-
|
| 111 |
-
repos = _repo_records(api, owner)
|
| 112 |
scanned_repos = []
|
| 113 |
total_files = 0
|
| 114 |
total_text_files = 0
|
|
|
|
| 15 |
from typing import Dict, List
|
| 16 |
|
| 17 |
import requests
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
TEXT_EXTENSIONS = {
|
|
|
|
| 26 |
MAX_TEXT_FILE_BYTES = int(os.environ.get("HF_COLLATERAL_MAX_TEXT_FILE_BYTES", "1000000"))
|
| 27 |
FILE_FETCH_TIMEOUT = int(os.environ.get("HF_COLLATERAL_FILE_TIMEOUT_SECONDS", "10"))
|
| 28 |
REPO_TREE_TIMEOUT = int(os.environ.get("HF_COLLATERAL_TREE_TIMEOUT_SECONDS", "10"))
|
| 29 |
+
ACCOUNT_LIST_TIMEOUT = int(os.environ.get("HF_COLLATERAL_ACCOUNT_TIMEOUT_SECONDS", "10"))
|
| 30 |
|
| 31 |
|
| 32 |
def _sha256_text(value: str) -> str:
|
|
|
|
| 84 |
return sorted(files)
|
| 85 |
|
| 86 |
|
| 87 |
+
def _account_items(path: str, owner: str) -> List[Dict]:
|
| 88 |
+
response = requests.get(
|
| 89 |
+
f"https://huggingface.co/api/{path}",
|
| 90 |
+
params={"author": owner, "limit": 1000},
|
| 91 |
+
timeout=ACCOUNT_LIST_TIMEOUT,
|
| 92 |
+
)
|
| 93 |
+
response.raise_for_status()
|
| 94 |
+
return response.json()
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def _repo_records(owner: str) -> List[Dict]:
|
| 98 |
records = []
|
| 99 |
+
for repo_type, path in (
|
| 100 |
+
("model", "models"),
|
| 101 |
+
("dataset", "datasets"),
|
| 102 |
+
("space", "spaces"),
|
| 103 |
):
|
| 104 |
+
for item in _account_items(path, owner):
|
| 105 |
+
repo_id = item.get("id") or item.get("name")
|
| 106 |
if not repo_id:
|
| 107 |
continue
|
| 108 |
records.append({
|
| 109 |
"repo_type": repo_type,
|
| 110 |
"repo_id": repo_id,
|
| 111 |
+
"likes": item.get("likes", 0) or 0,
|
| 112 |
+
"downloads": item.get("downloads", 0) or 0,
|
| 113 |
+
"last_modified": str(item.get("lastModified") or item.get("updatedAt") or ""),
|
| 114 |
+
"sdk": item.get("sdk"),
|
| 115 |
})
|
| 116 |
return sorted(records, key=lambda item: (item["repo_type"], item["repo_id"]))
|
| 117 |
|
| 118 |
|
| 119 |
def scan_hf_account_collateral(owner: str) -> Dict:
|
| 120 |
+
repos = _repo_records(owner)
|
|
|
|
| 121 |
scanned_repos = []
|
| 122 |
total_files = 0
|
| 123 |
total_text_files = 0
|