hysts HF staff commited on
Commit
b4a6915
·
1 Parent(s): d118e8d
Files changed (8) hide show
  1. .pre-commit-config.yaml +36 -0
  2. .style.yapf +5 -0
  3. README.md +2 -1
  4. app.py +55 -53
  5. patch +0 -54
  6. patch-cpu +54 -0
  7. requirements.txt +4 -4
  8. style.css +7 -0
.pre-commit-config.yaml ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ exclude: ^patch
2
+ repos:
3
+ - repo: https://github.com/pre-commit/pre-commit-hooks
4
+ rev: v4.2.0
5
+ hooks:
6
+ - id: check-executables-have-shebangs
7
+ - id: check-json
8
+ - id: check-merge-conflict
9
+ - id: check-shebang-scripts-are-executable
10
+ - id: check-toml
11
+ - id: check-yaml
12
+ - id: double-quote-string-fixer
13
+ - id: end-of-file-fixer
14
+ - id: mixed-line-ending
15
+ args: ['--fix=lf']
16
+ - id: requirements-txt-fixer
17
+ - id: trailing-whitespace
18
+ - repo: https://github.com/myint/docformatter
19
+ rev: v1.4
20
+ hooks:
21
+ - id: docformatter
22
+ args: ['--in-place']
23
+ - repo: https://github.com/pycqa/isort
24
+ rev: 5.12.0
25
+ hooks:
26
+ - id: isort
27
+ - repo: https://github.com/pre-commit/mirrors-mypy
28
+ rev: v0.991
29
+ hooks:
30
+ - id: mypy
31
+ args: ['--ignore-missing-imports']
32
+ - repo: https://github.com/google/yapf
33
+ rev: v0.32.0
34
+ hooks:
35
+ - id: yapf
36
+ 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,9 +4,10 @@ emoji: 🏃
4
  colorFrom: green
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 3.0.17
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
 
4
  colorFrom: green
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 3.36.1
8
  app_file: app.py
9
  pinned: false
10
+ suggested_hardware: t4-small
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
app.py CHANGED
@@ -2,9 +2,10 @@
2
 
3
  from __future__ import annotations
4
 
5
- import argparse
6
  import functools
7
  import os
 
 
8
  import subprocess
9
  import sys
10
 
@@ -16,16 +17,20 @@ from huggingface_hub import hf_hub_download
16
 
17
  if os.environ.get('SYSTEM') == 'spaces':
18
  with open('patch') as f:
19
- subprocess.run('patch -p1'.split(), cwd='stylegan2-pytorch', stdin=f)
 
 
 
 
 
 
 
20
 
21
  sys.path.insert(0, 'stylegan2-pytorch')
22
 
23
  from model import Generator
24
 
25
- TITLE = 'TADNE (This Anime Does Not Exist)'
26
- DESCRIPTION = '''The original TADNE site is https://thisanimedoesnotexist.ai/.
27
-
28
- Expected execution time on Hugging Face Spaces: 4s
29
 
30
  Related Apps:
31
  - [TADNE Image Viewer](https://huggingface.co/spaces/hysts/TADNE-image-viewer)
@@ -39,28 +44,20 @@ ARTICLE = f'''## Generated images
39
  - truncation: 0.7
40
  - seed: 0-99
41
  ![samples]({SAMPLE_IMAGE_DIR}/sample.jpg)
