# Upload model to hub

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fupload-model-to-hub%2F84152%3E%5C
**Category:** 🤗Hub\
**Created:** [April 28, 2024, 9:45am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fupload-model-to-hub%2F84152 "2024-04-28T09:45:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sachin](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sachin/32/26534_2.png) [@sachin](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsachin)%5C
**Post date:** [April 28, 2024, 9:45am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fupload-model-to-hub%2F84152%2F1 "2024-04-28T09:45:53Z")

</div>

I am using the following function to upload my custom CLIP model. The issue is that given that my vision and text models have different configs, the config of the last overwrites the first. It is not taking into account the `variant` argument to tag it.

Any tips on the “correct” way to save this model and upload it (fingers crossed without using the git functionality).

Side question: It’s also uploading a readme.md file to my understanding. Can I avoid doing this?

```python
def _upload_model_to_hub(
    vision_encoder: models.TinyCLIPVisionEncoder, text_encoder: models.TinyCLIPTextEncoder
):
    vision_encoder.save_pretrained(
        str(config.MODEL_PATH),
        variant="vision_encoder",
        safe_serialization=True,
        push_to_hub=True,
        repo_id="debug-clip-model",
    )
    text_encoder.save_pretrained(
        str(config.MODEL_PATH),
        variant="text_encoder",
        safe_serialization=True,
        push_to_hub=True,
        repo_id="debug-clip-model",
    )

```

If it helps this is part of the `save_pretrained` (in `transformers/modelling_utls.py`) code that is causing the issue imo. As can be seen there is no option attach the variant argument to the config.

```python
        # Save the config
        if is_main_process:
            if not _hf_peft_config_loaded:
                model_to_save.config.save_pretrained(save_directory)

```

---

<div class="post-metadata">

**Author:** ![sachin](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sachin/32/26534_2.png) [@sachin](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsachin)%5C
**Post date:** [May 4, 2024, 12:22pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fupload-model-to-hub%2F84152%2F2 "2024-05-04T12:22:58Z")

</div>

To upload my models I used the following code instead. The trick was to create separate subfolders for vision and text, and then use `huggingface_hub`’s `upload_folder` (uploading the common parent) method.

```python
def _upload_model_to_hub(
    vision_encoder: models.TinyCLIPVisionEncoder,
    text_encoder: models.TinyCLIPTextEncoder,
    debug: bool = False,
):
    vision_encoder.save_pretrained(
        str(config.VISION_MODEL_PATH),
        safe_serialization=True,
    )
    text_encoder.save_pretrained(
        str(config.TEXT_MODEL_PATH),
        safe_serialization=True,
    )

    api = HfApi()
    if debug:
        repo_components = config.REPO_ID.split("/", maxsplit=1)
        repo_components[1] = f"debug-{repo_components[1]}"
        repo_id = "/".join(repo_components)
    else:
        repo_id = config.REPO_ID
    common_hf_api_params = {
        "repo_id": repo_id,
        "repo_type": "model",
    }
    if not api.repo_exists(**common_hf_api_params):
        logger.info(f"Creating repo {repo_id} on Hugging Face Hub.")
        api.create_repo(**common_hf_api_params) # type: ignore
    logger.info(f"Uploading models in {str(config.MODEL_PATH)} to {repo_id}.")
    api.upload_folder(
        folder_path=config.MODEL_PATH,
        **common_hf_api_params, # type: ignore
    ) # type: ignore

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsystem)%5C
**Post date:** [May 5, 2024, 12:23am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fupload-model-to-hub%2F84152%2F3 "2024-05-05T00:23:32Z")

</div>

Closed on May 5, 2024, 12:23am UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
