Fail to upload models to hub
Author: minghao-wu
Pull request: no

### System Info

- `transformers` version: 4.34.1
- Platform: Linux-4.18.0-513.9.1.el8_9.x86_64-x86_64-with-glibc2.28
- Python version: 3.11.5
- Huggingface_hub version: 0.16.4
- Safetensors version: 0.4.0
- Accelerate version: 0.24.0
- Accelerate config:    not found
- PyTorch version (GPU?): 2.0.1+cu117 (False)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: <fill in>
- Using distributed or parallel set-up in script?: <fill in>

### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [X] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [X] My own task or dataset (give details below)

### Reproduction

I was using following snippet to push my models to hub (I cannot sucessfully push my models using `.push_to_hub()` my slurm cluster). 
```
import huggingface_hub
huggingface_hub.login(token="XXX")
model_name = os.path.basename(os.path.dirname(args.ckpt))
repo_id = f"minghaowu/"+model_name
print("uploading to", repo_id)
api = huggingface_hub.HfApi()
api.create_repo(
    repo_id=repo_id,
    repo_type="model",
    private=True,
    exist_ok=True,
)
api.upload_folder(
    folder_path=args.ckpt,
    repo_id=repo_id,
    repo_type="model",
)
```

### Expected behavior

The provided code snippet has been working smoothly for a few days, but today I got the error message as follows:
```
Traceback (most recent call last):
  File "/home/minghaow/.conda/envs/upload/lib/python3.11/site-packages/huggingface_hub/utils/_errors.py", line 261, in hf_raise_for_statusenizer.json:  94%|████████████████████████████████████████████████████████████████████████▌    | 13.7M/14.5M [00:01<00:00, 11.3MB/s]
    response.raise_for_status()██████████                                                                | 1/5 [00:06<00:24,  6.19s/it]
  File "/home/minghaow/.conda/envs/upload/lib/python3.11/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://huggingface.co/api/models/minghaowu/docnmt-bloom-7b-lora-p4-en-fr/commit/main

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/minghaow/docnmtllm-project/docnmtllm/train_para/upload_model.py", line 44, in <module>
    api.upload_folder(
  File "/home/minghaow/.conda/envs/upload/lib/python3.11/site-packages/huggingface_hub/utils/_validators.py", line 118, in _inner_fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/minghaow/.conda/envs/upload/lib/python3.11/site-packages/huggingface_hub/hf_api.py", line 849, in _inner
    return fn(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/minghaow/.conda/envs/upload/lib/python3.11/site-packages/huggingface_hub/hf_api.py", line 3748, in upload_folder
    commit_info = self.create_commit(
                  ^^^^^^^^^^^^^^^^^^^
  File "/home/minghaow/.conda/envs/upload/lib/python3.11/site-packages/huggingface_hub/utils/_validators.py", line 118, in _inner_fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/minghaow/.conda/envs/upload/lib/python3.11/site-packages/huggingface_hub/hf_api.py", line 849, in _inner
    return fn(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/minghaow/.conda/envs/upload/lib/python3.11/site-packages/huggingface_hub/hf_api.py", line 2967, in create_commit
    hf_raise_for_status(commit_resp, endpoint_name="commit")
  File "/home/minghaow/.conda/envs/upload/lib/python3.11/site-packages/huggingface_hub/utils/_errors.py", line 299, in hf_raise_for_status
    raise BadRequestError(message, response=response) from e
huggingface_hub.utils._errors.BadRequestError:  (Request ID: Root=1-65817fb9-7af65e20605305f129b7ad48;ddc7d2fa-2111-4a83-b540-25eda4ca6e86)

Bad request for commit endpoint:
"model-index[0].results[0].dataset.config" must be a string
```

Comments:
Hi @minghao-wu, thanks for raising this issue! 

Is this issue transient or have you seen in multiple times? 

cc @Wauplin the hub master 👑 

Hi @amyeroberts, thanks for your reply.

it's transient. The provided snippet was working smoothly until a few days ago. This is my first time to see this error.

BTW, although `api.upload_folder` fails, `.push_to_hub` works on my slurm cluster now.😂

Hi @minghao-wu, could you share the README file you are trying to upload when this error is happening?

`Bad request for commit endpoint: "model-index[0].results[0].dataset.config" must be a string` means that the server rejected your commit because the model card metadata is not correct. It's weird that it's a transient error given that the server always checks the model card metadata, no matter how you upload your files. Can it be that the README file is sometimes updated between retries?

Hi @Wauplin , 

This is one of those [README.md](https://github.com/huggingface/transformers/files/13725273/README.md).

Please note that, for this README file, I am finetining some in-house model with LoRA. More interestingly, I recently found that the fully fine-tuned models have to be uploaded with `api.upload_folder`, while the lora models have to be uploaded by `model.push_to_hub()`. 


Thanks for the quick response @minghao-wu. So in the readme file you've shared you can see
 
```yml
(...)
    dataset:
      name: monashnlp/iwslt2017_en_ar
      type: monashnlp/iwslt2017_en_ar
      config: null
      split: None
(...)
```

which is not valid on the server. Maybe the error looked transient because you did that for different trainings, some of which the config was not `null`? The fix would be to remove the line when config is null in the method that generates the model card. I don't know if this part is owned by you or the library but fixing it should solves your problem.

> I recently found that the fully fine-tuned models have to be uploaded with api.upload_folder, while the lora models have to be uploaded by model.push_to_hub().

I'm no expert on how `model.push_to_hub` is implemented but it is most likely using `api.upload_folder` under the hood since it is the high-level method to create a commit on the Hub. For your info, every method to upload files will always end up calling `api.create_commit` under the hood (this is where real things are happening :) ). So if you witness a difference between 2 upload methods, the problem usually lies on which files are generated and committed rather than a difference of upload method.

Hope this makes it clearer for you!

---

For the record, I opened an issue in `huggingface_hub` (the underlying library that makes the upload) to fail early in case of invalid model card: https://github.com/huggingface/huggingface_hub/issues/1927. This should avoid confusion in the future.

Hi @Wauplin , 

well explained!

Thank you very much and I think this issue can be closed.

Perfect! Glad to hear that! :hugs: