Anthonyg5005 commited on
Commit
0e9f590
·
verified ·
1 Parent(s): eefccd1

Multiple fixes and changes

Browse files

1. Shouldn't error on non posix (linux)/nt (windows) systems when clearing screen (in theory)
2. Added shortcuts when typing in required variables
3. Added revision cloning
4. Grammar fixes
5. Updated confirmation message
6. Check if running in Kaggle environment (Not getting token secret yet)
7. Check if HF_TOKEN is set to read only then exit

Files changed (1) hide show
  1. manage branches.py +80 -17
manage branches.py CHANGED
@@ -3,8 +3,14 @@ import os
3
  from huggingface_hub import create_branch, delete_branch, login, get_token, whoami
4
 
5
  #define clear screen function
 
 
 
 
 
 
6
  def clear_screen():
7
- os.system('cls' if os.name == 'nt' else 'clear')
8
 
9
  #clear screen before starting
10
  clear_screen()
@@ -14,28 +20,44 @@ clear_screen()
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 following two options.")
20
  continue
 
 
 
 
21
  break
22
  clear_screen()
23
- #name of effected repository
24
  repo = input("Repository name (User/Repo): ")
25
  clear_screen()
26
  #type of huggingface repository (restricted)
27
  while True:
28
  r_type = input("Repo type (model) (dataset) (space): ").lower()
29
 
30
- if r_type not in ['model', 'dataset', 'space']:
31
  clear_screen()
32
  print("Please choose one of the following three options.")
33
  continue
 
 
 
 
 
 
34
  break
35
  clear_screen()
36
  #name of created or deleted branch
37
  branch = input("Branch name (No spaces): ")
38
  clear_screen()
 
 
 
 
 
 
39
 
40
  #get token
41
  if get_token() is not None:
@@ -43,6 +65,12 @@ if get_token() is not None:
43
  login(get_token())
44
  tfound = "Where are my doritos?"
45
  else:
 
 
 
 
 
 
46
  #if the token is not found then prompt user to provide it:
47
  login(input("API token not detected. Enter your HuggingFace (WRITE) token: "))
48
  tfound = "false"
@@ -51,33 +79,67 @@ else:
51
  while True:
52
  if whoami().get('auth', {}).get('accessToken', {}).get('role', None) != 'write':
53
  clear_screen()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  print("You do not have write access to this repository. Please use a valid token with (WRITE) access.")
55
  login(input("Enter your HuggingFace (WRITE) token: "))
56
  continue
57
  break
58
 
59
  clear_screen()
60
- #prompt the user for confirmation on creation/deletion of the branch
61
- while True:
62
- yorn = input(f"Are you sure you want to {cord} branch '{branch}' in {repo} (Y/n): ").lower()
63
- if yorn == '':
64
- yorn = 'y'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  break
66
- else:
67
- if yorn not in ['y', 'n']:
68
- clear_screen()
69
- print("Please choose one of the following two options carefully.")
70
- continue
71
- break
72
  clear_screen()
73
 
74
  #create or delete the branch
 
75
  if yorn == 'y':
76
  if cord == 'create':
77
- create_branch(repo, repo_type=r_type, branch=branch)
78
  else:
79
  delete_branch(repo, repo_type=r_type, branch=branch)
80
  else:
 
81
  exit()
82
  clear_screen()
83
 
@@ -97,7 +159,7 @@ else:
97
  print(f"Branch {branch} deleted on {r_type} https://huggingface.co/datasets/{repo}")
98
  elif r_type == 'space':
99
  print(f"Branch {branch} deleted on {r_type} https://huggingface.co/spaces/{repo}")
100
- #if token wasn't found from line 36 then display following text:
101
  if tfound == 'false':
102
  print(f'''
103
  You are now logged in as {whoami().get('fullname', None)}.
@@ -105,4 +167,5 @@ if tfound == 'false':
105
  To logout, use the hf command line interface 'huggingface-cli logout'
106
  To view your active account, use 'huggingface-cli whoami'
107
  ''')
 
108
  input("Press enter to continue.")
 
3
  from huggingface_hub import create_branch, delete_branch, login, get_token, whoami
4
 
5
  #define clear screen function
6
+ if os.name == 'nt':
7
+ osclear = 'cls'
8
+ elif os.name == 'posix':
9
+ osclear = 'clear'
10
+ else:
11
+ osclear = ''
12
  def clear_screen():
13
+ os.system(osclear)
14
 
15
  #clear screen before starting
16
  clear_screen()
 
20
  while True:
21
  cord = input("What would you like to do? (create) (delete): ").lower()
22
 
23
+ if cord not in ['create', 'delete', 'c', 'd']:
24
  clear_screen()
25
  print("Please choose one of the following two options.")
26
  continue
27
+ if cord == 'c':
28
+ cord = 'create'
29
+ elif cord == 'd':
30
+ cord = 'delete'
31
  break
32
  clear_screen()
33
+ #name of affected repository
34
  repo = input("Repository name (User/Repo): ")
35
  clear_screen()
