Upload manage branches.py
Browse filesCreate or delete branches easily
- manage branches.py +89 -0
manage branches.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#import required modules
|
| 2 |
+
import os
|
| 3 |
+
import huggingface_hub
|
| 4 |
+
from huggingface_hub import create_branch, delete_branch
|
| 5 |
+
|
| 6 |
+
#set clear screen function
|
| 7 |
+
def clear_screen():
|
| 8 |
+
os.system('cls' if os.name == 'nt' else 'clear')
|
| 9 |
+
|
| 10 |
+
#clear screen before starting
|
| 11 |
+
clear_screen()
|
| 12 |
+
|
| 13 |
+
#get user variables
|
| 14 |
+
while True:
|
| 15 |
+
cord = input("What would you like to do? (create) (delete): ").lower()
|
| 16 |
+
|
| 17 |
+
if cord not in ['create', 'delete']:
|
| 18 |
+
clear_screen()
|
| 19 |
+
print("Please choose one of the two options.")
|
| 20 |
+
continue
|
| 21 |
+
|
| 22 |
+
break
|
| 23 |
+
clear_screen()
|
| 24 |
+
repo = input("Repository name: ")
|
| 25 |
+
clear_screen()
|
| 26 |
+
while True:
|
| 27 |
+
r_type = input("Repo type (model) (dataset) (space): ").lower()
|
| 28 |
+
|
| 29 |
+
if r_type not in ['model', 'dataset', 'space']:
|
| 30 |
+
clear_screen()
|
| 31 |
+
print("Please choose one of the three options.")
|
| 32 |
+
continue
|
| 33 |
+
|
| 34 |
+
break
|
| 35 |
+
clear_screen()
|
| 36 |
+
branch = input("New branch name (No spaces): ")
|
| 37 |
+
clear_screen()
|
| 38 |
+
|
| 39 |
+
#get token
|
| 40 |
+
if 'HF_TOKEN' in os.environ:
|
| 41 |
+
#if the variable is found then write it to hf_token:
|
| 42 |
+
hf_token = os.environ['HF_TOKEN']
|
| 43 |
+
tfound = "Where are my doritos?"
|
| 44 |
+
else:
|
| 45 |
+
#if the variable is not found then prompt user to provide it:
|
| 46 |
+
hf_token = input("HF_TOKEN Variable not detected. Enter your HuggingFace (WRITE) token: ")
|
| 47 |
+
tfound = "false"
|
| 48 |
+
|
| 49 |
+
#login
|
| 50 |
+
huggingface_hub.login(hf_token)
|
| 51 |
+
|
| 52 |
+
#create or delete the branch
|
| 53 |
+
if cord == 'create':
|
| 54 |
+
create_branch(repo, repo_type=r_type, branch=branch)
|
| 55 |
+
else:
|
| 56 |
+
delete_branch(repo, repo_type=r_type, branch=branch)
|
| 57 |
+
|
| 58 |
+
#extra information
|
| 59 |
+
clear_screen()
|
| 60 |
+
#won't work if special characters are used but should still successfully be created/deleted
|
| 61 |
+
if cord == 'create':
|
| 62 |
+
if r_type == 'model':
|
| 63 |
+
print(f"Branch created at https://huggingface.co/{repo}/tree/{branch}")
|
| 64 |
+
elif r_type == 'dataset':
|
| 65 |
+
print(f"Branch created at https://huggingface.co/datasets/{repo}/tree/{branch}")
|
| 66 |
+
elif r_type == 'space':
|
| 67 |
+
print(f"Branch created at https://huggingface.co/spaces/{repo}/tree/{branch}")
|
| 68 |
+
else:
|
| 69 |
+
if r_type == 'model':
|
| 70 |
+
print(f"Branch deleted on {r_type} https://huggingface.co/{repo}")
|
| 71 |
+
elif r_type == 'dataset':
|
| 72 |
+
print(f"Branch deleted on {r_type} https://huggingface.co/datasets/{repo}")
|
| 73 |
+
elif r_type == 'space':
|
| 74 |
+
print(f"Branch deleted on {r_type} https://huggingface.co/spaces/{repo}")
|
| 75 |
+
#if token wasn't found then display following text:
|
| 76 |
+
if tfound == 'false':
|
| 77 |
+
print('''
|
| 78 |
+
Set HF_TOKEN to a token with WRITE permissions to skip inputting token on each run.
|
| 79 |
+
|
| 80 |
+
On Unix systems, edit the file ~/.bashrc with an editor of your choise.
|
| 81 |
+
On a new line add: export HF_TOKEN=Your-HuggingFace-token-here
|
| 82 |
+
(Terminal Refresh Required)
|
| 83 |
+
To temporarily set a token to the active terminal use 'export HF_TOKEN=Your-HuggingFace-token-here'
|
| 84 |
+
|
| 85 |
+
On Windows use 'setx HF_TOKEN Your-HuggingFace-token-here'
|
| 86 |
+
(System Restart Required)
|
| 87 |
+
To temporarily set a token to the active terminal use 'set HF_TOKEN=Your-HuggingFace-token-here'
|
| 88 |
+
''')
|
| 89 |
+
input("Press enter to continue.")
|