Spaces:
Runtime error
Runtime error
#!/usr/bin/env python | |
from __future__ import annotations | |
import pathlib | |
import tempfile | |
import git | |
import tqdm.auto | |
import yaml | |
repo_dir = pathlib.Path(__file__).parents[1] | |
yaml_path = repo_dir / 'list.yaml' | |
def add_creation_timestamps() -> None: | |
with open(yaml_path) as f: | |
data = yaml.safe_load(f) | |
space_ids = list(data.keys()) | |
for space_id in tqdm.auto.tqdm(space_ids): | |
info = data[space_id] | |
if 'created' in info: | |
continue | |
with tempfile.TemporaryDirectory() as temp_dir: | |
url = f'https://huggingface.co/spaces/{space_id}' | |
repo = git.Repo.clone_from(url, temp_dir) | |
commits = list(repo.iter_commits()) | |
initial_commit = commits[-1] | |
date = initial_commit.authored_datetime | |
date_str = date.strftime('%Y-%m-%dT%H:%M:%S.000Z') | |
info['created'] = date_str | |
with open(yaml_path, 'w') as f: | |
yaml.dump(data, f) | |
def sort_by_creation_date() -> None: | |
with open(yaml_path) as f: | |
data = yaml.safe_load(f) | |
keys = sorted(data.keys(), key=lambda x: data[x]['created']) | |
sorted_data = dict() | |
for key in keys[::-1]: | |
sorted_data[key] = data[key] | |
with open(yaml_path, 'w') as f: | |
yaml.dump(sorted_data, f, sort_keys=False) | |
if __name__ == '__main__': | |
add_creation_timestamps() | |
sort_by_creation_date() | |