gradio

Форк
0
/
delete_old_spaces.py 
47 строк · 1.5 Кб
1
import argparse
2
import datetime
3
from typing import Optional
4

5
from huggingface_hub import HfApi
6

7

8
def delete_space(space_id: str, hf_token: str, api_client: Optional[HfApi] = None):
9
    api_client = api_client or HfApi()
10
    api_client.delete_repo(repo_id=space_id, token=hf_token, repo_type="space")
11

12

13
def get_spaces_to_delete(
14
    n_days: int, org_name: str, api_client: Optional[HfApi] = None
15
):
16
    api_client = api_client or HfApi()
17
    spaces = api.list_spaces(author=org_name)
18
    spaces_to_delete = []
19
    for space in spaces:
20
        last_modified = api_client.space_info(space.id).lastModified
21
        age = (
22
            datetime.datetime.now()
23
            - datetime.datetime.fromisoformat(last_modified.rsplit(".", 1)[0])
24
        ).days
25
        if age > n_days:
26
            spaces_to_delete.append(space.id)
27
    return spaces_to_delete
28

29

30
if __name__ == "__main__":
31
    parser = argparse.ArgumentParser(description="Upload a demo to a space")
32
    parser.add_argument(
33
        "n_days",
34
        type=int,
35
        help="Spaces older than n_days will be automatically deleted",
36
    )
37
    parser.add_argument(
38
        "org_name", type=str, help="Name of the author/org to search in"
39
    )
40
    parser.add_argument("hf_token", type=str, help="HF API token")
41
    args = parser.parse_args()
42
    api = HfApi()
43

44
    to_delete = get_spaces_to_delete(args.n_days, args.org_name, api_client=api)
45
    for space in to_delete:
46
        print(f"Deleting {space}")
47
        delete_space(space, args.hf_token, api_client=api)
48

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.