Ray Chen commited on
Commit
8d705e7
·
2 Parent(s): 22ed5bf 2ff1844

New: Add dev mode and related config/workflow

Browse files
.github/tools/config.py CHANGED
@@ -2,10 +2,10 @@ from typing import Dict, Literal
2
 
3
  from model import Config
4
 
5
- UserName = Literal["default"]
6
 
7
- PRODUCTION_SPACE_CONFIG: Dict[UserName, Config] = {
8
- "default": {
9
  "space_name": "example",
10
  "space_owner": "jy-raychen",
11
  "space_readme": {
@@ -20,3 +20,36 @@ PRODUCTION_SPACE_CONFIG: Dict[UserName, Config] = {
20
  },
21
  }
22
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  from model import Config
4
 
5
+ ProductionSpace = Literal["production"]
6
 
7
+ PRODUCTION_SPACE_CONFIG: Dict[ProductionSpace, Config] = {
8
+ "production": {
9
  "space_name": "example",
10
  "space_owner": "jy-raychen",
11
  "space_readme": {
 
20
  },
21
  }
22
  }
23
+
24
+ UserName = Literal["default", "jhihruei"]
25
+
26
+ DEV_SPACE_CONFIG: Dict[UserName, Config] = {
27
+ "default": {
28
+ "space_name": "dev-space",
29
+ "space_owner": "jy-raychen",
30
+ "space_readme": {
31
+ "title": "Hugging Face Developing",
32
+ "emoji": "🏢",
33
+ "color_from": "blue",
34
+ "color_to": "indigo",
35
+ "sdk": "gradio",
36
+ "sdk_version": "4.36.0",
37
+ "app_file": "app.py",
38
+ "pinned": False,
39
+ },
40
+ },
41
+ "jhihruei": {
42
+ "space_name": "personal-dev",
43
+ "space_owner": "jy-raychen",
44
+ "space_readme": {
45
+ "title": "Personal Developing",
46
+ "emoji": "🏢",
47
+ "color_from": "yellow",
48
+ "color_to": "indigo",
49
+ "sdk": "gradio",
50
+ "sdk_version": "4.21.0",
51
+ "app_file": "app.py",
52
+ "pinned": False,
53
+ },
54
+ },
55
+ }
.github/tools/deploy_to_space.py CHANGED
@@ -2,7 +2,7 @@ import argparse
2
  import subprocess
3
  import sys
4
 
5
- from config import PRODUCTION_SPACE_CONFIG
6
  from model import Space
7
 
8
 
@@ -32,10 +32,24 @@ if __name__ == "__main__":
32
  parser.add_argument("--branch_name", required=True)
33
  parser.add_argument("--username", required=True)
34
  parser.add_argument("--user_token", required=True)
 
 
 
 
 
35
  args = parser.parse_args()
36
 
37
- # TODO: Use different space config when deploy to dev environment.
38
- space = Space(PRODUCTION_SPACE_CONFIG["default"])
 
 
 
 
 
 
 
 
 
39
 
40
  push_result = force_push_to_remote(
41
  space, args.username, args.user_token, args.branch_name
 
2
  import subprocess
3
  import sys
4
 
5
+ from config import DEV_SPACE_CONFIG, PRODUCTION_SPACE_CONFIG
6
  from model import Space
7
 
8
 
 
32
  parser.add_argument("--branch_name", required=True)
33
  parser.add_argument("--username", required=True)
34
  parser.add_argument("--user_token", required=True)
35
+ parser.add_argument("--mode", required=True, choices=["prod", "dev"])
36
+ parser.add_argument(
37
+ "--deployer",
38
+ help="The actor in Github Actions, it used to choose space config in dev mode.",
39
+ )
40
  args = parser.parse_args()
41
 
42
+ try:
43
+ if args.mode == "prod":
44
+ space = Space(PRODUCTION_SPACE_CONFIG["production"])
45
+ elif args.mode == "dev":
46
+ space_config = DEV_SPACE_CONFIG.get(
47
+ args.deployer, DEV_SPACE_CONFIG["default"]
48
+ )
49
+ space = Space(space_config)
50
+ except Exception:
51
+ print("Error: The space is not set correctly!")
52
+ sys.exit(1)
53
 
54
  push_result = force_push_to_remote(
55
  space, args.username, args.user_token, args.branch_name
.github/tools/generate_readme.py CHANGED
@@ -1,6 +1,7 @@
 
1
  import sys
2
 
3
- from config import PRODUCTION_SPACE_CONFIG
4
  from model import ReadMe, Space
5
 
6
  DEFAULT_USER = "jy-raychen"
@@ -22,7 +23,27 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
22
 
23
 
24
  if __name__ == "__main__":
25
- space = Space(PRODUCTION_SPACE_CONFIG["default"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  readme = readme_template(space.readme)
28
  print(readme)
 
1
+ import argparse
2
  import sys
3
 
4
+ from config import DEV_SPACE_CONFIG, PRODUCTION_SPACE_CONFIG
5
  from model import ReadMe, Space
6
 
7
  DEFAULT_USER = "jy-raychen"
 
23
 
24
 
25
  if __name__ == "__main__":
26
+ parser = argparse.ArgumentParser(
27
+ description="Generate Hugging Face space config file (i.e. README.md)."
28
+ )
29
+ parser.add_argument("--mode", required=True, choices=["prod", "dev"])
30
+ parser.add_argument(
31
+ "--deployer",
32
+ help="The actor in Github Actions, it used to choose space config in dev mode.",
33
+ )
34
+ args = parser.parse_args()
35
+
36
+ try:
37
+ if args.mode == "prod":
38
+ space = Space(PRODUCTION_SPACE_CONFIG["production"])
39
+ elif args.mode == "dev":
40
+ space_config = DEV_SPACE_CONFIG.get(
41
+ args.deployer, DEV_SPACE_CONFIG["default"]
42
+ )
43
+ space = Space(space_config)
44
+ except Exception:
45
+ print("Error: The space is not set correctly!")
46
+ sys.exit(1)
47
 
48
  readme = readme_template(space.readme)
49
  print(readme)
.github/workflows/deploy-to-dev.yaml ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Deploy Dev
2
+ run-name: Deploy to dev space
3
+
4
+ on:
5
+ push:
6
+ branches:
7
+ - '**'
8
+ - '!main'
9
+
10
+ concurrency:
11
+ # NOTE: The same actor will deploy to the same space, so we should cancel other earlier jobs.
12
+ group: ${{ github.workflow }}-${{ github.actor }}
13
+ cancel-in-progress: true
14
+
15
+ jobs:
16
+ run_deploy_tool:
17
+ runs-on: ubuntu-latest
18
+ permissions:
19
+ contents: read
20
+ steps:
21
+ - name: Check out repository code
22
+ uses: actions/checkout@v4
23
+ with:
24
+ fetch-depth: '0'
25
+ - name: Update Repo README.md
26
+ run: |
27
+ python3 .github/tools/generate_readme.py --mode="dev" --deployer="${{ github.actor }}" > README.md
28
+ git config --global user.email "github-actions[bot]@users.noreply.github.com"
29
+ git config --global user.name "GitHub Actions[bot]"
30
+ git add ./README.md
31
+ if [[ `git status --porcelain` ]]; then
32
+ git commit -m "[skip ci] update README.md"
33
+ fi
34
+ - name: Run deploy tool
35
+ run: |
36
+ python3 .github/tools/deploy_to_space.py \
37
+ --mode="dev" \
38
+ --deployer="${{ github.actor }}" \
39
+ --branch_name="${{ github.ref_name }}" \
40
+ --username="${{ vars.HF_USERNAME }}" \
41
+ --user_token="${{ secrets.HF_TOKEN }}"
.github/workflows/deploy-to-production.yaml CHANGED
@@ -22,7 +22,7 @@ jobs:
22
  fetch-depth: '0'
23
  - name: Update Repo README.md
24
  run: |
25
- python3 .github/tools/generate_readme.py > README.md
26
  git config --global user.email "github-actions[bot]@users.noreply.github.com"
27
  git config --global user.name "GitHub Actions[bot]"
28
  git add ./README.md
@@ -30,4 +30,9 @@ jobs:
30
  git commit -m "[skip ci] update README.md"
31
  fi
32
  - name: Run deploy tool
33
- run: python3 .github/tools/deploy_to_space.py --branch_name="${{ github.ref_name }}" --username="${{ vars.HF_USERNAME }}" --user_token="${{ secrets.HF_TOKEN }}"
 
 
 
 
 
 
22
  fetch-depth: '0'
23
  - name: Update Repo README.md
24
  run: |
25
+ python3 .github/tools/generate_readme.py --mode="prod" > README.md
26
  git config --global user.email "github-actions[bot]@users.noreply.github.com"
27
  git config --global user.name "GitHub Actions[bot]"
28
  git add ./README.md
 
30
  git commit -m "[skip ci] update README.md"
31
  fi
32
  - name: Run deploy tool
33
+ run: |
34
+ python3 .github/tools/deploy_to_space.py \
35
+ --mode="prod" \
36
+ --branch_name="${{ github.ref_name }}" \
37
+ --username="${{ vars.HF_USERNAME }}" \
38
+ --user_token="${{ secrets.HF_TOKEN }}"