36
  #type of huggingface repository (restricted)
37
  while True:
38
  r_type = input("Repo type (model) (dataset) (space): ").lower()
39
 
40
+ if r_type not in ['model', 'dataset', 'space', 'm', 'd', 's']:
41
  clear_screen()
42
  print("Please choose one of the following three options.")
43
  continue
44
+ if r_type == 'm':
45
+ r_type = 'model'
46
+ elif r_type == 'd':
47
+ r_type = 'dataset'
48
+ elif r_type == 's':
49
+ r_type = 'space'
50
  break
51
  clear_screen()
52
  #name of created or deleted branch
53
  branch = input("Branch name (No spaces): ")
54
  clear_screen()
55
+ #promt user for revision, or clone from main
56
+ if cord == 'create':
57
+ rev = input("Revision to clone from (Can be a branch name or the OID/SHA of a commit) (Empty clones main): ")
58
+ if rev == '':
59
+ rev = 'main'
60
+ clear_screen()
61
 
62
  #get token
63
  if get_token() is not None:
 
65
  login(get_token())
66
  tfound = "Where are my doritos?"
67
  else:
68
+ if os.environ.get('KAGGLE_KERNEL_RUN_TYPE', None) is not None: #check if user in kaggle
69
+ print('''
70
+ When using Kaggle, make sure to use the secret key HF_TOKEN with a 'WRITE' token.
71
+ This will prevent the need to login every time you run the script.
72
+ Set your secrets with the secrets add-on on the top of the screen.
73
+ ''')
74
  #if the token is not found then prompt user to provide it:
75
  login(input("API token not detected. Enter your HuggingFace (WRITE) token: "))
76
  tfound = "false"
 
79
  while True:
80
  if whoami().get('auth', {}).get('accessToken', {}).get('role', None) != 'write':
81
  clear_screen()
82
+ if os.environ.get('HF_TOKEN', None) is not None: #if environ finds HF_TOKEN as write then display following text and exit:
83
+ print(f'''
84
+ You have the environment variable HF_TOKEN set.
85
+ You cannot log in.
86
+ Either set the environment variable to a (WRITE) token or remove it.
87
+ ''')
88
+ exit()
89
+
90
+ if os.environ.get('KAGGLE_KERNEL_RUN_TYPE', None) is not None: #check if user in kaggle
91
+ print('''
92
+ When using Kaggle, make sure to use the secret key HF_TOKEN with a 'WRITE' token.
93
+ This will prevent the need to login every time you run the script.
94
+ Set your secrets with the secrets add-on on the top of the screen.
95
+ ''')
96
  print("You do not have write access to this repository. Please use a valid token with (WRITE) access.")
97
  login(input("Enter your HuggingFace (WRITE) token: "))
98
  continue
99
  break
100
 
101
  clear_screen()
102
+ if cord == 'delete':
103
+ #prompt the user for confirmation on deletion of the branch
104
+ while True:
105
+ yorn = input(f"Are you sure you want to remove branch '{branch}' in {repo}? (Y/n): ").lower()
106
+ if yorn == '':
107
+ yorn = 'y'
108
+ break
109
+ else:
110
+ if yorn not in ['y', 'n']:
111
+ clear_screen()
112
+ print("Please choose one of the following two options carefully.")
113
+ continue
114
+ break
115
+ else:
116
+ #prompt the user for confirmation on creation of the branch
117
+ while True:
118
+ yorn = input(f"Are you sure you want to clone revision '{rev}' to create branch '{branch}' in {repo}? (Y/n): ").lower()
119
+ if yorn == '':
120
+ yorn = 'y'
121
+ elif yorn == 'yes':
122
+ yorn = 'y'
123
+ elif yorn == 'no':
124
+ yorn = 'n'
125
+ break
126
+ else:
127
+ if yorn not in ['y', 'n']:
128
+ clear_screen()
129
+ print("Please choose one of the following two options carefully.")
130
+ continue
131
  break
 
 
 
 
 
 
132
  clear_screen()
133
 
134
  #create or delete the branch
135
+ #if user selected yes then continue, else exit
136
  if yorn == 'y':
137
  if cord == 'create':
138
+ create_branch(repo, revision=rev, repo_type=r_type, branch=branch)
139
  else:
140
  delete_branch(repo, repo_type=r_type, branch=branch)
141
  else:
142
+ print("Cancelled action")
143
  exit()
144
  clear_screen()
145
 
 
159
  print(f"Branch {branch} deleted on {r_type} https://huggingface.co/datasets/{repo}")
160
  elif r_type == 'space':
161
  print(f"Branch {branch} deleted on {r_type} https://huggingface.co/spaces/{repo}")
162
+ #if token wasn't found from line 60 then display following text:
163
  if tfound == 'false':
164
  print(f'''
165
  You are now logged in as {whoami().get('fullname', None)}.
 
167
  To logout, use the hf command line interface 'huggingface-cli logout'
168
  To view your active account, use 'huggingface-cli whoami'
169
  ''')
170
+
171
  input("Press enter to continue.")