hysts HF staff commited on
Commit
a6dde31
1 Parent(s): c2dc3db
Files changed (4) hide show
  1. .pre-commit-config.yaml +35 -0
  2. .style.yapf +5 -0
  3. README.md +1 -1
  4. app.py +43 -62
.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,7 +4,7 @@ emoji: 🏢
4
  colorFrom: blue
5
  colorTo: gray
6
  sdk: gradio
7
- sdk_version: 3.0.5
8
  app_file: app.py
9
  pinned: false
10
  ---
4
  colorFrom: blue
5
  colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 3.19.1
8
  app_file: app.py
9
  pinned: false
10
  ---
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
@@ -19,33 +18,16 @@ sys.path.insert(0, 'StyleGAN-Human')
19
  TITLE = 'StyleGAN-Human (Interpolation)'
20
  DESCRIPTION = '''This is an unofficial demo for https://github.com/stylegan-human/StyleGAN-Human.
21
 
22
- Expected execution time on Hugging Face Spaces: 0.8s for one image
23
-
24
  Related App: [StyleGAN-Human](https://huggingface.co/spaces/hysts/StyleGAN-Human)
25
  '''
26
- ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.stylegan-human-interpolation" alt="visitor badge"/></center>'
27
-
28
- TOKEN = os.environ['TOKEN']
29
-
30
 
31
- def parse_args() -> argparse.Namespace:
32
- parser = argparse.ArgumentParser()
33
- parser.add_argument('--device', type=str, default='cpu')
34
- parser.add_argument('--theme', type=str)
35
- parser.add_argument('--live', action='store_true')
36
- parser.add_argument('--share', action='store_true')
37
- parser.add_argument('--port', type=int)
38
- parser.add_argument('--disable-queue',
39
- dest='enable_queue',
40
- action='store_false')
41
- parser.add_argument('--allow-flagging', type=str, default='never')
42
- return parser.parse_args()
43
 
44
 
45
  def load_model(file_name: str, device: torch.device) -> nn.Module:
46
  path = hf_hub_download('hysts/StyleGAN-Human',
47
  f'models/{file_name}',
48
- use_auth_token=TOKEN)
49
  with open(path, 'rb') as f:
50
  model = pickle.load(f)['G_ema']
51
  model.eval()
@@ -90,45 +72,44 @@ def generate_interpolated_images(seed0: int, psi0: float, seed1: int,
90
  return res
91
 
92
 
93
- def main():
94
- args = parse_args()
95
- device = torch.device(args.device)
96
-
97
- model = load_model('stylegan_human_v2_1024.pkl', device)
98
-
99
- func = functools.partial(generate_interpolated_images,
100
- model=model,
101
- device=device)
102
- func = functools.update_wrapper(func, generate_interpolated_images)
103
-
104
- gr.Interface(
105
- func,
106
- [
107
- gr.inputs.Number(default=0, label='Seed 1'),
108
- gr.inputs.Slider(
109
- 0, 2, step=0.05, default=0.7, label='Truncation psi 1'),
110
- gr.inputs.Number(default=1, label='Seed 2'),
111
- gr.inputs.Slider(
112
- 0, 2, step=0.05, default=0.7, label='Truncation psi 2'),
113
- gr.inputs.Slider(0,
114
- 21,
115
- step=1,
116
- default=7,
117
- label='Number of Intermediate Frames'),
118
- ],
119
- gr.Gallery(type='numpy', label='Output Images'),
120
- title=TITLE,
121
- description=DESCRIPTION,
122
- article=ARTICLE,
123
- theme=args.theme,
124
- allow_flagging=args.allow_flagging,
125
- live=args.live,
126
- ).launch(
127
- enable_queue=args.enable_queue,
128
- server_port=args.port,
129
- share=args.share,
130
- )
131
-
132
-
133
- if __name__ == '__main__':
134
- main()
2
 
3
  from __future__ import annotations
4
 
 
5
  import functools
6
  import os
7
  import pickle
18
  TITLE = 'StyleGAN-Human (Interpolation)'
19
  DESCRIPTION = '''This is an unofficial demo for https://github.com/stylegan-human/StyleGAN-Human.
20
 
 
 
21
  Related App: [StyleGAN-Human](https://huggingface.co/spaces/hysts/StyleGAN-Human)
22
  '''
 
 
 
 
23
 
24
+ HF_TOKEN = os.getenv('HF_TOKEN')
 
 
 
 
 
 
 
 
 
 
 
25
 
26
 
27
  def load_model(file_name: str, device: torch.device) -> nn.Module:
28
  path = hf_hub_download('hysts/StyleGAN-Human',
29
  f'models/{file_name}',
30
+ use_auth_token=HF_TOKEN)
31
  with open(path, 'rb') as f:
32
  model = pickle.load(f)['G_ema']
33
  model.eval()
72
  return res
73
 
74
 
75
+ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
76
+ model = load_model('stylegan_human_v2_1024.pkl', device)
77
+ func = functools.partial(generate_interpolated_images,
78
+ model=model,
79
+ device=device)
80
+
81
+ gr.Interface(
82
+ fn=func,
83
+ inputs=[
84
+ gr.Slider(label='Seed 1',
85
+ minimum=0,
86
+ maximum=100000,
87
+ step=1,
88
+ value=0,
89
+ randomize=True),
90
+ gr.Slider(label='Truncation psi 1',
91
+ minimum=0,
92
+ maximum=2,
93
+ step=0.05,
94
+ value=0.7),
95
+ gr.Slider(label='Seed 2',
96
+ minimum=0,
97
+ maximum=100000,
98
+ step=1,
99
+ value=1,
100
+ randomize=True),
101
+ gr.Slider(label='Truncation psi 2',
102
+ minimum=0,
103
+ maximum=2,
104
+ step=0.05,
105
+ value=0.7),
106
+ gr.Slider(label='Number of Intermediate Frames',
107
+ minimum=0,
108
+ maximum=21,
109
+ step=1,
110
+ value=7),
111
+ ],
112
+ outputs=gr.Gallery(label='Output Images', type='numpy'),
113
+ title=TITLE,
114
+ description=DESCRIPTION,
115
+ ).launch(show_api=False)