hysts HF staff commited on
Commit
db4dac0
1 Parent(s): 69f15b2
Files changed (7) hide show
  1. .pre-commit-config.yaml +59 -34
  2. .style.yapf +0 -5
  3. .vscode/settings.json +30 -0
  4. README.md +1 -1
  5. app.py +36 -58
  6. requirements.txt +4 -4
  7. style.css +11 -0
.pre-commit-config.yaml CHANGED
@@ -1,35 +1,60 @@
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']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.5.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: end-of-file-fixer
12
+ - id: mixed-line-ending
13
+ args: ["--fix=lf"]
14
+ - id: requirements-txt-fixer
15
+ - id: trailing-whitespace
16
+ - repo: https://github.com/myint/docformatter
17
+ rev: v1.7.5
18
+ hooks:
19
+ - id: docformatter
20
+ args: ["--in-place"]
21
+ - repo: https://github.com/pycqa/isort
22
+ rev: 5.13.2
23
+ hooks:
24
+ - id: isort
25
+ args: ["--profile", "black"]
26
+ - repo: https://github.com/pre-commit/mirrors-mypy
27
+ rev: v1.8.0
28
+ hooks:
29
+ - id: mypy
30
+ args: ["--ignore-missing-imports"]
31
+ additional_dependencies:
32
+ [
33
+ "types-python-slugify",
34
+ "types-requests",
35
+ "types-PyYAML",
36
+ "types-pytz",
37
+ ]
38
+ - repo: https://github.com/psf/black
39
+ rev: 24.2.0
40
+ hooks:
41
+ - id: black
42
+ language_version: python3.10
43
+ args: ["--line-length", "119"]
44
+ - repo: https://github.com/kynan/nbstripout
45
+ rev: 0.7.1
46
+ hooks:
47
+ - id: nbstripout
48
+ args:
49
+ [
50
+ "--extra-keys",
51
+ "metadata.interpreter metadata.kernelspec cell.metadata.pycharm",
52
+ ]
53
+ - repo: https://github.com/nbQA-dev/nbQA
54
+ rev: 1.7.1
55
+ hooks:
56
+ - id: nbqa-black
57
+ - id: nbqa-pyupgrade
58
+ args: ["--py37-plus"]
59
+ - id: nbqa-isort
60
+ args: ["--float-to-top"]
.style.yapf DELETED
@@ -1,5 +0,0 @@
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
 
 
 
 
 
 
.vscode/settings.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "editor.formatOnSave": true,
3
+ "files.insertFinalNewline": false,
4
+ "[python]": {
5
+ "editor.defaultFormatter": "ms-python.black-formatter",
6
+ "editor.formatOnType": true,
7
+ "editor.codeActionsOnSave": {
8
+ "source.organizeImports": "explicit"
9
+ }
10
+ },
11
+ "[jupyter]": {
12
+ "files.insertFinalNewline": false
13
+ },
14
+ "black-formatter.args": [
15
+ "--line-length=119"
16
+ ],
17
+ "isort.args": ["--profile", "black"],
18
+ "flake8.args": [
19
+ "--max-line-length=119"
20
+ ],
21
+ "ruff.lint.args": [
22
+ "--line-length=119"
23
+ ],
24
+ "notebook.output.scrolling": true,
25
+ "notebook.formatOnCellExecution": true,
26
+ "notebook.formatOnSave.enabled": true,
27
+ "notebook.codeActionsOnSave": {
28
+ "source.organizeImports": "explicit"
29
+ }
30
+ }
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 📚
4
  colorFrom: red
5
  colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 3.36.1
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: red
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 4.19.2
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -2,8 +2,6 @@
2
 
3
  from __future__ import annotations
4
 
5
- import functools
6
- import os
7
  import pickle
8
  import sys
9
 
@@ -13,14 +11,12 @@ import torch
13
  import torch.nn as nn
14
  from huggingface_hub import hf_hub_download
15
 
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:
@@ -36,15 +32,29 @@ def make_transform(translate: tuple[float, float], angle: float) -> np.ndarray:
36
  return mat
37
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  def generate_z(seed: int, device: torch.device) -> torch.Tensor:
40
- return torch.from_numpy(np.random.RandomState(seed).randn(1,
41
- 512)).to(device)
42
 
