huggingface / huggingface_hub

The official Python client for the Huggingface Hub.
https://huggingface.co/docs/huggingface_hub
Apache License 2.0
2.03k stars 532 forks source link

UnboundLocalError When Using Commit Context Manager #1721

Closed hahunavth closed 12 months ago

hahunavth commented 1 year ago

Describe the bug

When using the commit context manager to clone, create a commit with a file, and push, an UnboundLocalError is raised in the Hugging Face library code. This error occurs when manually copying a file to the local repository directory and then rerunning the process.

Reproduction

  1. Use the commit context manager to clone, create a commit with a file, and push.
  2. Manually copy a file to the local repo directory.
  3. Rerun step 1.

Logs

Token is valid (permission: write).
Your token has been saved in your configured git credential helpers (store).
Your token has been saved to /home/hahunavth/.cache/huggingface/token
Login successful
/home/hahunavth/PycharmProjects/fastApiProject/./test-repo is already a clone of https://huggingface.co/hahunavth/test-repo. Make sure you pull the latest changes with `repo.git_pull()`.
Traceback (most recent call last):
  File "/home/hahunavth/PycharmProjects/fastApiProject/./utils/hf_utils.py", line 20, in <module>
    with repo.commit(commit_message="My cool model :)", blocking=False):
  File "/home/hahunavth/miniconda3/envs/py3.9/lib/python3.9/contextlib.py", line 119, in __enter__
    return next(self.gen)
  File "/home/hahunavth/miniconda3/envs/py3.9/lib/python3.9/site-packages/huggingface_hub/repository.py", line 1375, in commit
    f" committed: {files_in_msg}. This may lead to errors if checking out"
UnboundLocalError: local variable 'files_in_msg' referenced before assignment

System info

- huggingface_hub version: 0.17.3
- Platform: Linux-6.2.0-33-generic-x86_64-with-glibc2.35
- Python version: 3.9.18
- Running in iPython ?: No
- Running in notebook ?: No
- Running in Google Colab ?: No
- Token path ?: /home/hahunavth/.cache/huggingface/token
- Has saved token ?: True
- Who am I ?: hahunavth
- Configured git credential helpers: store
- FastAI: N/A
- Tensorflow: N/A
- Torch: 2.0.1
- Jinja2: 3.1.2
- Graphviz: N/A
- Pydot: N/A
- Pillow: 10.0.1
- hf_transfer: N/A
- gradio: 3.47.0
- tensorboard: N/A
- numpy: 1.25.2
- pydantic: 2.4.2
- aiohttp: N/A
- ENDPOINT: https://huggingface.co
- HUGGINGFACE_HUB_CACHE: /home/hahunavth/.cache/huggingface/hub
- HUGGINGFACE_ASSETS_CACHE: /home/hahunavth/.cache/huggingface/assets
- HF_TOKEN_PATH: /home/hahunavth/.cache/huggingface/token
- HF_HUB_OFFLINE: False
- HF_HUB_DISABLE_TELEMETRY: False
- HF_HUB_DISABLE_PROGRESS_BARS: None
- HF_HUB_DISABLE_SYMLINKS_WARNING: False
- HF_HUB_DISABLE_EXPERIMENTAL_WARNING: False
- HF_HUB_DISABLE_IMPLICIT_TOKEN: False
- HF_HUB_ENABLE_HF_TRANSFER: False
hahunavth commented 1 year ago

Is that an indentation error? https://github.com/huggingface/huggingface_hub/blob/33b0405a61ad2ff0de732521b79aca226ea787aa/src/huggingface_hub/repository.py#L1369-L1378

Wauplin commented 12 months ago

Hi @hahunavth, thanks for reporting. I think it's due to a forgotten case where less than 5 files are to be staged.

If we replace

     if len(files_to_stage) > 5: 
         files_in_msg = str(files_to_stage[:5])[:-1] + ", ...]" 

by

     files_in_msg = str(files_to_stage[:5])[:-1] + ", ...]" if len(files_to_stage) > 5 else str(files_to_stage)

in the snippet you've shared, it should fix it.

Would you like to open a PR for that? :)

Wauplin commented 12 months ago

Also, Repository is a bit of legacy code now. It is still supported and maintained but we are slowly moving out from it in favor of more practical http-based methods. For more details, check out this explanation guide.

hahunavth commented 12 months ago

Hi @Wauplin, thanks for the quick response and for pinpointing the issue. I've submitted a pull request with the proposed code change. You can review it by following this #1722 . I'm a new to Hugging Face, the information and the link to the explanation guide you provided have been very helpful to me. Thank you for your assistance!

Wauplin commented 12 months ago

Great, thank you! I'll merge the PR once the CI is happy :) Have a great continuation with HF ecosystem :hugs: