hysts HF staff commited on
Commit
1a719a6
1 Parent(s): ebc859a
Files changed (4) hide show
  1. .pre-commit-config.yaml +35 -0
  2. .style.yapf +5 -0
  3. README.md +11 -11
  4. app.py +37 -53
.pre-commit-config.yaml ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.2.0
4
+ hooks:
5
+ - id: check-executables-have-shebangs
6
+ - id: check-json
7
+ - id: check-merge-conflict
8
+ - id: check-shebang-scripts-are-executable
9
+ - id: check-toml
10
+ - id: check-yaml
11
+ - id: double-quote-string-fixer
12
+ - id: end-of-file-fixer
13
+ - id: mixed-line-ending
14
+ args: ['--fix=lf']
15
+ - id: requirements-txt-fixer
16
+ - id: trailing-whitespace
17
+ - repo: https://github.com/myint/docformatter
18
+ rev: v1.4
19
+ hooks:
20
+ - id: docformatter
21
+ args: ['--in-place']
22
+ - repo: https://github.com/pycqa/isort
23
+ rev: 5.12.0
24
+ hooks:
25
+ - id: isort
26
+ - repo: https://github.com/pre-commit/mirrors-mypy
27
+ rev: v0.991
28
+ hooks:
29
+ - id: mypy
30
+ args: ['--ignore-missing-imports']
31
+ - repo: https://github.com/google/yapf
32
+ rev: v0.32.0
33
+ hooks:
34
+ - id: yapf
35
+ args: ['--parallel', '--in-place']
.style.yapf ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ [style]
2
+ based_on_style = pep8
3
+ blank_line_before_nested_class_or_def = false
4
+ spaces_before_comment = 2
5
+ split_before_logical_operator = true
README.md CHANGED
@@ -4,35 +4,35 @@ emoji: 📚
4
  colorFrom: red
5
  colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 3.0.5
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
  # Configuration
13
 
14
- `title`: _string_
15
  Display title for the Space
16
 
17
- `emoji`: _string_
18
  Space emoji (emoji-only character allowed)
19
 
20
- `colorFrom`: _string_
21
  Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
22
 
23
- `colorTo`: _string_
24
  Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
25
 
26
- `sdk`: _string_
27
  Can be either `gradio` or `streamlit`
28
 
29
- `sdk_version` : _string_
30
- Only applicable for `streamlit` SDK.
31
  See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
32
 
33
- `app_file`: _string_
34
- Path to your main application file (which contains either `gradio` or `streamlit` Python code).
35
  Path is relative to the root of the repository.
36
 
37
- `pinned`: _boolean_
38
  Whether the Space stays on top of your list.
 
4
  colorFrom: red
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 3.19.1
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
  # Configuration
13
 
14
+ `title`: _string_
15
  Display title for the Space
16
 
17
+ `emoji`: _string_
18
  Space emoji (emoji-only character allowed)
19
 
20
+ `colorFrom`: _string_
21
  Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
22
 
23
+ `colorTo`: _string_
24
  Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
25
 
26
+ `sdk`: _string_
27
  Can be either `gradio` or `streamlit`
28
 
29
+ `sdk_version` : _string_
30
+ Only applicable for `streamlit` SDK.
31
  See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
32
 
33
+ `app_file`: _string_
34
+ Path to your main application file (which contains either `gradio` or `streamlit` Python code).
35
  Path is relative to the root of the repository.
36
 
37
+ `pinned`: _boolean_
38
  Whether the Space stays on top of your list.
app.py CHANGED
@@ -2,7 +2,6 @@
2
 
3
  from __future__ import annotations
4
 
5
- import argparse
6
  import functools
7
  import os
8
  import pickle
@@ -17,27 +16,11 @@ from huggingface_hub import hf_hub_download
17
  sys.path.insert(0, 'stylegan3')
18
 
19
  TITLE = 'StyleGAN3 Anime Face Generation'
20
- DESCRIPTION = 'Expected execution time on Hugging Face Spaces: 20s'
21
- ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.stylegan3-anime-face-generation-exp001" alt="visitor badge"/></center>'
22
 
23
  MODEL_REPO = 'hysts/stylegan3-anime-face-exp001-model'
24
  MODEL_FILE_NAME = '006600.pkl'
25
 
