Update
Browse files- .pre-commit-config.yaml +59 -34
- .style.yapf +0 -5
- .vscode/settings.json +30 -0
- README.md +1 -1
- app.py +36 -58
- requirements.txt +4 -4
- 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 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
- repo: https://github.com/pre-commit/mirrors-mypy
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
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,
|
17 |
|
18 |
-
TITLE =
|
19 |
|
20 |
-
MODEL_REPO =
|
21 |
-
MODEL_FILE_NAME =
|
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 |
-
|
62 |
-
|
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=
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
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=
|
107 |
title=TITLE,
|
108 |
-
|
|
|
|
|
|
|
|
|
|
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
|
2 |
-
Pillow
|
3 |
-
scipy
|
4 |
-
torch
|
|
|
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 |
+
}
|