Spaces:
Running on Zero
Running on Zero
File size: 8,367 Bytes
f44aac9 4791c0a f44aac9 4791c0a f44aac9 d0718ca f44aac9 4791c0a d0718ca 4791c0a d0718ca 04ad98e f44aac9 4791c0a f44aac9 d0718ca f44aac9 d0718ca f44aac9 d0718ca 4791c0a f44aac9 d0718ca 4791c0a d0718ca 4791c0a d0718ca 4791c0a d0718ca f44aac9 04ad98e f44aac9 04ad98e d0718ca f44aac9 d0718ca f44aac9 d0718ca 4791c0a f44aac9 d0718ca 4791c0a d0718ca 4791c0a d0718ca f44aac9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | #!/usr/bin/env python3
from __future__ import annotations
import argparse
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
import json
import os
from pathlib import Path
from pathlib import PurePosixPath
import sys
from typing import Any
from urllib.parse import quote
import requests
from huggingface_hub import HfApi
from huggingface_hub.errors import EntryNotFoundError
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from hackathon_advisor.data import extract_app_file_embedding_text, normalize_project_tags
API = "https://huggingface.co/api"
README_BODY_CHAR_CAP = 24000
APP_FILE_SOURCE_CHAR_CAP = 24000
DOWNLOAD_TIMEOUT_SECONDS = (10, 45)
DOWNLOAD_ATTEMPTS = 2
DEFAULT_CRAWL_WORKERS = 12
def main() -> None:
parser = argparse.ArgumentParser(description="Snapshot public Spaces in a Hugging Face org.")
parser.add_argument("--org", default="build-small-hackathon")
parser.add_argument("--out", default="data/projects.json")
args = parser.parse_args()
projects = crawl_projects(args.org)
payload = {
"generated_at": datetime.now(timezone.utc).isoformat(timespec="seconds"),
"source": f"{API}/spaces?author={args.org}",
"projects": sorted(projects, key=lambda project: project["id"].lower()),
}
output = Path(args.out)
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text(json.dumps(payload, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
print(f"wrote {len(projects)} projects to {output}")
def crawl_projects(org: str) -> list[dict[str, Any]]:
api = HfApi(token=False)
spaces = [space for space in api.list_spaces(author=org, full=True, token=False)]
public_spaces = [space for space in spaces if not bool(getattr(space, "private", False))]
if not public_spaces:
return []
workers = min(crawl_workers(), len(public_spaces))
with ThreadPoolExecutor(max_workers=workers) as executor:
return list(executor.map(project_from_space, public_spaces))
def crawl_workers() -> int:
raw = os.environ.get("ADVISOR_CRAWL_WORKERS", "").strip()
if not raw:
return DEFAULT_CRAWL_WORKERS
try:
return max(1, int(raw))
except ValueError as error:
raise RuntimeError(f"ADVISOR_CRAWL_WORKERS must be an integer, got {raw!r}") from error
def project_from_space(space: Any) -> dict[str, Any]:
card = card_data(space)
space_id = str(space.id)
siblings = sibling_names(space)
readme = download_optional_repo_text(space_id, "README.md") if "README.md" in siblings else ""
readme_body = strip_frontmatter(readme)[:README_BODY_CHAR_CAP]
frontmatter = readme_frontmatter(readme)
app_file = validate_app_file(str(frontmatter.get("app_file") or ""), space_id=space_id)
app_file_embedding_text = ""
app_file_source = ""
if app_file:
app_file_source = download_optional_repo_text(space_id, app_file) if app_file in siblings else ""
if app_file_source:
app_file_source = app_file_source[:APP_FILE_SOURCE_CHAR_CAP]
app_file_embedding_text = extract_app_file_embedding_text(app_file, app_file_source)
title = str(card.get("title") or humanize_slug(space_id.rsplit("/", 1)[-1]))
summary = str(card.get("short_description") or card.get("description") or "")
raw_tags = sorted(set(str(tag) for tag in (card.get("tags") or getattr(space, "tags", None) or [])))
return {
"id": space_id,
"title": title,
"summary": summary,
"tags": list(normalize_project_tags(raw_tags)),
"models": [str(model) for model in getattr(space, "models", None) or card.get("models") or []],
"datasets": [
str(dataset) for dataset in getattr(space, "datasets", None) or card.get("datasets") or []
],
"likes": int(getattr(space, "likes", None) or 0),
"sdk": str(card.get("sdk") or getattr(space, "sdk", None) or ""),
"license": str(card.get("license") or ""),
"created_at": isoformat(getattr(space, "created_at", None)),
"last_modified": isoformat(getattr(space, "last_modified", None)),
"host": host_url(space),
"url": f"https://huggingface.co/spaces/{space_id}",
"app_file": app_file,
"app_file_embedding_text": app_file_embedding_text,
"readme_body": readme_body,
"app_file_source": app_file_source,
}
def card_data(space: Any) -> dict[str, Any]:
raw = getattr(space, "card_data", None) or getattr(space, "cardData", None) or {}
if isinstance(raw, dict):
return raw
to_dict = getattr(raw, "to_dict", None)
if callable(to_dict):
return dict(to_dict())
return {}
def sibling_names(space: Any) -> set[str]:
return {str(sibling.rfilename) for sibling in getattr(space, "siblings", None) or []}
def download_repo_text(repo_id: str, filename: str) -> str:
url = repo_file_url(repo_id, filename)
last_error: requests.RequestException | None = None
for _attempt in range(DOWNLOAD_ATTEMPTS):
try:
response = requests.get(url, timeout=DOWNLOAD_TIMEOUT_SECONDS)
if response.status_code == 404:
raise EntryNotFoundError(f"missing file: {repo_id}/{filename}")
response.raise_for_status()
response.encoding = response.encoding or "utf-8"
return response.text
except EntryNotFoundError:
raise
except requests.RequestException as error:
last_error = error
raise RuntimeError(
f"failed to download {repo_id}/{filename} after {DOWNLOAD_ATTEMPTS} attempts: {last_error}"
)
def download_optional_repo_text(repo_id: str, filename: str) -> str:
try:
return download_repo_text(repo_id, filename)
except EntryNotFoundError:
return ""
def repo_file_url(repo_id: str, filename: str) -> str:
return (
"https://huggingface.co/spaces/"
f"{quote(repo_id, safe='/')}/resolve/main/{quote(filename, safe='/')}"
)
def readme_frontmatter(readme: str) -> dict[str, str]:
lines = readme.splitlines()
if not lines or lines[0].strip() != "---":
return {}
values: dict[str, str] = {}
closed = False
for line in lines[1:]:
stripped = line.strip()
if stripped in {"---", "..."}:
closed = True
break
if not stripped or stripped.startswith("#") or ":" not in line:
continue
if line[:1].isspace() or stripped.startswith("-"):
continue
key, raw_value = line.split(":", 1)
key = key.strip()
if key:
values[key] = yaml_scalar(raw_value)
return values if closed else {}
def strip_frontmatter(readme: str) -> str:
lines = readme.splitlines()
if not lines or lines[0].strip() != "---":
return readme.strip()
for index in range(1, len(lines)):
if lines[index].strip() in {"---", "..."}:
return "\n".join(lines[index + 1 :]).strip()
return readme.strip()
def yaml_scalar(raw_value: str) -> str:
value = raw_value.strip()
if not value:
return ""
if " #" in value:
value = value.split(" #", 1)[0].rstrip()
if value[:1] in {"'", '"'} and value[-1:] == value[:1]:
return value[1:-1]
return value
def validate_app_file(app_file: str, *, space_id: str) -> str:
cleaned = app_file.strip()
if not cleaned:
return ""
path = PurePosixPath(cleaned)
if path.is_absolute() or ".." in path.parts or cleaned.endswith("/"):
raise RuntimeError(f"{space_id} README frontmatter has an invalid app_file path: {app_file}")
return path.as_posix()
def isoformat(value: Any) -> str:
if value is None:
return ""
formatter = getattr(value, "isoformat", None)
if callable(formatter):
return formatter()
return str(value)
def host_url(space: Any) -> str:
host = str(getattr(space, "host", None) or "")
if host:
return host
subdomain = str(getattr(space, "subdomain", None) or "")
return f"https://{subdomain}.hf.space" if subdomain else ""
def humanize_slug(slug: str) -> str:
return " ".join(part for part in slug.replace("_", "-").split("-") if part).title()
if __name__ == "__main__":
main()
|