diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml index b4cabf36..418cd3c4 100644 --- a/.github/workflows/sync.yml +++ b/.github/workflows/sync.yml @@ -9,11 +9,26 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + - name: Restore cache + id: file-restore + uses: actions/cache/restore@v3 + with: + path: | + sync/endpoint.txt + key: endpoint - uses: actions/setup-python@v4 with: python-version: '3.10' cache: 'pip' - name: run env: - HF_TOKEN: ${{ secrets.HF_TOKEN }} - run: python sync.py + HF_TOKEN: ${{ secrets.HF_TOKEN }} + ENDPOINTS: ${{ vars.ENDPOINTS }} + run: cd sync && cat endpoint.txt && pip install huggingface_hub && python sync.py + - name: Save cache + id: file-save + uses: actions/cache/save@v3 + with: + path: | + sync/endpoint.txt + key: ${{ steps.restore-cache.outputs.cache-primary-key }} diff --git a/sync.py b/sync.py deleted file mode 100644 index 083d7f02..00000000 --- a/sync.py +++ /dev/null @@ -1,11 +0,0 @@ -from huggingface_hub import HfApi, list_models -import os - -# Use root method -models = list_models() -hf_api = HfApi( - endpoint="https://huggingface.co", # Can be a Private Hub endpoint. - token=os.environ.get('HF_TOKEN'), # Token is not persisted on the machine. -) - -hf_api.add_space_secret('hf4all/bingo2', 'ENDPOINT', 'cf.github1s.tk') diff --git a/sync/.gitignore b/sync/.gitignore new file mode 100644 index 00000000..406bab72 --- /dev/null +++ b/sync/.gitignore @@ -0,0 +1,2 @@ +/endpoint.txt +/.env diff --git a/requirements.txt b/sync/requirements.txt similarity index 100% rename from requirements.txt rename to sync/requirements.txt diff --git a/sync/sync.py b/sync/sync.py new file mode 100644 index 00000000..c11ad81b --- /dev/null +++ b/sync/sync.py @@ -0,0 +1,25 @@ +from huggingface_hub import HfApi, list_models +import os + +hf_api = HfApi( + endpoint="https://huggingface.co", # Can be a Private Hub endpoint. + token=os.environ.get('HF_TOKEN'), # Token is not persisted on the machine. +) +ENDPOINTS = os.environ.get('ENDPOINTS') +file = 'endpoint.txt' +endpoints = ENDPOINTS.split('\n') +endpoint = endpoints[0].strip() +try: + with open(file,"r") as f: + content=f.read().strip() + endpoint = endpoints[1] if content == endpoints[0] else endpoints[0] + f.close() +except IOError: + print("File not accessible") + with open(file, "w") as wf: + wf.write(endpoint) + wf.close() + +print('new endpoint', endpoint) +hf_api.add_space_secret('hf4all/bingo2', 'ENDPOINT', endpoint.strip()) +