26
- TOKEN = os.environ['TOKEN']
27
-
28
-
29
- def parse_args() -> argparse.Namespace:
30
- parser = argparse.ArgumentParser()
31
- parser.add_argument('--device', type=str, default='cpu')
32
- parser.add_argument('--theme', type=str)
33
- parser.add_argument('--live', action='store_true')
34
- parser.add_argument('--share', action='store_true')
35
- parser.add_argument('--port', type=int)
36
- parser.add_argument('--disable-queue',
37
- dest='enable_queue',
38
- action='store_false')
39
- parser.add_argument('--allow-flagging', type=str, default='never')
40
- return parser.parse_args()
41
 
42
 
43
  def make_transform(translate: tuple[float, float], angle: float) -> np.ndarray:
@@ -76,7 +59,9 @@ def generate_image(seed: int, truncation_psi: float, tx: float, ty: float,
76
 
77
 
78
  def load_model(device: torch.device) -> nn.Module:
79
- path = hf_hub_download(MODEL_REPO, MODEL_FILE_NAME, use_auth_token=TOKEN)
 
 
80
  with open(path, 'rb') as f:
81
  model = pickle.load(f)
82
  model.eval()
@@ -88,37 +73,36 @@ def load_model(device: torch.device) -> nn.Module:
88
  return model
89
 
90
 
91
- def main():
92
- args = parse_args()
93
- device = torch.device(args.device)
94
-
95
- model = load_model(device)
96
- func = functools.partial(generate_image, model=model, device=device)
97
- func = functools.update_wrapper(func, generate_image)
98
-
99
- gr.Interface(
100
- func,
101
- [
102
- gr.inputs.Number(default=3407851645, label='Seed'),
103
- gr.inputs.Slider(
104
- 0, 2, step=0.05, default=0.7, label='Truncation psi'),
105
- gr.inputs.Slider(-1, 1, step=0.05, default=0, label='Translate X'),
106
- gr.inputs.Slider(-1, 1, step=0.05, default=0, label='Translate Y'),
107
- gr.inputs.Slider(-180, 180, step=5, default=0, label='Angle'),
108
- ],
109
- gr.outputs.Image(type='numpy', label='Output'),
110
- title=TITLE,
111
- description=DESCRIPTION,
112
- article=ARTICLE,
113
- theme=args.theme,
114
- allow_flagging='never',
115
- live=args.live,
116
- ).launch(
117
- enable_queue=args.enable_queue,
118
- server_port=args.port,
119
- share=args.share,
120
- )
121
-
122
-
123
- if __name__ == '__main__':
124
- main()
 
2
 
3
  from __future__ import annotations
4
 
 
5
  import functools
6
  import os
7
  import pickle
 
16
  sys.path.insert(0, 'stylegan3')
17
 
18
  TITLE = 'StyleGAN3 Anime Face Generation'
 
 
19
 
20
  MODEL_REPO = 'hysts/stylegan3-anime-face-exp001-model'
21
  MODEL_FILE_NAME = '006600.pkl'
22
 
23
+ HF_TOKEN = os.getenv('HF_TOKEN')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
 
26
  def make_transform(translate: tuple[float, float], angle: float) -> np.ndarray:
 
59
 
60
 
61
  def load_model(device: torch.device) -> nn.Module:
62
+ path = hf_hub_download(MODEL_REPO,
63
+ MODEL_FILE_NAME,
64
+ use_auth_token=HF_TOKEN)
65
  with open(path, 'rb') as f:
66
  model = pickle.load(f)
67
  model.eval()
 
73
  return model
74
 
75
 
76
+ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
77
+
78
+ model = load_model(device)
79
+ func = functools.partial(generate_image, model=model, device=device)
80
+
81
+ gr.Interface(
82
+ fn=func,
83
+ inputs=[
84
+ gr.Slider(label='Seed',
85
+ minimum=0,
86
+ maximum=10000000000,
87
+ step=1,
88
+ value=3407851645),
89
+ gr.Slider(label='Truncation psi',
90
+ minimum=0,
91
+ maximum=2,
92
+ step=0.05,
93
+ value=0.7),
94
+ gr.Slider(label='Translate X',
95
+ minimum=-1,
96
+ maximum=1,
97
+ step=0.05,
98
+ value=0),
99
+ gr.Slider(label='Translate Y',
100
+ minimum=-1,
101
+ maximum=1,
102
+ step=0.05,
103
+ value=0),
104
+ gr.Slider(label='Angle', minimum=-180, maximum=180, step=5, value=0),
105
+ ],
106
+ outputs=gr.Image(label='Output', type='numpy'),
107
+ title=TITLE,
108
+ ).queue().launch(show_api=False)