43
 
44
  @torch.inference_mode()
45
- def generate_image(seed: int, truncation_psi: float, tx: float, ty: float,
46
- angle: float, model: nn.Module,
47
- device: torch.device) -> np.ndarray:
48
  seed = int(np.clip(seed, 0, np.iinfo(np.uint32).max))
49
  z = generate_z(seed, device)
50
  c = torch.zeros(0).to(device)
@@ -58,51 +68,19 @@ def generate_image(seed: int, truncation_psi: float, tx: float, ty: float,
58
  return out[0].cpu().numpy()
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()
68
- model.to(device)
69
- with torch.inference_mode():
70
- z = torch.zeros((1, 512)).to(device)
71
- c = torch.zeros(0).to(device)
72
- model(z, c)
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=np.iinfo(np.uint32).max,
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)
 
 
 
 
 
2
 
3
  from __future__ import annotations
4
 
 
 
5
  import pickle
6
  import sys
7
 
 
11
  import torch.nn as nn
12
  from huggingface_hub import hf_hub_download
13
 
14
+ sys.path.insert(0, "stylegan3")
15
 
16
+ TITLE = "StyleGAN3 Anime Face Generation"
17
 
18
+ MODEL_REPO = "hysts/stylegan3-anime-face-exp001-model"
19
+ MODEL_FILE_NAME = "006600.pkl"
 
 
20
 
21
 
22
  def make_transform(translate: tuple[float, float], angle: float) -> np.ndarray:
 
32
  return mat
33
 
34
 
35
+ def load_model(device: torch.device) -> nn.Module:
36
+ path = hf_hub_download(MODEL_REPO, MODEL_FILE_NAME)
37
+ with open(path, "rb") as f:
38
+ model = pickle.load(f)
39
+ model.eval()
40
+ model.to(device)
41
+ with torch.inference_mode():
42
+ z = torch.zeros((1, 512)).to(device)
43
+ c = torch.zeros(0).to(device)
44
+ model(z, c)
45
+ return model
46
+
47
+
48
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
49
+ model = load_model(device)
50
+
51
+
52
  def generate_z(seed: int, device: torch.device) -> torch.Tensor:
53
+ return torch.from_numpy(np.random.RandomState(seed).randn(1, 512)).to(device)
 
54
 
55
 
56
  @torch.inference_mode()
57
+ def generate_image(seed: int, truncation_psi: float, tx: float, ty: float, angle: float) -> np.ndarray:
 
 
58
  seed = int(np.clip(seed, 0, np.iinfo(np.uint32).max))
59
  z = generate_z(seed, device)
60
  c = torch.zeros(0).to(device)
 
68
  return out[0].cpu().numpy()
69
 
70
 
71
+ demo = gr.Interface(
72
+ fn=generate_image,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  inputs=[
74
+ gr.Slider(label="Seed", minimum=0, maximum=np.iinfo(np.uint32).max, step=1, value=3407851645),
75
+ gr.Slider(label="Truncation psi", minimum=0, maximum=2, step=0.05, value=0.7),
76
+ gr.Slider(label="Translate X", minimum=-1, maximum=1, step=0.05, value=0),
77
+ gr.Slider(label="Translate Y", minimum=-1, maximum=1, step=0.05, value=0),
78
+ gr.Slider(label="Angle", minimum=-180, maximum=180, step=5, value=0),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  ],
80
+ outputs=gr.Image(label="Output"),
81
  title=TITLE,
82
+ css="style.css",
83
+ )
84
+
85
+ if __name__ == "__main__":
86
+ demo.queue(max_size=20, api_open=False).launch(show_api=False)
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- numpy>=1.21.4
2
- Pillow>=8.3.1
3
- scipy>=1.7.2
4
- torch>=1.10.0
 
1
+ numpy==1.26.4
2
+ Pillow==10.2.0
3
+ scipy==1.12.0
4
+ torch==2.0.1
style.css ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ display: block;
4
+ }
5
+
6
+ #duplicate-button {
7
+ margin: auto;
8
+ color: #fff;
9
+ background: #1565c0;
10
+ border-radius: 100vh;
11
+ }