Spaces:
Running
on
Zero
Running
on
Zero
Update
Browse files- .pre-commit-config.yaml +59 -34
- .style.yapf +0 -5
- .vscode/settings.json +30 -0
- README.md +1 -1
- app.py +23 -34
- requirements.txt +2 -2
.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.6.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.10.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.4.2
|
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.8.5
|
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: green
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
4 |
colorFrom: green
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.33.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
app.py
CHANGED
@@ -11,32 +11,24 @@ import PIL.Image
|
|
11 |
import torch
|
12 |
import torch.nn as nn
|
13 |
|
14 |
-
sys.path.insert(0,
|
15 |
|
16 |
from data import read_img_path, tensor_to_img
|
17 |
from model import UnetGenerator
|
18 |
|
19 |
-
TITLE =
|
20 |
-
DESCRIPTION =
|
21 |
|
22 |
|
23 |
def load_model(device: torch.device) -> nn.Module:
|
24 |
-
norm_layer = functools.partial(nn.InstanceNorm2d,
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
1,
|
29 |
-
8,
|
30 |
-
64,
|
31 |
-
norm_layer=norm_layer,
|
32 |
-
use_dropout=False)
|
33 |
-
|
34 |
-
path = huggingface_hub.hf_hub_download('public-data/Anime2Sketch',
|
35 |
-
'netG.pth')
|
36 |
ckpt = torch.load(path)
|
37 |
for key in list(ckpt.keys()):
|
38 |
-
if
|
39 |
-
ckpt[key.replace(
|
40 |
del ckpt[key]
|
41 |
model.load_state_dict(ckpt)
|
42 |
model.to(device)
|
@@ -44,11 +36,12 @@ def load_model(device: torch.device) -> nn.Module:
|
|
44 |
return model
|
45 |
|
46 |
|
|
|
|
|
|
|
|
|
47 |
@torch.inference_mode()
|
48 |
-
def run(image_file: str,
|
49 |
-
model: nn.Module,
|
50 |
-
device: torch.device,
|
51 |
-
load_size: int = 512) -> PIL.Image.Image:
|
52 |
tensor, orig_size = read_img_path(image_file, load_size)
|
53 |
tensor = tensor.to(device)
|
54 |
out = model(tensor)
|
@@ -58,18 +51,14 @@ def run(image_file: str,
|
|
58 |
return res
|
59 |
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
examples = [['Anime2Sketch/test_samples/madoka.jpg']]
|
67 |
-
|
68 |
-
gr.Interface(
|
69 |
-
fn=fn,
|
70 |
-
inputs=gr.Image(label='Input', type='filepath'),
|
71 |
-
outputs=gr.Image(label='Output'),
|
72 |
-
examples=examples,
|
73 |
title=TITLE,
|
74 |
description=DESCRIPTION,
|
75 |
-
)
|
|
|
|
|
|
|
|
11 |
import torch
|
12 |
import torch.nn as nn
|
13 |
|
14 |
+
sys.path.insert(0, "Anime2Sketch")
|
15 |
|
16 |
from data import read_img_path, tensor_to_img
|
17 |
from model import UnetGenerator
|
18 |
|
19 |
+
TITLE = "Anime2Sketch"
|
20 |
+
DESCRIPTION = "https://github.com/Mukosame/Anime2Sketch"
|
21 |
|
22 |
|
23 |
def load_model(device: torch.device) -> nn.Module:
|
24 |
+
norm_layer = functools.partial(nn.InstanceNorm2d, affine=False, track_running_stats=False)
|
25 |
+
model = UnetGenerator(3, 1, 8, 64, norm_layer=norm_layer, use_dropout=False)
|
26 |
+
|
27 |
+
path = huggingface_hub.hf_hub_download("public-data/Anime2Sketch", "netG.pth")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
ckpt = torch.load(path)
|
29 |
for key in list(ckpt.keys()):
|
30 |
+
if "module." in key:
|
31 |
+
ckpt[key.replace("module.", "")] = ckpt[key]
|
32 |
del ckpt[key]
|
33 |
model.load_state_dict(ckpt)
|
34 |
model.to(device)
|
|
|
36 |
return model
|
37 |
|
38 |
|
39 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
40 |
+
model = load_model(device)
|
41 |
+
|
42 |
+
|
43 |
@torch.inference_mode()
|
44 |
+
def run(image_file: str, load_size: int = 512) -> PIL.Image.Image:
|
|
|
|
|
|
|
45 |
tensor, orig_size = read_img_path(image_file, load_size)
|
46 |
tensor = tensor.to(device)
|
47 |
out = model(tensor)
|
|
|
51 |
return res
|
52 |
|
53 |
|
54 |
+
demo = gr.Interface(
|
55 |
+
fn=run,
|
56 |
+
inputs=gr.Image(label="Input", type="filepath"),
|
57 |
+
outputs=gr.Image(label="Output"),
|
58 |
+
examples=["Anime2Sketch/test_samples/madoka.jpg"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
title=TITLE,
|
60 |
description=DESCRIPTION,
|
61 |
+
)
|
62 |
+
|
63 |
+
if __name__ == "__main__":
|
64 |
+
demo.queue().launch()
|
requirements.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
numpy==1.
|
2 |
-
Pillow==10.
|
3 |
torch==2.0.1
|
4 |
torchvision==0.15.2
|
|
|
1 |
+
numpy==1.26.4
|
2 |
+
Pillow==10.3.0
|
3 |
torch==2.0.1
|
4 |
torchvision==0.15.2
|