Wauplin HF staff commited on
Commit
ac36ca5
1 Parent(s): 4989ade

Readme-based configuration

Browse files
README.md CHANGED
@@ -7,9 +7,35 @@ sdk: gradio
7
  sdk_version: 4.7.1
8
  app_file: app.py
9
  pinned: false
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  ---
11
 
12
- # Build a CI for your Spaces 🚀
13
 
14
  **Gradio Space CI** is a plugin (and package) to create ephemeral Spaces for each PR opened on your Space repo.
15
  The goal is to foster community contributions by making the review process as lean as possible.
@@ -42,10 +68,9 @@ Add the following line to it:
42
 
43
  ```bash
44
  # requirements.txt
45
- gradio-space-ci@git+https://huggingface.co/spaces/Wauplin/gradio-space-ci
46
  ```
47
 
48
-
49
  ### 2. Add a user token as `HF_TOKEN` secret
50
 
51
  1. Go to your [user settings page](https://huggingface.co/settings/tokens).
@@ -53,33 +78,60 @@ gradio-space-ci@git+https://huggingface.co/spaces/Wauplin/gradio-space-ci
53
  3. Go to your Space settings page.
54
  4. Add `HF_TOKEN` as a Space secret with your newly created token.
55
 
56
- (optional) You can also define a `SPACE_CI_SECRET` secret value that will be used to authenticate webhook calls. If you
57
- don't define one, a random secret value will be generated for you.
 
58
 
 
59
 
60
- ### 3. Configure CI in `app.py`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- Import the `gradio_space_ci` package and update the last line of your `app.py` script, just before launching the Gradio app.
 
 
63
 
64
  ```py
65
  # app.py
66
  import gradio as gr
67
- from gradio_space_ci import configure_space_ci
 
 
68
 
69
  # ANY gradio app
70
  with gr.Blocks() as demo:
71
  ...
72
-
73
- # Replace `demo.launch()` by `configure_space_ci(demo).launch()`.
74
- configure_space_ci(
75
- demo,
76
- trusted_authors=[], # space owners + manually trusted authors
77
- private="auto", # ephemeral spaces will have same visibility as the main space. Otherwise, set to `True` or `False` explicitly.
78
- variables="auto", # same variables as the main space. Otherwise, set to a `Dict[str, str]`.
79
- secrets=["HF_TOKEN"], # which secret do I want to copy from the main space? Can be a `List[str]`.
80
- hardware=None, # "cpu-basic" by default. Otherwise set to "auto" to have same hardware as the main space or any valid string value.
81
- storage=None, # no storage by default. Otherwise set to "auto" to have same storage as the main space or any valid string value.
82
- ).launch()
83
  ```
84
 
85
  And you're done! Ephemeral Spaces will be launched for each and every PR on your repo.
@@ -89,4 +141,5 @@ And you're done! Ephemeral Spaces will be launched for each and every PR on your
89
 
90
  - **Demo:** https://huggingface.co/spaces/Wauplin/gradio-space-ci
91
  - **README:** https://huggingface.co/spaces/Wauplin/gradio-space-ci/blob/main/README.md
92
- - **Questions and feedback:** https://huggingface.co/spaces/Wauplin/gradio-space-ci/discussions
 
 
7
  sdk_version: 4.7.1
8
  app_file: app.py
9
  pinned: false
10
+ space_ci:
11
+ trusted_authors:
12
+ - clefourrier
13
+ # ^ Only PRs from trusted authors are deployed in an ephemeral Space.
14
+ # Space owners are automatically trusted.
15
+ # It is possible to manually trust other authors by adding them as a list.
16
+ private: auto
17
+ # ^ Can be 'true', 'false' or 'auto'. If 'auto', the ephemeral Spaces will have same
18
+ # visibility as the main Space.
19
+ variables: auto
20
+ # ^ By default ('auto'), all variables from the main Space are copied to the ephemeral Space.
21
+ # Otherwise, you can define a key-value mapping with variables to set.
22
+ # Variables are injected as environment variable in the ephemeral Space and are public for anyone landing on the Space.
23
+ secrets:
24
+ - HF_TOKEN
25
+ # ^ A list of secrets that must be copied from the main Space to the ephemeral Spaces.
26
+ # Secrets are injected as environment variable in the ephemeral Space.
27
+ # Be careful with the 'trusted_authors' list when setting secrets.
28
+ hardware: cpu-basic
29
+ # ^ Defines which hardware to assign to the ephemeral Space.
30
+ # Can be set to 'auto' to assign the same hardware as the main Space.
31
+ # Defaults to 'cpu-basic' (free).
32
+ storage: null
33
+ # ^ Defines which storage to assign to the ephemeral Space.
34
+ # Can be set to 'auto' to assign the same storage as the main Space.
35
+ # Defaults to no storage (free).
36
  ---
37
 
38
+ # Configure a CI for your Spaces 🚀
39
 
40
  **Gradio Space CI** is a plugin (and package) to create ephemeral Spaces for each PR opened on your Space repo.
41
  The goal is to foster community contributions by making the review process as lean as possible.
 
68
 
69
  ```bash
70
  # requirements.txt
71
+ gradio-space-ci @ git+https://huggingface.co/spaces/Wauplin/gradio-space-ci@0.1.3
72
  ```
73
 
 
74
  ### 2. Add a user token as `HF_TOKEN` secret
75
 
76
  1. Go to your [user settings page](https://huggingface.co/settings/tokens).
 
78
  3. Go to your Space settings page.
79
  4. Add `HF_TOKEN` as a Space secret with your newly created token.
80
 
81
+ (optional) You can also define a `SPACE_CI_SECRET` secret value that will be used to authenticate webhook calls. If you don't define one, a random secret value will be generated for you.
82
+
83
+ ### 3. Configure CI in `README.md`
84
 
85
+ Edit the Space metadata in your `README.md` file:
86
 
87
+ ```yaml
88
+ title: ...
89
+ sdk: gradio
90
+ sdk_version: ...
91
+ app_file: ...
92
+ space_ci:
93
+ trusted_authors:
94
+ - Wauplin
95
+ # ^ Only PRs from trusted authors are deployed in an ephemeral Space.
96
+ # Space owners are automatically trusted.
97
+ # It is possible to manually trust other authors by adding them as a list.
98
+ private: auto
99
+ # ^ Can be 'true', 'false' or 'auto'. If 'auto', the ephemeral Spaces will have same
100
+ # visibility as the main Space.
101
+ variables: auto
102
+ # ^ By default ('auto'), all variables from the main Space are copied to the ephemeral Space.
103
+ # Otherwise, you can define a key-value mapping with variables to set.
104
+ # Variables are injected as environment variable in the ephemeral Space and are public for anyone landing on the Space.
105
+ secrets:
106
+ - HF_TOKEN
107
+ # ^ A list of secrets that must be copied from the main Space to the ephemeral Spaces.
108
+ # Secrets are injected as environment variable in the ephemeral Space.
109
+ # Be careful with the 'trusted_authors' list when setting secrets.
110
+ hardware: cpu-basic
111
+ # ^ Defines which hardware to assign to the ephemeral Space.
112
+ # Can be set to 'auto' to assign the same hardware as the main Space.
113
+ # Defaults to 'cpu-basic' (free).
114
+ storage: null
115
+ # ^ Defines which storage to assign to the ephemeral Space.
116
+ # Can be set to 'auto' to assign the same storage as the main Space.
117
+ # Defaults to no storage (free).
118
+ ```
119
 
120
+ ### 4. Enable CI in `app.py`
121
+
122
+ Call `enable_space_ci` in your `app.py` file:
123
 
124
  ```py
125
  # app.py
126
  import gradio as gr
127
+ from gradio_space_ci import enable_space_ci
128
+
129
+ enable_space_ci()
130
 
131
  # ANY gradio app
132
  with gr.Blocks() as demo:
133
  ...
134
+ demo.launch()
 
 
 
 
 
 
 
 
 
 
135
  ```
136
 
137
  And you're done! Ephemeral Spaces will be launched for each and every PR on your repo.
 
141
 
142
  - **Demo:** https://huggingface.co/spaces/Wauplin/gradio-space-ci
143
  - **README:** https://huggingface.co/spaces/Wauplin/gradio-space-ci/blob/main/README.md
144
+ - **Questions and feedback:** https://huggingface.co/spaces/Wauplin/gradio-space-ci/discussions
145
+ - **Release notes:** https://huggingface.co/spaces/Wauplin/gradio-space-ci/blob/main/RELEASE.md
app.py CHANGED
@@ -2,18 +2,11 @@
2
  from pathlib import Path
3
 
4
  import gradio as gr
5
- from gradio_space_ci import configure_space_ci
 
 
6
 
7
  with gr.Blocks() as demo:
8
  gr.Markdown(Path("README.md").read_text().split("---")[-1])
9
 
10
- if __name__ == "__main__":
11
- configure_space_ci(
12
- blocks=demo, # ANY gradio app
13
- trusted_authors=["clefourrier"], # space owners + manually trusted authors
14
- private="auto", # ephemeral spaces will have same visibility as the main space. Otherwise, set to `True` or `False` explicitly.
15
- variables="auto", # same variables as the main space. Otherwise, set to a `Dict[str, str]`.
16
- secrets=["HF_TOKEN"], # which secret do I want to copy from the main space? Can be a `List[str]`.
17
- hardware=None, # "cpu-basic" by default. Otherwise set to "auto" to have same hardware as the main space or any valid string value.
18
- storage=None, # no storage by default. Otherwise set to "auto" to have same storage as the main space or any valid string value.
19
- ).launch()
 
2
  from pathlib import Path
3
 
4
  import gradio as gr
5
+ from gradio_space_ci import enable_space_ci
6
+
7
+ enable_space_ci()
8
 
9
  with gr.Blocks() as demo:
10
  gr.Markdown(Path("README.md").read_text().split("---")[-1])
11
 
12
+ demo.launch()
 
 
 
 
 
 
 
 
 
src/gradio_space_ci/__init__.py CHANGED
@@ -20,21 +20,22 @@ The goal is to foster community contributions by making the review process as le
20
  - Demo: https://huggingface.co/spaces/Wauplin/gradio-space-ci
21
  - README: https://huggingface.co/spaces/Wauplin/gradio-space-ci/blob/main/README.md
22
  - Questions and feedback: https://huggingface.co/spaces/Wauplin/gradio-space-ci/discussions
 
23
  """
24
  import warnings
25
 
26
- from huggingface_hub import HfFolder
27
 
28
  # Check if `HF_TOKEN` is set. If not, Space CI will be disabled but no error is raised.
29
- if HfFolder.get_token() is None:
30
  warnings.warn(
31
  "Cannot find `HF_TOKEN` in environment variables. Please set a token in your Space secrets to enable ephemeral Spaces."
32
  )
33
 
34
- def configure_space_ci(blocks, *args, kwargs):
35
- return blocks
36
  else:
37
- from .webhook import configure_space_ci # noqa: F401
38
 
39
 
40
- __version__ = "0.1.2"
 
20
  - Demo: https://huggingface.co/spaces/Wauplin/gradio-space-ci
21
  - README: https://huggingface.co/spaces/Wauplin/gradio-space-ci/blob/main/README.md
22
  - Questions and feedback: https://huggingface.co/spaces/Wauplin/gradio-space-ci/discussions
23
+ - Release notes: https://huggingface.co/spaces/Wauplin/gradio-space-ci/blob/main/RELEASE.md
24
  """
25
  import warnings
26
 
27
+ from huggingface_hub import get_token
28
 
29
  # Check if `HF_TOKEN` is set. If not, Space CI will be disabled but no error is raised.
30
+ if get_token() is None:
31
  warnings.warn(
32
  "Cannot find `HF_TOKEN` in environment variables. Please set a token in your Space secrets to enable ephemeral Spaces."
33
  )
34
 
35
+ def enable_space_ci() -> None:
36
+ return
37
  else:
38
+ from .webhook import enable_space_ci # noqa: F401
39
 
40
 
41
+ __version__ = "0.1.3"
src/gradio_space_ci/webhook.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import uuid
3
  import warnings
4
  from concurrent.futures import ThreadPoolExecutor
 
5
  from pathlib import Path
6
  from typing import Any, Dict, List, Literal, Optional, TypedDict, Union
7
 
@@ -54,6 +55,50 @@ if SPACE_ID is not None: # If running in a Space (i.e. not locally)
54
  EPHEMERAL_SPACES_CONFIG: Dict[str, Any] = {}
55
 
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  def configure_space_ci(
58
  blocks: Optional["gr.Blocks"] = None,
59
  trusted_authors: Optional[List[str]] = None,
 
2
  import uuid
3
  import warnings
4
  from concurrent.futures import ThreadPoolExecutor
5
+ from functools import wraps
6
  from pathlib import Path
7
  from typing import Any, Dict, List, Literal, Optional, TypedDict, Union
8
 
 
55
  EPHEMERAL_SPACES_CONFIG: Dict[str, Any] = {}
56
 
57
 
58
+ def enable_space_ci() -> None:
59
+ """Enable Space CI for the current Space based on config from the README.md file.
60
+
61
+ Example:
62
+ ```py
63
+ import gradio as gr
64
+ from gradio_space_ci import enable_space_ci
65
+
66
+ enable_space_ci()
67
+
68
+ with gr.Blocks() as demo:
69
+ ...
70
+
71
+ demo.launch()
72
+ ```
73
+ """
74
+ if SPACE_ID is None:
75
+ print("Not in a Space: Space CI disabled.")
76
+ return
77
+
78
+ if IS_EPHEMERAL_SPACE:
79
+ print("In an ephemeral Space: Space CI disabled.")
80
+ return
81
+
82
+ card = RepoCard.load(repo_id_or_path=SPACE_ID, repo_type="space")
83
+ config = card.data.get("space_ci", {})
84
+ print(f"Enabling Space CI with config from README: {config}")
85
+
86
+ @wraps(gr.Blocks.launch)
87
+ def _launch(self: gr.Blocks, *args, **kwargs) -> None:
88
+ configure_space_ci(
89
+ blocks=self,
90
+ trusted_authors=config.get("trusted_authors"),
91
+ private=config.get("private", "auto"),
92
+ variables=config.get("variables", "auto"),
93
+ secrets=config.get("secrets"),
94
+ hardware=config.get("hardware"),
95
+ storage=config.get("storage"),
96
+ ).launch(*args, *kwargs)
97
+
98
+ # Monkey patch gradio
99
+ gr.Blocks.launch = _launch
100
+
101
+
102
  def configure_space_ci(
103
  blocks: Optional["gr.Blocks"] = None,
104
  trusted_authors: Optional[List[str]] = None,