mirror of
https://github.com/gradio-app/gradio.git
synced 2025-04-12 12:40:29 +08:00
Delete spaces created on PRs after a certain amount of time (#2249)
* Add script to delete spaces * Add workflow * See if required=false fixes it * Fix input param * Run again * Test delete 2 * Delete for real * Fix typo * Undo changes for testing * pass api
This commit is contained in:
parent
361e461b97
commit
5e7f5a0f6b
35
.github/workflows/delete-stale-spaces.yml
vendored
Normal file
35
.github/workflows/delete-stale-spaces.yml
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
name: Delete Stale Spaces
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 0 * * *'
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
daysStale:
|
||||
description: 'How stale a space needs to be to be deleted (days)'
|
||||
required: true
|
||||
default: '7'
|
||||
|
||||
|
||||
jobs:
|
||||
delete-old-spaces:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Install Python
|
||||
uses: actions/setup-python@v3
|
||||
with:
|
||||
python-version: '3.9'
|
||||
- name: Install pip
|
||||
run: python -m pip install pip wheel requests
|
||||
- name: Install Hub Client Library
|
||||
run: pip install huggingface-hub==0.9.1
|
||||
- name: Set daysStale
|
||||
env:
|
||||
DEFAULT_DAYS_STALE: '7'
|
||||
run: echo "DAYS_STALE=${{ github.event.inputs.daysStale || env.DEFAULT_DAYS_STALE }}" >> $GITHUB_ENV
|
||||
- name: Find and delete stale spaces
|
||||
run: |
|
||||
python scripts/delete_old_spaces.py $DAYS_STALE \
|
||||
gradio-pr-deploys \
|
||||
${{ secrets.SPACES_DEPLOY_TOKEN }}
|
47
scripts/delete_old_spaces.py
Normal file
47
scripts/delete_old_spaces.py
Normal file
@ -0,0 +1,47 @@
|
||||
import argparse
|
||||
import datetime
|
||||
from typing import Optional
|
||||
|
||||
from huggingface_hub import HfApi
|
||||
|
||||
|
||||
def delete_space(space_id: str, hf_token: str, api_client: Optional[HfApi] = None):
|
||||
api_client = api_client or HfApi()
|
||||
api_client.delete_repo(repo_id=space_id, token=hf_token, repo_type="space")
|
||||
|
||||
|
||||
def get_spaces_to_delete(
|
||||
n_days: int, org_name: str, api_client: Optional[HfApi] = None
|
||||
):
|
||||
api_client = api_client or HfApi()
|
||||
spaces = api.list_spaces(author=org_name)
|
||||
spaces_to_delete = []
|
||||
for space in spaces:
|
||||
last_modified = api_client.space_info(space.id).lastModified
|
||||
age = (
|
||||
datetime.datetime.now()
|
||||
- datetime.datetime.fromisoformat(last_modified.rsplit(".", 1)[0])
|
||||
).days
|
||||
if age > n_days:
|
||||
spaces_to_delete.append(space.id)
|
||||
return spaces_to_delete
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Upload a demo to a space")
|
||||
parser.add_argument(
|
||||
"n_days",
|
||||
type=int,
|
||||
help="Spaces older than n_days will be automatically deleted",
|
||||
)
|
||||
parser.add_argument(
|
||||
"org_name", type=str, help="Name of the author/org to search in"
|
||||
)
|
||||
parser.add_argument("hf_token", type=str, help="HF API token")
|
||||
args = parser.parse_args()
|
||||
api = HfApi()
|
||||
|
||||
to_delete = get_spaces_to_delete(args.n_days, args.org_name, api_client=api)
|
||||
for space in to_delete:
|
||||
print(f"Deleting {space}")
|
||||
delete_space(space, args.hf_token, api_client=api)
|
Loading…
x
Reference in New Issue
Block a user