42
-
43
- <center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.tadne" alt="visitor badge"/></center>
44
  '''
45
 
 
 
46
 
47
- def parse_args() -> argparse.Namespace:
48
- parser = argparse.ArgumentParser()
49
- parser.add_argument('--device', type=str, default='cpu')
50
- parser.add_argument('--theme', type=str)
51
- parser.add_argument('--live', action='store_true')
52
- parser.add_argument('--share', action='store_true')
53
- parser.add_argument('--port', type=int)
54
- parser.add_argument('--disable-queue',
55
- dest='enable_queue',
56
- action='store_false')
57
- parser.add_argument('--allow-flagging', type=str, default='never')
58
- return parser.parse_args()
59
 
60
 
61
  def load_model(device: torch.device) -> nn.Module:
62
  model = Generator(512, 1024, 4, channel_multiplier=2)
63
- path = hf_hub_download('hysts/TADNE',
64
  'models/aydao-anime-danbooru2019s-512-5268480.pt')
65
  checkpoint = torch.load(path)
66
  model.load_state_dict(checkpoint['g_ema'])
@@ -92,36 +89,41 @@ def generate_image(seed: int, truncation_psi: float, randomize_noise: bool,
92
  return out[0].cpu().numpy()
93
 
94
 
95
- def main():
96
- args = parse_args()
97
- device = torch.device(args.device)
98
-
99
- model = load_model(device)
100
-
101
- func = functools.partial(generate_image, model=model, device=device)
102
- func = functools.update_wrapper(func, generate_image)
103
-
104
- gr.Interface(
105
- func,
106
- [
107
- gr.inputs.Number(default=55376, label='Seed'),
108
- gr.inputs.Slider(
109
- 0, 2, step=0.05, default=0.7, label='Truncation psi'),
110
- gr.inputs.Checkbox(default=False, label='Randomize Noise'),
111
- ],
112
- gr.outputs.Image(type='numpy', label='Output'),
113
- title=TITLE,
114
- description=DESCRIPTION,
115
- article=ARTICLE,
116
- theme=args.theme,
117
- allow_flagging=args.allow_flagging,
118
- live=args.live,
119
- ).launch(
120
- enable_queue=args.enable_queue,
121
- server_port=args.port,
122
- share=args.share,
 
 
 
 
 
 
 
 
123
  )
124
-
125
-
126
- if __name__ == '__main__':
127
- main()
 
2
 
3
  from __future__ import annotations
4
 
 
5
  import functools
6
  import os
7
+ import random
8
+ import shlex
9
  import subprocess
10
  import sys
11
 
 
17
 
18
  if os.environ.get('SYSTEM') == 'spaces':
19
  with open('patch') as f:
20
+ subprocess.run(shlex.split('patch -p1'),
21
+ cwd='stylegan2-pytorch',
22
+ stdin=f)
23
+ if not torch.cuda.is_available():
24
+ with open('patch-cpu') as f:
25
+ subprocess.run(shlex.split('patch -p1'),
26
+ cwd='stylegan2-pytorch',
27
+ stdin=f)
28
 
29
  sys.path.insert(0, 'stylegan2-pytorch')
30
 
31
  from model import Generator
32
 
33
+ DESCRIPTION = '''# [TADNE](https://thisanimedoesnotexist.ai/) (This Anime Does Not Exist)
 
 
 
34
 
35
  Related Apps:
