3
from typing import Optional
5
from huggingface_hub import HfApi
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")
13
def get_spaces_to_delete(
14
n_days: int, org_name: str, api_client: Optional[HfApi] = None
16
api_client = api_client or HfApi()
17
spaces = api.list_spaces(author=org_name)
20
last_modified = api_client.space_info(space.id).lastModified
22
datetime.datetime.now()
23
- datetime.datetime.fromisoformat(last_modified.rsplit(".", 1)[0])
26
spaces_to_delete.append(space.id)
27
return spaces_to_delete
30
if __name__ == "__main__":
31
parser = argparse.ArgumentParser(description="Upload a demo to a space")
35
help="Spaces older than n_days will be automatically deleted",
38
"org_name", type=str, help="Name of the author/org to search in"
40
parser.add_argument("hf_token", type=str, help="HF API token")
41
args = parser.parse_args()
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)