# How to use a privately hosted model for a Space?

**URL:** <https://discuss.huggingface.co/t/how-to-use-a-privately-hosted-model-for-a-space/16366>\
**Category:** 🔒 Gradio\
**Created:** [April 1, 2022, 2:15pm UTC](https://discuss.huggingface.co/t/how-to-use-a-privately-hosted-model-for-a-space/16366 "2022-04-01T14:15:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![carted-ml](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/carted-ml/32/4381_2.png) [@carted-ml](https://discuss.huggingface.co/u/carted-ml)\
**Post date:** [April 1, 2022, 2:15pm UTC](https://discuss.huggingface.co/t/how-to-use-a-privately-hosted-model-for-a-space/16366/1 "2022-04-01T14:15:49Z")

</div>

Hi,

Is it possible to use a privately hosted model to create a Space? I know one option would be to use `git lfs` to add all the necessary files to the repository and then be done with it.

But is there any way around it?

---

<div class="post-metadata">

**Author:** ![NimaBoscarino](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nimaboscarino/32/4358_2.png) [@NimaBoscarino](https://discuss.huggingface.co/u/NimaBoscarino)\
**Post date:** [April 1, 2022, 11:21pm UTC](https://discuss.huggingface.co/t/how-to-use-a-privately-hosted-model-for-a-space/16366/2 "2022-04-01T23:21:04Z")

</div>

If your model is already hosted somewhere and is accessible via an API, you could have your space [make an HTTP request to your model](https://huggingface.co/spaces/flax-community/dalle-mini/blob/main/app/streamlit/app.py#L18), as it’s done in the [dalle-mini space](https://huggingface.co/spaces/flax-community/dalle-mini). In that space they even their API URL with a secret.

Otherwise, you can have your space download the model at run time when the space starts up. In [this space it’s done with torch.hub.download\_url\_to\_file](https://huggingface.co/spaces/EleutherAI/VQGAN_CLIP/blob/main/app.py#L5), for example, but you could do it with Python’s requests module or something similar. If you need to hide the URL that you’re downloading your model from, but still want to make the space public, you could use a secret for that.

---

<div class="post-metadata">

**Author:** ![carted-ml](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/carted-ml/32/4381_2.png) [@carted-ml](https://discuss.huggingface.co/u/carted-ml)\
**Post date:** [April 2, 2022, 5:37am UTC](https://discuss.huggingface.co/t/how-to-use-a-privately-hosted-model-for-a-space/16366/3 "2022-04-02T05:37:33Z")

</div>

As I mentioned it’s a privately hosted model on Hugging Face hub. It can be loaded with:

```nohighlight
AutoModelForMaskedLM.from_pretrained("my-org/model-name")

```

It’s compatible with Hugging Face’s Inference API. I am happy to utilize that but for that, I need to pass my token. I know Spaces allows us to host secrets. But I’m not sure how can I access it in my Gradio `app.py` file.

---

<div class="post-metadata">

**Author:** ![NimaBoscarino](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nimaboscarino/32/4358_2.png) [@NimaBoscarino](https://discuss.huggingface.co/u/NimaBoscarino)\
**Post date:** [April 2, 2022, 6:28am UTC](https://discuss.huggingface.co/t/how-to-use-a-privately-hosted-model-for-a-space/16366/4 "2022-04-02T06:28:07Z")

</div>

Ah sorry about that, I’d misunderstood what privately hosted meant! In that case, you should be able to set your user access token [as a secret in your Space](https://huggingface.co/docs/hub/spaces#manage-secrets) and then access it as an environment variable within your Gradio space. e.g. [in this space I put together, the secret “NAME” gets fetched](https://huggingface.co/spaces/NimaBoscarino/gradio-secrets/blob/main/app.py#L5). Once that’s fetched, then you’d pass it to `from_pretrained` with the param `use_auth_token`.

---

<div class="post-metadata">

**Author:** ![carted-ml](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/carted-ml/32/4381_2.png) [@carted-ml](https://discuss.huggingface.co/u/carted-ml)\
**Post date:** [April 2, 2022, 7:11am UTC](https://discuss.huggingface.co/t/how-to-use-a-privately-hosted-model-for-a-space/16366/5 "2022-04-02T07:11:11Z")

</div>

> [@NimaBoscarino](#):
>
> as a secret in your Space

Thanks so much! As far as I know, setting `use_auth_token=True` will try to fetch the token from a pre-specified location. Would that be a problem here because the secret’s location might not match with what `from_pretrained()` is expecting?

---

<div class="post-metadata">

**Author:** ![NimaBoscarino](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nimaboscarino/32/4358_2.png) [@NimaBoscarino](https://discuss.huggingface.co/u/NimaBoscarino)\
**Post date:** [April 2, 2022, 4:19pm UTC](https://discuss.huggingface.co/t/how-to-use-a-privately-hosted-model-for-a-space/16366/6 "2022-04-02T16:19:35Z")

</div>

> Would that be a problem here because the secret’s location might not match with what `from_pretrained()` is expecting

Precisely! When you pass `True` to [use\_auth\_token](https://github.com/huggingface/transformers/blob/v4.17.0/src/transformers/modeling_utils.py#L1172) it will look under `~/.huggingface` for your token, which would be generated when someone logs in with the CLI. That would usually be done when you’re working locally, but with a Space I think the best thing to do is to pass the token as a string to `use_auth_token`. You could even do something like

```auto
auth_token = os.environ.get("TOKEN_FROM_SECRET") or True
AutoModelForMaskedLM.from_pretrained("my-org/model-name", use_auth_token=auth_token)

```

so that you can have it work both locally and on your Space easily.

---

<div class="post-metadata">

**Author:** ![carted-ml](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/carted-ml/32/4381_2.png) [@carted-ml](https://discuss.huggingface.co/u/carted-ml)\
**Post date:** [April 4, 2022, 5:23am UTC](https://discuss.huggingface.co/t/how-to-use-a-privately-hosted-model-for-a-space/16366/7 "2022-04-04T05:23:50Z")

</div>

Thank you so much!

---

<div class="post-metadata">

**Author:** ![osanseviero](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/osanseviero/32/38738_2.png) [@osanseviero](https://discuss.huggingface.co/u/osanseviero)\
**Post date:** [April 5, 2022, 9:39am UTC](https://discuss.huggingface.co/t/how-to-use-a-privately-hosted-model-for-a-space/16366/8 "2022-04-05T09:39:17Z")

</div>

If it helps, the following blog post shows how to use private models with Spaces (although not using `transformers`)

> **[HF Tips & Tricks - a Hugging Face Space by osanseviero](https://huggingface.co/spaces/osanseviero/tips-and-tricks)**
>
> Discover amazing ML apps made by the community
