-
Notifications
You must be signed in to change notification settings - Fork 551
Huggingface deployer #4119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
htahir1
wants to merge
29
commits into
develop
Choose a base branch
from
claude/huggingface-deployer-011CUj51UperMPAytgCheWG1
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,071
−2
Open
Huggingface deployer #4119
Changes from 17 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
9522252
Add Huggingface deployer for ZenML
claude 1be2a1b
Fix logical issues in Huggingface deployer
claude ab7bd79
Simplify Huggingface deployer implementation
claude fd8bf5d
Fix critical bugs in Huggingface deployer
claude ba9b41b
Standardize Hugging Face deployer with existing integration
claude 546eb96
Change deployer flavor name to 'huggingface'
claude 1bf6465
Fix linting and docstring errors
claude 73b157a
Add Hugging Face deployer documentation
claude 00915d7
Implement two-mode deployment and add entrypoint to Dockerfile
claude 919ca6c
Refactor to use ZenML's internal Dockerfile generation
claude 5b2ed08
Fix linting and docstring errors
claude c06146b
CRITICAL SECURITY FIX: Use HF Space Secrets/Variables API
claude 3d3d317
Add reference to HF Spaces GPU documentation
claude 9f92376
Address code review feedback for HuggingFace deployer
claude f08d8cc
Remove redundant secret_name parameter from HuggingFace deployer
claude 5a3595b
Add comprehensive Field descriptions to HuggingFace deployer config
claude 125ce99
Change HuggingFace Space default visibility to private for security
claude 1d84d2e
Update src/zenml/integrations/huggingface/deployers/huggingface_deplo…
htahir1 270f8e2
Address PR review feedback
claude d895d03
Address comprehensive PR review feedback from @stefannica
claude 986cbf4
Fix Space stage mapping to use only ZenML standard deployment states
claude ff9599c
Add missing DeploymentDeprovisionError import
claude 295f7b6
Fix deployment URL to use actual Space domain instead of HF page
claude 9fdadcc
Merge branch 'develop' into claude/huggingface-deployer-011CUj51UperM…
htahir1 f58c1fd
Update docs/book/component-guide/deployers/huggingface.md
htahir1 2fc937e
Check domain stage before returning RUNNING status
claude f43c6c1
Fix HfHubHTTPError import for mypy compatibility
claude da58e37
Refactor settings to follow deployer pattern
claude cf57df0
Add debug logging for deployment state detection
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| --- | ||
| description: Deploying your pipelines to Hugging Face Spaces. | ||
| --- | ||
|
|
||
| # Hugging Face Deployer | ||
|
|
||
| [Hugging Face Spaces](https://huggingface.co/spaces) is a platform for hosting and sharing machine learning applications. The Hugging Face deployer is a [deployer](./) flavor included in the ZenML Hugging Face integration that deploys your pipelines to Hugging Face Spaces as Docker-based applications. | ||
|
|
||
| {% hint style="warning" %} | ||
| This component is only meant to be used within the context of a [remote ZenML installation](https://docs.zenml.io/getting-started/deploying-zenml). Usage with a local ZenML setup may lead to unexpected behavior! | ||
| {% endhint %} | ||
|
|
||
| ## When to use it | ||
|
|
||
| You should use the Hugging Face deployer if: | ||
|
|
||
| * you're already using Hugging Face for model hosting or datasets. | ||
| * you want to share your ML pipelines as publicly accessible or private Spaces. | ||
| * you're looking for a simple, managed platform for deploying Docker-based applications. | ||
| * you want to leverage Hugging Face's infrastructure for hosting your pipeline deployments. | ||
| * you need an easy way to showcase ML workflows to the community. | ||
|
|
||
| ## How to deploy it | ||
|
|
||
| {% hint style="info" %} | ||
| The Hugging Face deployer requires a remote ZenML installation. You must ensure that you are connected to the remote ZenML server before using this stack component. | ||
| {% endhint %} | ||
|
|
||
| In order to use a Hugging Face deployer, you need to first deploy [ZenML to the cloud](https://docs.zenml.io/getting-started/deploying-zenml/). | ||
|
|
||
| The only other requirement is having a Hugging Face account and generating an access token with write permissions. | ||
|
|
||
| ## How to use it | ||
|
|
||
| To use the Hugging Face deployer, you need: | ||
|
|
||
| * The ZenML `huggingface` integration installed. If you haven't done so, run | ||
|
|
||
| ```shell | ||
| zenml integration install huggingface | ||
| ``` | ||
| * [Docker](https://www.docker.com) installed and running. | ||
| * A [remote artifact store](https://docs.zenml.io/stacks/artifact-stores/) as part of your stack. | ||
| * A [remote container registry](https://docs.zenml.io/stacks/container-registries/) as part of your stack. | ||
| * A [Hugging Face access token with write permissions](https://huggingface.co/settings/tokens) | ||
|
|
||
| ### Hugging Face credentials | ||
|
|
||
| You need a Hugging Face access token with write permissions to deploy pipelines. You can create one at [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens). | ||
|
|
||
| You have two options to provide credentials to the Hugging Face deployer: | ||
|
|
||
| * Pass the token directly when registering the deployer using the `--token` parameter | ||
| * (recommended) Store the token in a ZenML secret and reference it using secret syntax | ||
|
||
|
|
||
| ### Registering the deployer | ||
|
|
||
| The deployer can be registered as follows: | ||
|
|
||
| ```shell | ||
| # Option 1: Direct token (not recommended for production) | ||
| zenml deployer register <DEPLOYER_NAME> \ | ||
| --flavor=huggingface \ | ||
| --token=<YOUR_HF_TOKEN> | ||
|
|
||
| # Option 2: Using a secret (recommended) | ||
| zenml secret create hf_token --token=<YOUR_HF_TOKEN> | ||
| zenml deployer register <DEPLOYER_NAME> \ | ||
| --flavor=huggingface \ | ||
| --token='{{hf_token.token}}' | ||
| ``` | ||
|
|
||
| ### Configuring the stack | ||
|
|
||
| With the deployer registered, it can be used in the active stack: | ||
|
|
||
| ```shell | ||
| # Register and activate a stack with the new deployer | ||
| zenml stack register <STACK_NAME> -D <DEPLOYER_NAME> ... --set | ||
| ``` | ||
|
|
||
| {% hint style="info" %} | ||
| ZenML will build a Docker image called `<CONTAINER_REGISTRY_URI>/zenml:<PIPELINE_NAME>` which will be referenced in a Dockerfile deployed to your Hugging Face Space. Check out [this page](https://docs.zenml.io/how-to/customize-docker-builds/) if you want to learn more about how ZenML builds these images and how you can customize them. | ||
| {% endhint %} | ||
|
|
||
| You can now [deploy any ZenML pipeline](https://docs.zenml.io/concepts/deployment) using the Hugging Face deployer: | ||
|
|
||
| ```shell | ||
| zenml pipeline deploy --name my_deployment my_module.my_pipeline | ||
| ``` | ||
|
|
||
| ### Additional configuration | ||
|
|
||
| For additional configuration of the Hugging Face deployer, you can pass the following `HuggingFaceDeployerSettings` attributes defined in the `zenml.integrations.huggingface.flavors.huggingface_deployer_flavor` module when configuring the deployer or defining or deploying your pipeline: | ||
|
|
||
| * Basic settings common to all Deployers: | ||
|
|
||
| * `auth_key`: A user-defined authentication key to use to authenticate with deployment API calls. | ||
| * `generate_auth_key`: Whether to generate and use a random authentication key instead of the user-defined one. | ||
| * `lcm_timeout`: The maximum time in seconds to wait for the deployment lifecycle management to complete. | ||
|
|
||
| * Hugging Face Spaces-specific settings: | ||
|
|
||
| * `space_hardware` (default: `None`): Hardware tier for the Space (e.g., `'cpu-basic'`, `'cpu-upgrade'`, `'t4-small'`, `'t4-medium'`, `'a10g-small'`, `'a10g-large'`). If not specified, uses free CPU tier. See [Hugging Face Spaces GPU documentation](https://huggingface.co/docs/hub/spaces-gpus) for available options and pricing. | ||
| * `space_storage` (default: `None`): Persistent storage tier for the Space (e.g., `'small'`, `'medium'`, `'large'`). If not specified, no persistent storage is allocated. | ||
| * `private` (default: `True`): Whether to create the Space as private. Set to `False` to make the Space publicly visible to everyone. | ||
| * `app_port` (default: `8000`): Port number where your deployment server listens. Defaults to 8000 (ZenML server default). Hugging Face Spaces will route traffic to this port. | ||
|
|
||
| Check out [this docs page](https://docs.zenml.io/concepts/steps_and_pipelines/configuration) for more information on how to specify settings. | ||
|
|
||
| For example, if you wanted to deploy on GPU hardware with persistent storage, you would configure settings as follows: | ||
|
|
||
| ```python | ||
| from zenml.integrations.huggingface.deployers import HuggingFaceDeployerSettings | ||
|
|
||
| huggingface_settings = HuggingFaceDeployerSettings( | ||
| space_hardware="t4-small", | ||
| space_storage="small", | ||
| # private=True is the default for security | ||
| ) | ||
|
|
||
| @pipeline( | ||
| settings={ | ||
| "deployer": huggingface_settings | ||
| } | ||
| ) | ||
| def my_pipeline(...): | ||
| ... | ||
| ``` | ||
|
|
||
| ### Managing deployments | ||
|
|
||
| Once deployed, you can manage your deployments using the ZenML CLI: | ||
|
|
||
| ```shell | ||
| # List all deployments | ||
| zenml deployment list | ||
|
|
||
| # Get deployment status | ||
| zenml deployment describe <DEPLOYMENT_NAME> | ||
|
|
||
| # Get deployment logs | ||
| zenml deployment logs <DEPLOYMENT_NAME> | ||
|
|
||
| # Delete a deployment | ||
| zenml deployment delete <DEPLOYMENT_NAME> | ||
| ``` | ||
|
|
||
| The deployed pipeline will be available as a Hugging Face Space at: | ||
| ``` | ||
| https://huggingface.co/spaces/<YOUR_USERNAME>/<SPACE_PREFIX>-<DEPLOYMENT_NAME> | ||
| ``` | ||
|
|
||
| By default, the space prefix is `zenml` but this can be configured using the `space_prefix` parameter when registering the deployer. | ||
|
|
||
| ## Important Requirements | ||
|
|
||
| ### Secure Secrets and Environment Variables | ||
|
|
||
| {% hint style="success" %} | ||
| The Hugging Face deployer handles secrets and environment variables **securely** using Hugging Face's Space Secrets and Variables API. Credentials are **never** written to the Dockerfile. | ||
| {% endhint %} | ||
|
|
||
| **How it works:** | ||
| - Environment variables are set using `HfApi.add_space_variable()` - stored securely by Hugging Face | ||
| - Secrets are set using `HfApi.add_space_secret()` - encrypted and never exposed in the Space repository | ||
| - **Nothing is baked into the Dockerfile** - no risk of leaked credentials even in public Spaces | ||
|
|
||
| **What this means:** | ||
| - ✅ Safe to use with both private and public Spaces | ||
| - ✅ Secrets remain encrypted and hidden from view | ||
| - ✅ Environment variables are managed through HF's secure API | ||
| - ✅ No credentials exposed in Dockerfile or repository files | ||
|
|
||
| This secure approach ensures that if you choose to make your Space public (`private=False`), credentials remain protected and are never visible to anyone viewing your Space's repository. | ||
|
|
||
| ### Container Registry Requirement | ||
|
|
||
| {% hint style="warning" %} | ||
| The Hugging Face deployer **requires** a container registry to be part of your ZenML stack. The Docker image must be pre-built and pushed to a **publicly accessible** container registry. | ||
| {% endhint %} | ||
|
|
||
| **Why public access is required:** | ||
| Hugging Face Spaces cannot authenticate with private Docker registries when building Docker Spaces. The platform pulls your Docker image during the build process, which means it needs public access. | ||
|
|
||
| **Recommended registries:** | ||
| - [Docker Hub](https://hub.docker.com/) public repositories | ||
| - [GitHub Container Registry (GHCR)](https://ghcr.io) with public images | ||
| - Any other public container registry | ||
|
|
||
| **Example setup with GitHub Container Registry:** | ||
| ```shell | ||
| # Register a public container registry | ||
| zenml container-registry register ghcr_public \ | ||
| --flavor=default \ | ||
| --uri=ghcr.io/<your-github-username> | ||
|
|
||
| # Add it to your stack | ||
| zenml stack update <STACK_NAME> --container-registry=ghcr_public | ||
| ``` | ||
|
|
||
| ### Configuring iframe Embedding (X-Frame-Options) | ||
|
|
||
| By default, ZenML's deployment server sends an `X-Frame-Options` header that prevents the deployment UI from being embedded in iframes. This causes issues with Hugging Face Spaces, which displays deployments in an iframe. | ||
|
|
||
| **To fix this**, you must configure your pipeline's `DeploymentSettings` to disable the `X-Frame-Options` header: | ||
|
|
||
| ```python | ||
| from zenml import pipeline | ||
| from zenml.config import DeploymentSettings, SecureHeadersConfig | ||
|
|
||
| # Configure deployment settings | ||
| deployment_settings = DeploymentSettings( | ||
| app_title="My ZenML Pipeline", | ||
| app_description="ML pipeline deployed to Hugging Face Spaces", | ||
| app_version="1.0.0", | ||
| secure_headers=SecureHeadersConfig( | ||
| xfo=False, # Disable X-Frame-Options to allow iframe embedding | ||
| server=True, | ||
| hsts=False, | ||
| content=True, | ||
| referrer=True, | ||
| cache=True, | ||
| permissions=True, | ||
| ), | ||
| cors={ | ||
| "allow_origins": ["*"], | ||
| "allow_methods": ["GET", "POST", "OPTIONS"], | ||
| "allow_headers": ["*"], | ||
| "allow_credentials": False, | ||
| }, | ||
| ) | ||
|
|
||
| @pipeline( | ||
| name="my_hf_pipeline", | ||
| settings={"deployment": deployment_settings} | ||
| ) | ||
| def my_pipeline(): | ||
| # Your pipeline steps here | ||
| pass | ||
| ``` | ||
|
|
||
| Without this configuration, the Hugging Face Spaces UI will show a blank page or errors when trying to display your deployment. | ||
|
|
||
| ## Additional Resources | ||
|
|
||
| * [Hugging Face Spaces Documentation](https://huggingface.co/docs/hub/spaces) | ||
| * [Docker Spaces Guide](https://huggingface.co/docs/hub/spaces-sdks-docker) | ||
| * [Hugging Face Hardware Options](https://huggingface.co/docs/hub/spaces-gpus) | ||
| * [ZenML Deployment Concepts](https://docs.zenml.io/concepts/deployment) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Copyright (c) ZenML GmbH 2025. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at: | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
| # or implied. See the License for the specific language governing | ||
| # permissions and limitations under the License. | ||
| """Hugging Face deployers.""" | ||
|
|
||
| from zenml.integrations.huggingface.deployers.huggingface_deployer import ( | ||
| HuggingFaceDeployer, | ||
| HuggingFaceDeployerSettings, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "HuggingFaceDeployer", | ||
| "HuggingFaceDeployerSettings", | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.