hysts HF staff commited on
Commit
6a22c0b
β€’
1 Parent(s): 979514d
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 +20 -52
.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: purple
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 3.0.5
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: purple
5
  colorTo: yellow
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,27 +18,10 @@ sys.path.insert(0, 'StyleGAN-Human')
19
  TITLE = 'StyleGAN-Human'
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
23
-
24
  Related App: [StyleGAN-Human (Interpolation)](https://huggingface.co/spaces/hysts/StyleGAN-Human-Interpolation)
25
  '''
26
- ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.stylegan-human" 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 generate_z(z_dim: int, seed: int, device: torch.device) -> torch.Tensor:
@@ -63,7 +45,7 @@ def generate_image(seed: int, truncation_psi: float, model: nn.Module,
63
  def load_model(file_name: str, device: torch.device) -> nn.Module:
64
  path = hf_hub_download('hysts/StyleGAN-Human',
65
  f'models/{file_name}',
66
- use_auth_token=TOKEN)
67
  with open(path, 'rb') as f:
68
  model = pickle.load(f)['G_ema']
69
  model.eval()
@@ -75,35 +57,21 @@ def load_model(file_name: str, device: torch.device) -> nn.Module:
75
  return model
76
 
77
 
78
- def main():
79
- args = parse_args()
80
- device = torch.device(args.device)
81
-
82
- model = load_model('stylegan_human_v2_1024.pkl', device)
83
-
84
- func = functools.partial(generate_image, model=model, device=device)
85
- func = functools.update_wrapper(func, generate_image)
86
-
87
- gr.Interface(
88
- func,
89
- [
90
- gr.inputs.Number(default=0, label='Seed'),
91
- gr.inputs.Slider(
92
- 0, 2, step=0.05, default=0.7, label='Truncation psi'),
93
- ],
94
- gr.outputs.Image(type='numpy', label='Output'),
95
- title=TITLE,
96
- description=DESCRIPTION,
97
- article=ARTICLE,
98
- theme=args.theme,
99
- allow_flagging=args.allow_flagging,
100
- live=args.live,
101
- ).launch(
102
- enable_queue=args.enable_queue,
103
- server_port=args.port,
104
- share=args.share,
105
- )
106
-
107
-
108
- if __name__ == '__main__':
109
- main()
 
2
 
3
  from __future__ import annotations
4
 
 
5
  import functools
6
  import os
7
  import pickle
 
18
  TITLE = 'StyleGAN-Human'
19
  DESCRIPTION = '''This is an unofficial demo for https://github.com/stylegan-human/StyleGAN-Human.
20
 
 
 
21
  Related App: [StyleGAN-Human (Interpolation)](https://huggingface.co/spaces/hysts/StyleGAN-Human-Interpolation)
22
  '''
 
 
 
 
23
 
24
+ HF_TOKEN = os.getenv('HF_TOKEN')
 
 
 
 
 
 
 
 
 
 
 
25
 
26
 
27
  def generate_z(z_dim: int, seed: int, device: torch.device) -> torch.Tensor:
 
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=HF_TOKEN)
49
  with open(path, 'rb') as f:
50
  model = pickle.load(f)['G_ema']
51
  model.eval()
 
57
  return model
58
 
59
 
60
+ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
61
+ model = load_model('stylegan_human_v2_1024.pkl', device)
62
+ func = functools.partial(generate_image, model=model, device=device)
63
+
64
+ gr.Interface(
65
+ fn=func,
66
+ inputs=[
67
+ gr.Slider(label='Seed', minimum=0, maximum=100000, step=1, value=0),
68
+ gr.Slider(label='Truncation psi',
69
+ minimum=0,
70
+ maximum=2,
71
+ step=0.05,
72
+ value=0.7),
73
+ ],
74
+ outputs=gr.Image(label='Output', type='numpy'),
75
+ title=TITLE,
76
+ description=DESCRIPTION,
77
+ ).launch(show_api=False)