File size: 1,392 Bytes
e51e8f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7de579
 
 
e51e8f8
 
 
a7de579
e51e8f8
 
 
 
968af70
e51e8f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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()