update to new token finding method and verify valid WRITE token
Browse files- manage branches.py +34 -30
    	
        manage branches.py
    CHANGED
    
    | @@ -1,7 +1,6 @@ | |
| 1 | 
             
            #import required modules
         | 
| 2 | 
             
            import os
         | 
| 3 | 
            -
            import  | 
| 4 | 
            -
            from huggingface_hub import create_branch, delete_branch
         | 
| 5 |  | 
| 6 | 
             
            #set clear screen function
         | 
| 7 | 
             
            def clear_screen():
         | 
| @@ -16,9 +15,8 @@ while True: | |
| 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 (User/Repo): ")
         | 
| @@ -28,26 +26,38 @@ while True: | |
| 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("Branch name (No spaces): ")
         | 
| 37 | 
             
            clear_screen()
         | 
| 38 |  | 
| 39 | 
             
            #get token
         | 
| 40 | 
            -
            if ' | 
| 41 | 
            -
                #if the  | 
| 42 | 
            -
                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 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 51 |  | 
| 52 | 
             
            #create or delete the branch
         | 
| 53 | 
             
            if cord == 'create':
         | 
| @@ -60,30 +70,24 @@ 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 | 
            -
                       | 
| 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 | 
            -
                       | 
| 86 | 
            -
                       | 
| 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.")
         | 
|  | |
| 1 | 
             
            #import required modules
         | 
| 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():
         | 
|  | |
| 15 |  | 
| 16 | 
             
                if cord not in ['create', 'delete']:
         | 
| 17 | 
             
                    clear_screen()
         | 
| 18 | 
            +
                    print("Please choose one of the following two options.")
         | 
| 19 | 
             
                    continue
         | 
|  | |
| 20 | 
             
                break
         | 
| 21 | 
             
            clear_screen()
         | 
| 22 | 
             
            repo = input("Repository name (User/Repo): ")
         | 
|  | |
| 26 |  | 
| 27 | 
             
                if r_type not in ['model', 'dataset', 'space']:
         | 
| 28 | 
             
                    clear_screen()
         | 
| 29 | 
            +
                    print("Please choose one of the following three options.")
         | 
| 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':
         | 
|  | |
| 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':
         | 
| 73 | 
            +
                    print(f"Branch {branch} created at https://huggingface.co/{repo}/tree/{branch}")
         | 
| 74 | 
             
                elif r_type == 'dataset':
         | 
| 75 | 
            +
                    print(f"Branch {branch} created at https://huggingface.co/datasets/{repo}/tree/{branch}")
         | 
| 76 | 
             
                elif r_type == 'space':
         | 
| 77 | 
            +
                    print(f"Branch {branch} created at https://huggingface.co/spaces/{repo}/tree/{branch}")
         | 
| 78 | 
             
            else:
         | 
| 79 | 
             
                if r_type == 'model':
         | 
| 80 | 
            +
                    print(f"Branch {branch} deleted on {r_type} https://huggingface.co/{repo}")
         | 
| 81 | 
             
                elif r_type == 'dataset':
         | 
| 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.")
         | 
