Anthonyg5005 commited on
Commit
7c5419b
·
verified ·
1 Parent(s): 4ab466a

copy from unfinished to main

Browse files
Files changed (1) hide show
  1. HF Login Snippet.py +51 -0
HF Login Snippet.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #import required functions
2
+ from huggingface_hub import login, get_token, whoami
3
+ import os
4
+
5
+ #define clear screen function (OPTIONAL)
6
+ oname = os.name
7
+ if oname == 'nt':
8
+ osclear = 'cls'
9
+ elif oname == 'posix':
10
+ osclear = 'clear'
11
+ else:
12
+ osclear = ''
13
+ def clear_screen():
14
+ os.system(osclear)
15
+
16
+ #get token
17
+ if get_token() is not None:
18
+ #if the token is found in either HF_TOKEN or cli login then log in:
19
+ login(get_token())
20
+ tfound = 'true' #remove if lines 27-34 removed
21
+ else:
22
+ #if the token is not found then prompt user to provide it:
23
+ login(input("API token not detected. Enter your HuggingFace (WRITE) token: ")) # can remove "(WRITE)" if not required
24
+ tfound = 'false' #remove if lines 27-34 removed
25
+
26
+ #if the token is read only then prompt user to provide a write token (Only required if user needs a WRITE token, remove if READ is enough):
27
+ while True:
28
+ if whoami().get('auth', {}).get('accessToken', {}).get('role', None) != 'write':
29
+ clear_screen()
30
+ if os.environ.get('HF_TOKEN', None) is not None: #if environ finds HF_TOKEN as write then display following text and exit:
31
+ print(f'''
32
+ You have the environment variable HF_TOKEN set.
33
+ You cannot log in.
34
+ Either set the environment variable to a (WRITE) token or remove it.
35
+ ''')
36
+ input("Press enter to continue.")
37
+ exit()
38
+ print("You do not have write access to this repository. Please use a valid token with (WRITE) access.")
39
+ login(input("Enter your HuggingFace (WRITE) token: "))
40
+ continue
41
+ break
42
+
43
+ #if token wasn't found at first then print the name of the new logged in user (OPTIONAL)
44
+ if tfound == 'false':
45
+ print(f'''
46
+ You are now logged in as {whoami().get('fullname', None)}.
47
+
48
+ To logout, use the hf command line interface 'huggingface-cli logout'
49
+ To view your active account, use 'huggingface-cli whoami'
50
+ ''')
51
+