36
  - [TADNE Image Viewer](https://huggingface.co/spaces/hysts/TADNE-image-viewer)
 
44
  - truncation: 0.7
45
  - seed: 0-99
46
  ![samples]({SAMPLE_IMAGE_DIR}/sample.jpg)
 
 
47
  '''
48
 
49
+ MAX_SEED = np.iinfo(np.int32).max
50
+
51
 
52
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
53
+ if randomize_seed:
54
+ seed = random.randint(0, MAX_SEED)
55
+ return seed
 
 
 
 
 
 
 
 
56
 
57
 
58
  def load_model(device: torch.device) -> nn.Module:
59
  model = Generator(512, 1024, 4, channel_multiplier=2)
60
+ path = hf_hub_download('public-data/TADNE',
61
  'models/aydao-anime-danbooru2019s-512-5268480.pt')
62
  checkpoint = torch.load(path)
63
  model.load_state_dict(checkpoint['g_ema'])
 
89
  return out[0].cpu().numpy()
90
 
91
 
92
+ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
93
+ model = load_model(device)
94
+ fn = functools.partial(generate_image, model=model, device=device)
95
+
96
+ with gr.Blocks(css='style.css') as demo:
97
+ gr.Markdown(DESCRIPTION)
98
+ with gr.Row():
99
+ with gr.Column():
100
+ seed = gr.Slider(label='Seed',
101
+ minimum=0,
102
+ maximum=MAX_SEED,
103
+ step=1,
104
+ value=0)
105
+ randomize_seed = gr.Checkbox(label='Randomize seed', value=True)
106
+ psi = gr.Slider(label='Truncation psi',
107
+ minimum=0,
108
+ maximum=2,
109
+ step=0.05,
110
+ value=0.7)
111
+ randomize_noise = gr.Checkbox(label='Randomize Noise', value=False)
112
+ run_button = gr.Button('Run')
113
+ with gr.Column():
114
+ result = gr.Image(label='Output')
115
+ gr.Markdown(ARTICLE)
116
+
117
+ run_button.click(
118
+ fn=randomize_seed_fn,
119
+ inputs=[seed, randomize_seed],
120
+ outputs=seed,
121
+ queue=False,
122
+ api_name=False,
123
+ ).then(
124
+ fn=fn,
125
+ inputs=[seed, psi, randomize_noise],
126
+ outputs=result,
127
+ api_name='run',
128
  )
129
+ demo.queue(max_size=10).launch()
 
 
 
patch CHANGED
@@ -29,57 +29,3 @@ index 0134c39..3a7826c 100755
29
  style_t = []
30
 
31
  for style in styles:
32
- diff --git a/op/fused_act.py b/op/fused_act.py
33
- index 5d46e10..bc522ed 100755
34
- --- a/op/fused_act.py
35
- +++ b/op/fused_act.py
36
- @@ -1,5 +1,3 @@
37
- -import os
38
- -
39
- import torch
40
- from torch import nn
41
- from torch.nn import functional as F
42
- @@ -7,16 +5,6 @@ from torch.autograd import Function
43
- from torch.utils.cpp_extension import load
44
-
45
-
46
- -module_path = os.path.dirname(__file__)
47
- -fused = load(
48
- - "fused",
49
- - sources=[
50
- - os.path.join(module_path, "fused_bias_act.cpp"),
51
- - os.path.join(module_path, "fused_bias_act_kernel.cu"),
52
- - ],
53
- -)
54
- -
55
- -
56
- class FusedLeakyReLUFunctionBackward(Function):
57
- @staticmethod
58
- def forward(ctx, grad_output, out, bias, negative_slope, scale):
59
- diff --git a/op/upfirdn2d.py b/op/upfirdn2d.py
60
- index 67e0375..6c5840e 100755
61
- --- a/op/upfirdn2d.py
62
- +++ b/op/upfirdn2d.py
63
- @@ -1,5 +1,4 @@
64
- from collections import abc
65
- -import os
66
-
67
- import torch
68
- from torch.nn import functional as F
69
- @@ -7,16 +6,6 @@ from torch.autograd import Function
70
- from torch.utils.cpp_extension import load
71
-
72
-
73
- -module_path = os.path.dirname(__file__)
74
- -upfirdn2d_op = load(
75
- - "upfirdn2d",
76
- - sources=[
77
- - os.path.join(module_path, "upfirdn2d.cpp"),
78
- - os.path.join(module_path, "upfirdn2d_kernel.cu"),
79
- - ],
80
- -)
81
- -
82
- -
83
- class UpFirDn2dBackward(Function):
84
- @staticmethod
85
- def forward(
 
29
  style_t = []
30
 
31
  for style in styles:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
patch-cpu ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ diff --git a/op/fused_act.py b/op/fused_act.py
2
+ index 5d46e10..bc522ed 100755
3
+ --- a/op/fused_act.py
4
+ +++ b/op/fused_act.py
5
+ @@ -1,5 +1,3 @@
6
+ -import os
7
+ -
8
+ import torch
9
+ from torch import nn
10
+ from torch.nn import functional as F
11
+ @@ -7,16 +5,6 @@ from torch.autograd import Function
12
+ from torch.utils.cpp_extension import load
13
+
14
+
15
+ -module_path = os.path.dirname(__file__)
16
+ -fused = load(
17
+ - "fused",
18
+ - sources=[
19
+ - os.path.join(module_path, "fused_bias_act.cpp"),
20
+ - os.path.join(module_path, "fused_bias_act_kernel.cu"),
21
+ - ],
22
+ -)
23
+ -
24
+ -
25
+ class FusedLeakyReLUFunctionBackward(Function):
26
+ @staticmethod
27
+ def forward(ctx, grad_output, out, bias, negative_slope, scale):
28
+ diff --git a/op/upfirdn2d.py b/op/upfirdn2d.py
29
+ index 67e0375..6c5840e 100755
30
+ --- a/op/upfirdn2d.py
31
+ +++ b/op/upfirdn2d.py
32
+ @@ -1,5 +1,4 @@
33
+ from collections import abc
34
+ -import os
35
+
36
+ import torch
37
+ from torch.nn import functional as F
38
+ @@ -7,16 +6,6 @@ from torch.autograd import Function
39
+ from torch.utils.cpp_extension import load
40
+
41
+
42
+ -module_path = os.path.dirname(__file__)
43
+ -upfirdn2d_op = load(
44
+ - "upfirdn2d",
45
+ - sources=[
46
+ - os.path.join(module_path, "upfirdn2d.cpp"),
47
+ - os.path.join(module_path, "upfirdn2d_kernel.cu"),
48
+ - ],
49
+ -)
50
+ -
51
+ -
52
+ class UpFirDn2dBackward(Function):
53
+ @staticmethod
54
+ def forward(
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- numpy==1.22.3
2
- Pillow==9.0.1
3
- torch==1.11.0
4
- torchvision==0.12.0
 
1
+ numpy==1.23.5
2
+ Pillow==10.0.0
3
+ torch==2.0.1
4
+ torchvision==0.15.2
style.css ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }
4
+
5
+ #duplicate-button {
6
+ margin: auto;
7
+ }