Upload Error when api.upload_file

Hello,

I am new to the HuggingFace space API and I am currently attempting to upload a file to my existing model repository using the upload_file API. My upload part is as below:

try:
    api.upload_file(
        path_or_fileobj="/home/user/app/tmp/test.zip",
        path_in_repo="test.zip",
        repo_id="emmajoanne/models",
        repo_type="model",
    )
except:
    return "Upload error!"

It always falls into the except but doesn’t provide any more details.

From the official document (Upload files to the Hub) I don’t see any error handling information or what exceptions it throws.

Appreciate.

hi @emmajoanne , what’s the error message? do you have the write access token? have you login before attempting to write on your repo?

Thank you for your response.
I was able to locate the error message by printing out the BaseException as e. The problem is related to unauthorized access. From this error, I was able to determine that the root cause is an issue with the way I initialized the api lib.

So I updated my code from:

api = HfApi(token)

to :

api = HfApi(token=token)

Then, problem solved.

1 Like