Anthonyg5005 commited on
Commit
9561dd1
1 Parent(s): 42cf2bf

Token handled more securely and added confirmation

Browse files

not really tested but fixed any problems I encountered while using

Files changed (1) hide show
  1. manage branches.py +38 -23
manage branches.py CHANGED
@@ -2,14 +2,15 @@
2
  import os
3
  from huggingface_hub import create_branch, delete_branch, login, get_token, whoami
4
 
5
- #set 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()
11
 
12
- #get user variables
 
13
  while True:
14
  cord = input("What would you like to do? (create) (delete): ").lower()
15
 
@@ -19,8 +20,10 @@ while True:
19
  continue
20
  break
21
  clear_screen()
 
22
  repo = input("Repository name (User/Repo): ")
23
  clear_screen()
 
24
  while True:
25
  r_type = input("Repo type (model) (dataset) (space): ").lower()
26
 
@@ -30,43 +33,55 @@ while True:
30
  continue
31
  break
32
  clear_screen()
 
33
  branch = input("Branch name (No spaces): ")
34
  clear_screen()
35
 
36
  #get token
37
- if 'None' in str(get_token()):
 
 
 
 
38
  #if the token is not found then prompt user to provide it:
39
- hf_token = input("API token not detected. Enter your HuggingFace (WRITE) token: ")
40
  tfound = "false"
41
- else:
42
- #if the token is found then write it to hf_token:
43
- hf_token = get_token()
44
- tfound = "Where are my doritos?"
45
-
46
- #login
47
- login(hf_token)
48
 
49
  #if the token is read only then prompt user to provide a write token:
50
  while True:
51
  if whoami().get('auth', {}).get('accessToken', {}).get('role', None) != 'write':
52
  clear_screen()
53
  print("You do not have write access to this repository. Please use a valid token with (WRITE) access.")
54
- hf_token = input("Enter your HuggingFace (WRITE) token: ")
55
- login(hf_token)
56
  continue
57
  break
58
 
59
- #store the user's name
60
- fname = whoami().get('fullname', None)
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  #create or delete the branch
63
- if cord == 'create':
64
- create_branch(repo, repo_type=r_type, branch=branch)
 
 
 
65
  else:
66
- delete_branch(repo, repo_type=r_type, branch=branch)
67
-
68
- #extra information
69
  clear_screen()
 
 
70
  #won't work if special characters are used but should still successfully be created/deleted
71
  if cord == 'create':
72
  if r_type == 'model':
@@ -82,12 +97,12 @@ else:
82
  print(f"Branch {branch} deleted on {r_type} https://huggingface.co/datasets/{repo}")
83
  elif r_type == 'space':
84
  print(f"Branch {branch} deleted on {r_type} https://huggingface.co/spaces/{repo}")
85
- #if token wasn't found then display following text:
86
  if tfound == 'false':
87
  print(f'''
88
- You are now logged in as {fname}.
89
 
90
- To logout, use the cli 'huggingface-cli logout'
91
  To view your active account, use 'huggingface-cli whoami'
92
  ''')
93
  input("Press enter to continue.")
 
2
  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()
11
 
12
+ #store actions into variables
13
+ #create or delete (restricted)
14
  while True:
15
  cord = input("What would you like to do? (create) (delete): ").lower()
16
 
 
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
 
 
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:
42
+ #if the token is found then log in:
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"
 
 
 
 
 
 
 
49
 
50
  #if the token is read only then prompt user to provide a write token:
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
+
84
+ #extra information for the user
85
  #won't work if special characters are used but should still successfully be created/deleted
86
  if cord == 'create':
87
  if r_type == 'model':
 
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)}.
104
 
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.")