copy from unfinished to main
Browse files- HF Login Snippet Kaggle.py +53 -0
HF Login Snippet Kaggle.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#import required functions
|
| 2 |
+
import os
|
| 3 |
+
from huggingface_hub import login, get_token, whoami
|
| 4 |
+
|
| 5 |
+
#get token
|
| 6 |
+
if os.environ.get('KAGGLE_KERNEL_RUN_TYPE', None) is not None: #check if user in kaggle
|
| 7 |
+
from kaggle_secrets import UserSecretsClient
|
| 8 |
+
from kaggle_web_client import BackendError
|
| 9 |
+
try:
|
| 10 |
+
login(UserSecretsClient().get_secret("HF_TOKEN")) #login if token secret found
|
| 11 |
+
except BackendError:
|
| 12 |
+
print('''
|
| 13 |
+
When using Kaggle, make sure to use the secret key HF_TOKEN with a 'WRITE' token.
|
| 14 |
+
This will prevent the need to login every time you run the script.
|
| 15 |
+
Set your secrets with the secrets add-on on the top of the screen.
|
| 16 |
+
''')
|
| 17 |
+
if get_token() is not None:
|
| 18 |
+
#if the token is found then log in:
|
| 19 |
+
login(get_token())
|
| 20 |
+
tfound = "Where are my doritos?"
|
| 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: "))
|
| 24 |
+
tfound = "false"
|
| 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 |
+
if os.environ.get('HF_TOKEN', None) is not None: #if environ finds HF_TOKEN as read-only then display following text and exit:
|
| 30 |
+
print('''
|
| 31 |
+
You have the environment variable HF_TOKEN set.
|
| 32 |
+
You cannot log in.
|
| 33 |
+
Either set the environment variable to a 'WRITE' token or remove it.
|
| 34 |
+
''')
|
| 35 |
+
input("Press enter to continue.")
|
| 36 |
+
exit()
|
| 37 |
+
if os.environ.get('COLAB_BACKEND_VERSION', None) is not None:
|
| 38 |
+
print('''
|
| 39 |
+
Your Colab secret key is read-only
|
| 40 |
+
Please switch your key to 'write' or disable notebook access on the left.
|
| 41 |
+
For now, you are stuck in a loop
|
| 42 |
+
''')
|
| 43 |
+
elif os.environ.get('KAGGLE_KERNEL_RUN_TYPE', None) is not None:
|
| 44 |
+
print('''
|
| 45 |
+
Your Kaggle secret key is read-only
|
| 46 |
+
Please switch your key to 'write' or unattach from notebook in add-ons at the top.
|
| 47 |
+
Having a read-only key attched will require login every time.
|
| 48 |
+
''')
|
| 49 |
+
print("You do not have write access to this repository. Please use a valid token with (WRITE) access.")
|
| 50 |
+
login(input("Enter your HuggingFace (WRITE) token: "))
|
| 51 |
+
continue
|
| 52 |
+
break
|
| 53 |
+
|