source
stringclasses 40
values | file_type
stringclasses 1
value | chunk
stringlengths 3
512
| id
int64 0
1.5k
|
---|---|---|---|
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/webhooks.md | .md | [guide](https://huggingface.co/docs/hub/spaces-overview).
Your webhook server is now running on a public Space. If most cases, you will want to secure it with a secret. Go to
your Space settings > Section "Repository secrets" > "Add a secret". Set the `WEBHOOK_SECRET` environment variable to
the value of your choice. Go back to the [Webhooks settings](https://huggingface.co/settings/webhooks) and set the | 1,200 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/webhooks.md | .md | the value of your choice. Go back to the [Webhooks settings](https://huggingface.co/settings/webhooks) and set the
secret in the webhook configuration. Now, only requests with the correct secret will be accepted by your server.
And this is it! Your Space is now ready to receive webhooks from the Hub. Please keep in mind that if you run the Space
on a free 'cpu-basic' hardware, it will be shut down after 48 hours of inactivity. If you need a permanent Space, you | 1,201 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/webhooks.md | .md | on a free 'cpu-basic' hardware, it will be shut down after 48 hours of inactivity. If you need a permanent Space, you
should consider setting to an [upgraded hardware](https://huggingface.co/docs/hub/spaces-gpus#hardware-specs). | 1,202 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/webhooks.md | .md | The guide above explained the quickest way to setup a [`WebhooksServer`]. In this section, we will see how to customize
it further. | 1,203 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/webhooks.md | .md | You can register multiple endpoints on the same server. For example, you might want to have one endpoint to trigger
a training job and another one to trigger a model evaluation. You can do this by adding multiple `@webhook_endpoint`
decorators:
```python
# app.py
from huggingface_hub import webhook_endpoint, WebhookPayload | 1,204 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/webhooks.md | .md | @webhook_endpoint
async def trigger_training(payload: WebhookPayload) -> None:
if payload.repo.type == "dataset" and payload.event.action == "update":
# Trigger a training job if a dataset is updated
... | 1,205 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/webhooks.md | .md | @webhook_endpoint
async def trigger_evaluation(payload: WebhookPayload) -> None:
if payload.repo.type == "model" and payload.event.action == "update":
# Trigger an evaluation job if a model is updated
...
```
Which will create two endpoints:
```text
(...)
Webhooks are correctly setup and ready to use:
- POST https://1fadb0f52d8bf825fc.gradio.live/webhooks/trigger_training
- POST https://1fadb0f52d8bf825fc.gradio.live/webhooks/trigger_evaluation
``` | 1,206 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/webhooks.md | .md | To get more flexibility, you can also create a [`WebhooksServer`] object directly. This is useful if you want to
customize the landing page of your server. You can do this by passing a [Gradio UI](https://gradio.app/docs/#blocks)
that will overwrite the default one. For example, you can add instructions for your users or add a form to manually
trigger the webhooks. When creating a [`WebhooksServer`], you can register new webhooks using the
[`~WebhooksServer.add_webhook`] decorator. | 1,207 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/webhooks.md | .md | [`~WebhooksServer.add_webhook`] decorator.
Here is a complete example:
```python
import gradio as gr
from fastapi import Request
from huggingface_hub import WebhooksServer, WebhookPayload | 1,208 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/webhooks.md | .md | # 1. Define UI
with gr.Blocks() as ui:
...
# 2. Create WebhooksServer with custom UI and secret
app = WebhooksServer(ui=ui, webhook_secret="my_secret_key")
# 3. Register webhook with explicit name
@app.add_webhook("/say_hello")
async def hello(payload: WebhookPayload):
return {"message": "hello"}
# 4. Register webhook with implicit name
@app.add_webhook
async def goodbye(payload: WebhookPayload):
return {"message": "goodbye"} | 1,209 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/webhooks.md | .md | # 5. Start server (optional)
app.run()
```
1. We define a custom UI using Gradio blocks. This UI will be displayed on the landing page of the server.
2. We create a [`WebhooksServer`] object with a custom UI and a secret. The secret is optional and can be set with
the `WEBHOOK_SECRET` environment variable.
3. We register a webhook with an explicit name. This will create an endpoint at `/webhooks/say_hello`. | 1,210 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/webhooks.md | .md | 3. We register a webhook with an explicit name. This will create an endpoint at `/webhooks/say_hello`.
4. We register a webhook with an implicit name. This will create an endpoint at `/webhooks/goodbye`.
5. We start the server. This is optional as your server will automatically be started at the end of the script. | 1,211 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | <!--⚠️ Note that this file is in Markdown but contains specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
--> | 1,212 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | The Hugging Face Hub is a collection of git repositories. [Git](https://git-scm.com/) is a widely used tool in software
development to easily version projects when working collaboratively. This guide will show you how to interact with the
repositories on the Hub, especially:
- Create and delete a repository.
- Manage branches and tags.
- Rename your repository.
- Update your repository visibility.
- Manage a local copy of your repository.
<Tip warning={true}> | 1,213 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | - Update your repository visibility.
- Manage a local copy of your repository.
<Tip warning={true}>
If you are used to working with platforms such as GitLab/GitHub/Bitbucket, your first instinct
might be to use `git` CLI to clone your repo (`git clone`), commit changes (`git add, git commit`) and push them
(`git push`). This is valid when using the Hugging Face Hub. However, software engineering and machine learning do | 1,214 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | (`git push`). This is valid when using the Hugging Face Hub. However, software engineering and machine learning do
not share the same requirements and workflows. Model repositories might maintain large model weight files for different
frameworks and tools, so cloning the repository can lead to you maintaining large local folders with massive sizes. As
a result, it may be more efficient to use our custom HTTP methods. You can read our [Git vs HTTP paradigm](../concepts/git_vs_http) | 1,215 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | explanation page for more details.
</Tip>
If you want to create and manage a repository on the Hub, your machine must be logged in. If you are not, please refer to
[this section](../quick-start#authentication). In the rest of this guide, we will assume that your machine is logged in. | 1,216 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | The first step is to know how to create and delete repositories. You can only manage repositories that you own (under
your username namespace) or from organizations in which you have write permissions. | 1,217 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | Create an empty repository with [`create_repo`] and give it a name with the `repo_id` parameter. The `repo_id` is your namespace followed by the repository name: `username_or_org/repo_name`.
```py
>>> from huggingface_hub import create_repo
>>> create_repo("lysandre/test-model")
'https://huggingface.co/lysandre/test-model'
``` | 1,218 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | >>> create_repo("lysandre/test-model")
'https://huggingface.co/lysandre/test-model'
```
By default, [`create_repo`] creates a model repository. But you can use the `repo_type` parameter to specify another repository type. For example, if you want to create a dataset repository:
```py
>>> from huggingface_hub import create_repo
>>> create_repo("lysandre/test-dataset", repo_type="dataset")
'https://huggingface.co/datasets/lysandre/test-dataset'
``` | 1,219 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | >>> create_repo("lysandre/test-dataset", repo_type="dataset")
'https://huggingface.co/datasets/lysandre/test-dataset'
```
When you create a repository, you can set your repository visibility with the `private` parameter.
```py
>>> from huggingface_hub import create_repo
>>> create_repo("lysandre/test-private", private=True)
```
If you want to change the repository visibility at a later time, you can use the [`update_repo_visibility`] function.
<Tip> | 1,220 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | If you are part of an organization with an Enterprise plan, you can create a repo in a specific resource group by passing `resource_group_id` as parameter to [`create_repo`]. Resource groups are a security feature to control which members from your org can access a given resource. You can get the resource group ID by copying it from your org settings page url on the Hub (e.g. `"https://huggingface.co/organizations/huggingface/settings/resource-groups/66670e5163145ca562cb1988"` => | 1,221 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | url on the Hub (e.g. `"https://huggingface.co/organizations/huggingface/settings/resource-groups/66670e5163145ca562cb1988"` => `"66670e5163145ca562cb1988"`). For more details about resource group, check out this [guide](https://huggingface.co/docs/hub/en/security-resource-groups). | 1,222 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | </Tip> | 1,223 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | Delete a repository with [`delete_repo`]. Make sure you want to delete a repository because this is an irreversible process!
Specify the `repo_id` of the repository you want to delete:
```py
>>> delete_repo(repo_id="lysandre/my-corrupted-dataset", repo_type="dataset")
``` | 1,224 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | In some cases, you want to copy someone else's repo to adapt it to your use case.
This is possible for Spaces using the [`duplicate_space`] method. It will duplicate the whole repository.
You will still need to configure your own settings (hardware, sleep-time, storage, variables and secrets). Check out our [Manage your Space](./manage-spaces) guide for more details.
```py
>>> from huggingface_hub import duplicate_space
>>> duplicate_space("multimodalart/dreambooth-training", private=False) | 1,225 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | ```py
>>> from huggingface_hub import duplicate_space
>>> duplicate_space("multimodalart/dreambooth-training", private=False)
RepoUrl('https://huggingface.co/spaces/nateraw/dreambooth-training',...)
``` | 1,226 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | Now that you have created your repository, you are interested in pushing changes to it and downloading files from it.
These 2 topics deserve their own guides. Please refer to the [upload](./upload) and the [download](./download) guides
to learn how to use your repository. | 1,227 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | Git repositories often make use of branches to store different versions of a same repository.
Tags can also be used to flag a specific state of your repository, for example, when releasing a version.
More generally, branches and tags are referred as [git references](https://git-scm.com/book/en/v2/Git-Internals-Git-References). | 1,228 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | You can create new branch and tags using [`create_branch`] and [`create_tag`]:
```py
>>> from huggingface_hub import create_branch, create_tag
# Create a branch on a Space repo from `main` branch
>>> create_branch("Matthijs/speecht5-tts-demo", repo_type="space", branch="handle-dog-speaker") | 1,229 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | # Create a tag on a Dataset repo from `v0.1-release` branch
>>> create_tag("bigcode/the-stack", repo_type="dataset", revision="v0.1-release", tag="v0.1.1", tag_message="Bump release version.")
```
You can use the [`delete_branch`] and [`delete_tag`] functions in the same way to delete a branch or a tag. | 1,230 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | You can also list the existing git refs from a repository using [`list_repo_refs`]:
```py
>>> from huggingface_hub import list_repo_refs
>>> list_repo_refs("bigcode/the-stack", repo_type="dataset")
GitRefs(
branches=[
GitRefInfo(name='main', ref='refs/heads/main', target_commit='18edc1591d9ce72aa82f56c4431b3c969b210ae3'),
GitRefInfo(name='v1.1.a1', ref='refs/heads/v1.1.a1', target_commit='f9826b862d1567f3822d3d25649b0d6d22ace714')
],
converts=[],
tags=[ | 1,231 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | ],
converts=[],
tags=[
GitRefInfo(name='v1.0', ref='refs/tags/v1.0', target_commit='c37a8cd1e382064d8aced5e05543c5f7753834da')
]
)
``` | 1,232 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | Repositories come with some settings that you can configure. Most of the time, you will want to do that manually in the
repo settings page in your browser. You must have write access to a repo to configure it (either own it or being part of
an organization). In this section, we will see the settings that you can also configure programmatically using `huggingface_hub`. | 1,233 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | Some settings are specific to Spaces (hardware, environment variables,...). To configure those, please refer to our [Manage your Spaces](../guides/manage-spaces) guide. | 1,234 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | A repository can be public or private. A private repository is only visible to you or members of the organization in which the repository is located. Change a repository to private as shown in the following:
```py
>>> from huggingface_hub import update_repo_settings
>>> update_repo_settings(repo_id=repo_id, private=True)
``` | 1,235 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | To give more control over how repos are used, the Hub allows repo authors to enable **access requests** for their repos. User must agree to share their contact information (username and email address) with the repo authors to access the files when enabled. A repo with access requests enabled is called a **gated repo**.
You can set a repo as gated using [`update_repo_settings`]:
```py
>>> from huggingface_hub import HfApi | 1,236 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | >>> api = HfApi()
>>> api.update_repo_settings(repo_id=repo_id, gated="auto") # Set automatic gating for a model
``` | 1,237 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | You can rename your repository on the Hub using [`move_repo`]. Using this method, you can also move the repo from a user to
an organization. When doing so, there are a [few limitations](https://hf.co/docs/hub/repositories-settings#renaming-or-transferring-a-repo)
that you should be aware of. For example, you can't transfer your repo to another user.
```py
>>> from huggingface_hub import move_repo
>>> move_repo(from_id="Wauplin/cool-model", to_id="huggingface/cool-model")
``` | 1,238 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | All the actions described above can be done using HTTP requests. However, in some cases you might be interested in having
a local copy of your repository and interact with it using the Git commands you are familiar with. | 1,239 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | a local copy of your repository and interact with it using the Git commands you are familiar with.
The [`Repository`] class allows you to interact with files and repositories on the Hub with functions similar to Git commands. It is a wrapper over Git and Git-LFS methods to use the Git commands you already know and love. Before starting, please make sure you have Git-LFS installed (see [here](https://git-lfs.github.com/) for installation instructions).
<Tip warning={true}> | 1,240 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | <Tip warning={true}>
[`Repository`] is deprecated in favor of the http-based alternatives implemented in [`HfApi`]. Given its large adoption in legacy code, the complete removal of [`Repository`] will only happen in release `v1.0`. For more details, please read [this explanation page](./concepts/git_vs_http).
</Tip> | 1,241 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | Instantiate a [`Repository`] object with a path to a local repository:
```py
>>> from huggingface_hub import Repository
>>> repo = Repository(local_dir="<path>/<to>/<folder>")
``` | 1,242 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | The `clone_from` parameter clones a repository from a Hugging Face repository ID to a local directory specified by the `local_dir` argument:
```py
>>> from huggingface_hub import Repository
>>> repo = Repository(local_dir="w2v2", clone_from="facebook/wav2vec2-large-960h-lv60")
```
`clone_from` can also clone a repository using a URL:
```py
>>> repo = Repository(local_dir="huggingface-hub", clone_from="https://huggingface.co/facebook/wav2vec2-large-960h-lv60")
``` | 1,243 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | >>> repo = Repository(local_dir="huggingface-hub", clone_from="https://huggingface.co/facebook/wav2vec2-large-960h-lv60")
```
You can combine the `clone_from` parameter with [`create_repo`] to create and clone a repository:
```py
>>> repo_url = create_repo(repo_id="repo_name")
>>> repo = Repository(local_dir="repo_local_path", clone_from=repo_url)
``` | 1,244 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | >>> repo_url = create_repo(repo_id="repo_name")
>>> repo = Repository(local_dir="repo_local_path", clone_from=repo_url)
```
You can also configure a Git username and email to a cloned repository by specifying the `git_user` and `git_email` parameters when you clone a repository. When users commit to that repository, Git will be aware of the commit author.
```py
>>> repo = Repository(
... "my-dataset",
... clone_from="<user>/<dataset_id>",
... token=True,
... repo_type="dataset", | 1,245 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | >>> repo = Repository(
... "my-dataset",
... clone_from="<user>/<dataset_id>",
... token=True,
... repo_type="dataset",
... git_user="MyName",
... git_email="me@cool.mail"
... )
``` | 1,246 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | Branches are important for collaboration and experimentation without impacting your current files and code. Switch between branches with [`~Repository.git_checkout`]. For example, if you want to switch from `branch1` to `branch2`:
```py
>>> from huggingface_hub import Repository
>>> repo = Repository(local_dir="huggingface-hub", clone_from="<user>/<dataset_id>", revision='branch1')
>>> repo.git_checkout("branch2")
``` | 1,247 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/repository.md | .md | [`~Repository.git_pull`] allows you to update a current local branch with changes from a remote repository:
```py
>>> from huggingface_hub import Repository
>>> repo.git_pull()
```
Set `rebase=True` if you want your local commits to occur after your branch is updated with the new commits from the remote:
```py
>>> repo.git_pull(rebase=True)
``` | 1,248 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | <!--⚠️ Note that this file is in Markdown but contains specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
--> | 1,249 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | In this guide, we will see how to manage your Space runtime
([secrets](https://huggingface.co/docs/hub/spaces-overview#managing-secrets),
[hardware](https://huggingface.co/docs/hub/spaces-gpus), and [storage](https://huggingface.co/docs/hub/spaces-storage#persistent-storage)) using `huggingface_hub`. | 1,250 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | Here is an end-to-end example to create and setup a Space on the Hub.
**1. Create a Space on the Hub.**
```py
>>> from huggingface_hub import HfApi
>>> repo_id = "Wauplin/my-cool-training-space"
>>> api = HfApi() | 1,251 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | # For example with a Gradio SDK
>>> api.create_repo(repo_id=repo_id, repo_type="space", space_sdk="gradio")
```
**1. (bis) Duplicate a Space.**
This can prove useful if you want to build up from an existing Space instead of starting from scratch.
It is also useful is you want control over the configuration/settings of a public Space. See [`duplicate_space`] for more details.
```py
>>> api.duplicate_space("multimodalart/dreambooth-training")
``` | 1,252 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | ```py
>>> api.duplicate_space("multimodalart/dreambooth-training")
```
**2. Upload your code using your preferred solution.**
Here is an example to upload the local folder `src/` from your machine to your Space:
```py
>>> api.upload_folder(repo_id=repo_id, repo_type="space", folder_path="src/")
```
At this step, your app should already be running on the Hub for free !
However, you might want to configure it further with secrets and upgraded hardware.
**3. Configure secrets and variables** | 1,253 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | However, you might want to configure it further with secrets and upgraded hardware.
**3. Configure secrets and variables**
Your Space might require some secret keys, token or variables to work.
See [docs](https://huggingface.co/docs/hub/spaces-overview#managing-secrets) for more details.
For example, an HF token to upload an image dataset to the Hub once generated from your Space.
```py
>>> api.add_space_secret(repo_id=repo_id, key="HF_TOKEN", value="hf_api_***") | 1,254 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | ```py
>>> api.add_space_secret(repo_id=repo_id, key="HF_TOKEN", value="hf_api_***")
>>> api.add_space_variable(repo_id=repo_id, key="MODEL_REPO_ID", value="user/repo")
```
Secrets and variables can be deleted as well:
```py
>>> api.delete_space_secret(repo_id=repo_id, key="HF_TOKEN")
>>> api.delete_space_variable(repo_id=repo_id, key="MODEL_REPO_ID")
```
<Tip>
From within your Space, secrets are available as environment variables (or | 1,255 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | ```
<Tip>
From within your Space, secrets are available as environment variables (or
Streamlit Secrets Management if using Streamlit). No need to fetch them via the API!
</Tip>
<Tip warning={true}>
Any change in your Space configuration (secrets or hardware) will trigger a restart of your app.
</Tip>
**Bonus: set secrets and variables when creating or duplicating the Space!**
Secrets and variables can be set when creating or duplicating a space:
```py
>>> api.create_repo(
... repo_id=repo_id, | 1,256 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | Secrets and variables can be set when creating or duplicating a space:
```py
>>> api.create_repo(
... repo_id=repo_id,
... repo_type="space",
... space_sdk="gradio",
... space_secrets=[{"key"="HF_TOKEN", "value"="hf_api_***"}, ...],
... space_variables=[{"key"="MODEL_REPO_ID", "value"="user/repo"}, ...],
... )
```
```py
>>> api.duplicate_space(
... from_id=repo_id,
... secrets=[{"key"="HF_TOKEN", "value"="hf_api_***"}, ...], | 1,257 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | ```
```py
>>> api.duplicate_space(
... from_id=repo_id,
... secrets=[{"key"="HF_TOKEN", "value"="hf_api_***"}, ...],
... variables=[{"key"="MODEL_REPO_ID", "value"="user/repo"}, ...],
... )
```
**4. Configure the hardware**
By default, your Space will run on a CPU environment for free. You can upgrade the hardware
to run it on GPUs. A payment card or a community grant is required to access upgrade your
Space. See [docs](https://huggingface.co/docs/hub/spaces-gpus) for more details. | 1,258 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | Space. See [docs](https://huggingface.co/docs/hub/spaces-gpus) for more details.
```py
# Use `SpaceHardware` enum
>>> from huggingface_hub import SpaceHardware
>>> api.request_space_hardware(repo_id=repo_id, hardware=SpaceHardware.T4_MEDIUM) | 1,259 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | # Or simply pass a string value
>>> api.request_space_hardware(repo_id=repo_id, hardware="t4-medium")
```
Hardware updates are not done immediately as your Space has to be reloaded on our servers.
At any time, you can check on which hardware your Space is running to see if your request
has been met.
```py
>>> runtime = api.get_space_runtime(repo_id=repo_id)
>>> runtime.stage
"RUNNING_BUILDING"
>>> runtime.hardware
"cpu-basic"
>>> runtime.requested_hardware
"t4-medium"
``` | 1,260 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | >>> runtime.stage
"RUNNING_BUILDING"
>>> runtime.hardware
"cpu-basic"
>>> runtime.requested_hardware
"t4-medium"
```
You now have a Space fully configured. Make sure to downgrade your Space back to "cpu-classic"
when you are done using it.
**Bonus: request hardware when creating or duplicating the Space!**
Upgraded hardware will be automatically assigned to your Space once it's built.
```py
>>> api.create_repo(
... repo_id=repo_id,
... repo_type="space",
... space_sdk="gradio" | 1,261 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | ```py
>>> api.create_repo(
... repo_id=repo_id,
... repo_type="space",
... space_sdk="gradio"
... space_hardware="cpu-upgrade",
... space_storage="small",
... space_sleep_time="7200", # 2 hours in secs
... )
```
```py
>>> api.duplicate_space(
... from_id=repo_id,
... hardware="cpu-upgrade",
... storage="small",
... sleep_time="7200", # 2 hours in secs
... )
```
**5. Pause and restart your Space** | 1,262 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | ... storage="small",
... sleep_time="7200", # 2 hours in secs
... )
```
**5. Pause and restart your Space**
By default if your Space is running on an upgraded hardware, it will never be stopped. However to avoid getting billed,
you might want to pause it when you are not using it. This is possible using [`pause_space`]. A paused Space will be
inactive until the owner of the Space restarts it, either with the UI or via API using [`restart_space`]. | 1,263 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | inactive until the owner of the Space restarts it, either with the UI or via API using [`restart_space`].
For more details about paused mode, please refer to [this section](https://huggingface.co/docs/hub/spaces-gpus#pause)
```py
# Pause your Space to avoid getting billed
>>> api.pause_space(repo_id=repo_id)
# (...)
# Restart it when you need it
>>> api.restart_space(repo_id=repo_id)
``` | 1,264 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | >>> api.pause_space(repo_id=repo_id)
# (...)
# Restart it when you need it
>>> api.restart_space(repo_id=repo_id)
```
Another possibility is to set a timeout for your Space. If your Space is inactive for more than the timeout duration,
it will go to sleep. Any visitor landing on your Space will start it back up. You can set a timeout using
[`set_space_sleep_time`]. For more details about sleeping mode, please refer to [this section](https://huggingface.co/docs/hub/spaces-gpus#sleep-time).
```py | 1,265 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | ```py
# Put your Space to sleep after 1h of inactivity
>>> api.set_space_sleep_time(repo_id=repo_id, sleep_time=3600)
```
Note: if you are using a 'cpu-basic' hardware, you cannot configure a custom sleep time. Your Space will automatically
be paused after 48h of inactivity.
**Bonus: set a sleep time while requesting hardware**
Upgraded hardware will be automatically assigned to your Space once it's built.
```py | 1,266 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | Upgraded hardware will be automatically assigned to your Space once it's built.
```py
>>> api.request_space_hardware(repo_id=repo_id, hardware=SpaceHardware.T4_MEDIUM, sleep_time=3600)
```
**Bonus: set a sleep time when creating or duplicating the Space!**
```py
>>> api.create_repo(
... repo_id=repo_id,
... repo_type="space",
... space_sdk="gradio"
... space_hardware="t4-medium",
... space_sleep_time="3600",
... )
```
```py
>>> api.duplicate_space(
... from_id=repo_id, | 1,267 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | ... space_sleep_time="3600",
... )
```
```py
>>> api.duplicate_space(
... from_id=repo_id,
... hardware="t4-medium",
... sleep_time="3600",
... )
```
**6. Add persistent storage to your Space** | 1,268 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | ... hardware="t4-medium",
... sleep_time="3600",
... )
```
**6. Add persistent storage to your Space**
You can choose the storage tier of your choice to access disk space that persists across restarts of your Space. This means you can read and write from disk like you would with a traditional hard drive. See [docs](https://huggingface.co/docs/hub/spaces-storage#persistent-storage) for more details.
```py
>>> from huggingface_hub import SpaceStorage | 1,269 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | ```py
>>> from huggingface_hub import SpaceStorage
>>> api.request_space_storage(repo_id=repo_id, storage=SpaceStorage.LARGE)
```
You can also delete your storage, losing all the data permanently.
```py
>>> api.delete_space_storage(repo_id=repo_id)
```
Note: You cannot decrease the storage tier of your space once it's been granted. To do so,
you must delete the storage first then request the new desired tier.
**Bonus: request storage when creating or duplicating the Space!**
```py | 1,270 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | **Bonus: request storage when creating or duplicating the Space!**
```py
>>> api.create_repo(
... repo_id=repo_id,
... repo_type="space",
... space_sdk="gradio"
... space_storage="large",
... )
```
```py
>>> api.duplicate_space(
... from_id=repo_id,
... storage="large",
... )
``` | 1,271 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | Spaces allow for a lot of different use cases. Sometimes, you might want
to temporarily run a Space on a specific hardware, do something and then shut it down. In
this section, we will explore how to benefit from Spaces to finetune a model on demand.
This is only one way of solving this particular problem. It has to be taken as a suggestion
and adapted to your use case.
Let's assume we have a Space to finetune a model. It is a Gradio app that takes as input | 1,272 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | and adapted to your use case.
Let's assume we have a Space to finetune a model. It is a Gradio app that takes as input
a model id and a dataset id. The workflow is as follows:
0. (Prompt the user for a model and a dataset)
1. Load the model from the Hub.
2. Load the dataset from the Hub.
3. Finetune the model on the dataset.
4. Upload the new model to the Hub.
Step 3. requires a custom hardware but you don't want your Space to be running all the time on a paid | 1,273 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | Step 3. requires a custom hardware but you don't want your Space to be running all the time on a paid
GPU. A solution is to dynamically request hardware for the training and shut it
down afterwards. Since requesting hardware restarts your Space, your app must somehow "remember"
the current task it is performing. There are multiple ways of doing this. In this guide
we will see one solution using a Dataset as "task scheduler". | 1,274 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | Here is what your app would look like. On startup, check if a task is scheduled and if yes,
run it on the correct hardware. Once done, set back hardware to the free-plan CPU and
prompt the user for a new task.
<Tip warning={true}>
Such a workflow does not support concurrent access as normal demos.
In particular, the interface will be disabled when training occurs.
It is preferable to set your repo as private to ensure you are the only user.
</Tip>
```py | 1,275 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | It is preferable to set your repo as private to ensure you are the only user.
</Tip>
```py
# Space will need your token to request hardware: set it as a Secret !
HF_TOKEN = os.environ.get("HF_TOKEN") | 1,276 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | # Space own repo_id
TRAINING_SPACE_ID = "Wauplin/dreambooth-training"
from huggingface_hub import HfApi, SpaceHardware
api = HfApi(token=HF_TOKEN)
# On Space startup, check if a task is scheduled. If yes, finetune the model. If not,
# display an interface to request a new task.
task = get_task()
if task is None:
# Start Gradio app
def gradio_fn(task):
# On user request, add task and request hardware
add_task(task)
api.request_space_hardware(repo_id=TRAINING_SPACE_ID, hardware=SpaceHardware.T4_MEDIUM) | 1,277 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | gr.Interface(fn=gradio_fn, ...).launch()
else:
runtime = api.get_space_runtime(repo_id=TRAINING_SPACE_ID)
# Check if Space is loaded with a GPU.
if runtime.hardware == SpaceHardware.T4_MEDIUM:
# If yes, finetune base model on dataset !
train_and_upload(task)
# Then, mark the task as "DONE"
mark_as_done(task) | 1,278 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | # Then, mark the task as "DONE"
mark_as_done(task)
# DO NOT FORGET: set back CPU hardware
api.request_space_hardware(repo_id=TRAINING_SPACE_ID, hardware=SpaceHardware.CPU_BASIC)
else:
api.request_space_hardware(repo_id=TRAINING_SPACE_ID, hardware=SpaceHardware.T4_MEDIUM)
``` | 1,279 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | Scheduling tasks can be done in many ways. Here is an example how it could be done using
a simple CSV stored as a Dataset.
```py
# Dataset ID in which a `tasks.csv` file contains the tasks to perform.
# Here is a basic example for `tasks.csv` containing inputs (base model and dataset)
# and status (PENDING or DONE).
# multimodalart/sd-fine-tunable,Wauplin/concept-1,DONE
# multimodalart/sd-fine-tunable,Wauplin/concept-2,PENDING
TASK_DATASET_ID = "Wauplin/dreambooth-task-scheduler" | 1,280 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | def _get_csv_file():
return hf_hub_download(repo_id=TASK_DATASET_ID, filename="tasks.csv", repo_type="dataset", token=HF_TOKEN)
def get_task():
with open(_get_csv_file()) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
if row[2] == "PENDING":
return row[0], row[1] # model_id, dataset_id
def add_task(task):
model_id, dataset_id = task
with open(_get_csv_file()) as csv_file:
with open(csv_file, "r") as f:
tasks = f.read() | 1,281 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | api.upload_file(
repo_id=repo_id,
repo_type=repo_type,
path_in_repo="tasks.csv",
# Quick and dirty way to add a task
path_or_fileobj=(tasks + f"\n{model_id},{dataset_id},PENDING").encode()
)
def mark_as_done(task):
model_id, dataset_id = task
with open(_get_csv_file()) as csv_file:
with open(csv_file, "r") as f:
tasks = f.read() | 1,282 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/manage-spaces.md | .md | api.upload_file(
repo_id=repo_id,
repo_type=repo_type,
path_in_repo="tasks.csv",
# Quick and dirty way to set the task as DONE
path_or_fileobj=tasks.replace(
f"{model_id},{dataset_id},PENDING",
f"{model_id},{dataset_id},DONE"
).encode()
)
``` | 1,283 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | <!--⚠️ Note that this file is in Markdown but contains specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
--> | 1,284 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | Sharing your files and work is an important aspect of the Hub. The `huggingface_hub` offers several options for uploading your files to the Hub. You can use these functions independently or integrate them into your library, making it more convenient for your users to interact with the Hub. This guide will show you how to push files:
- without using Git.
- that are very large with [Git LFS](https://git-lfs.github.com/).
- with the `commit` context manager.
- with the [`~Repository.push_to_hub`] function. | 1,285 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | - with the `commit` context manager.
- with the [`~Repository.push_to_hub`] function.
Whenever you want to upload files to the Hub, you need to log in to your Hugging Face account. For more details about authentication, check out [this section](../quick-start#authentication). | 1,286 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | Once you've created a repository with [`create_repo`], you can upload a file to your repository using [`upload_file`].
Specify the path of the file to upload, where you want to upload the file to in the repository, and the name of the repository you want to add the file to. Depending on your repository type, you can optionally set the repository type as a `dataset`, `model`, or `space`.
```py
>>> from huggingface_hub import HfApi
>>> api = HfApi()
>>> api.upload_file( | 1,287 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | ```py
>>> from huggingface_hub import HfApi
>>> api = HfApi()
>>> api.upload_file(
... path_or_fileobj="/path/to/local/folder/README.md",
... path_in_repo="README.md",
... repo_id="username/test-dataset",
... repo_type="dataset",
... )
``` | 1,288 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | Use the [`upload_folder`] function to upload a local folder to an existing repository. Specify the path of the local folder
to upload, where you want to upload the folder to in the repository, and the name of the repository you want to add the
folder to. Depending on your repository type, you can optionally set the repository type as a `dataset`, `model`, or `space`.
```py
>>> from huggingface_hub import HfApi
>>> api = HfApi() | 1,289 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | # Upload all the content from the local folder to your remote Space.
# By default, files are uploaded at the root of the repo
>>> api.upload_folder(
... folder_path="/path/to/local/space",
... repo_id="username/my-cool-space",
... repo_type="space",
... )
``` | 1,290 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | ... folder_path="/path/to/local/space",
... repo_id="username/my-cool-space",
... repo_type="space",
... )
```
By default, the `.gitignore` file will be taken into account to know which files should be committed or not. By default we check if a `.gitignore` file is present in a commit, and if not, we check if it exists on the Hub. Please be aware that only a `.gitignore` file present at the root of the directory will be used. We do not check for `.gitignore` files in subdirectories. | 1,291 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | If you don't want to use an hardcoded `.gitignore` file, you can use the `allow_patterns` and `ignore_patterns` arguments to filter which files to upload. These parameters accept either a single pattern or a list of patterns. Patterns are Standard Wildcards (globbing patterns) as documented [here](https://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm). If both `allow_patterns` and `ignore_patterns` are provided, both constraints apply. | 1,292 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | Beside the `.gitignore` file and allow/ignore patterns, any `.git/` folder present in any subdirectory will be ignored.
```py
>>> api.upload_folder(
... folder_path="/path/to/local/folder",
... path_in_repo="my-dataset/train", # Upload to a specific folder
... repo_id="username/test-dataset",
... repo_type="dataset",
... ignore_patterns="**/logs/*.txt", # Ignore all text logs
... )
``` | 1,293 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | ... repo_type="dataset",
... ignore_patterns="**/logs/*.txt", # Ignore all text logs
... )
```
You can also use the `delete_patterns` argument to specify files you want to delete from the repo in the same commit.
This can prove useful if you want to clean a remote folder before pushing files in it and you don't know which files
already exists.
The example below uploads the local `./logs` folder to the remote `/experiment/logs/` folder. Only txt files are uploaded | 1,294 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | The example below uploads the local `./logs` folder to the remote `/experiment/logs/` folder. Only txt files are uploaded
but before that, all previous logs on the repo on deleted. All of this in a single commit.
```py
>>> api.upload_folder(
... folder_path="/path/to/local/folder/logs",
... repo_id="username/trained-model",
... path_in_repo="experiment/logs/",
... allow_patterns="*.txt", # Upload all local text files
... delete_patterns="*.txt", # Delete all remote text files before | 1,295 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | ... delete_patterns="*.txt", # Delete all remote text files before
... )
``` | 1,296 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | You can use the `huggingface-cli upload` command from the terminal to directly upload files to the Hub. Internally it uses the same [`upload_file`] and [`upload_folder`] helpers described above.
You can either upload a single file or an entire folder:
```bash
# Usage: huggingface-cli upload [repo_id] [local_path] [path_in_repo]
>>> huggingface-cli upload Wauplin/my-cool-model ./models/model.safetensors model.safetensors
https://huggingface.co/Wauplin/my-cool-model/blob/main/model.safetensors | 1,297 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | >>> huggingface-cli upload Wauplin/my-cool-model ./models .
https://huggingface.co/Wauplin/my-cool-model/tree/main
```
`local_path` and `path_in_repo` are optional and can be implicitly inferred. If `local_path` is not set, the tool will
check if a local folder or file has the same name as the `repo_id`. If that's the case, its content will be uploaded.
Otherwise, an exception is raised asking the user to explicitly set `local_path`. In any case, if `path_in_repo` is not | 1,298 |
/Users/nielsrogge/Documents/python_projecten/huggingface_hub/docs/source/en/guides/upload.md | .md | Otherwise, an exception is raised asking the user to explicitly set `local_path`. In any case, if `path_in_repo` is not
set, files are uploaded at the root of the repo.
For more details about the CLI upload command, please refer to the [CLI guide](./cli#huggingface-cli-upload). | 1,299 |