hysts HF staff commited on
Commit
4f1095a
1 Parent(s): 914222f
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 +47 -43
  6. requirements.txt +2 -2
  7. style.css +8 -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: green
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 3.36.1
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: green
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 4.19.2
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -15,29 +15,25 @@ import PIL.Image
15
  import torch
16
  import torchvision.transforms as T
17
 
18
- DESCRIPTION = '# [RF5/danbooru-pretrained](https://github.com/RF5/danbooru-pretrained)'
19
 
20
- MODEL_REPO = 'public-data/danbooru-pretrained'
21
 
22
 
23
  def load_sample_image_paths() -> list[pathlib.Path]:
24
- image_dir = pathlib.Path('images')
25
  if not image_dir.exists():
26
- dataset_repo = 'hysts/sample-images-TADNE'
27
- path = huggingface_hub.hf_hub_download(dataset_repo,
28
- 'images.tar.gz',
29
- repo_type='dataset')
30
  with tarfile.open(path) as f:
31
  f.extractall()
32
- return sorted(image_dir.glob('*'))
33
 
34
 
35
  def load_model(device: torch.device) -> torch.nn.Module:
36
- path = huggingface_hub.hf_hub_download(MODEL_REPO, 'resnet50-13306192.pth')
37
  state_dict = torch.load(path)
38
- model = torch.hub.load('RF5/danbooru-pretrained',
39
- 'resnet50',
40
- pretrained=False)
41
  model.load_state_dict(state_dict)
42
  model.to(device)
43
  model.eval()
@@ -45,16 +41,21 @@ def load_model(device: torch.device) -> torch.nn.Module:
45
 
46
 
47
  def load_labels() -> list[str]:
48
- path = huggingface_hub.hf_hub_download(MODEL_REPO, 'class_names_6000.json')
49
  with open(path) as f:
50
  labels = json.load(f)
51
  return labels
52
 
53
 
54
  @torch.inference_mode()
55
- def predict(image: PIL.Image.Image, score_threshold: float,
56
- transform: Callable, device: torch.device, model: torch.nn.Module,
57
- labels: list[str]) -> dict[str, float]:
 
 
 
 
 
58
  data = transform(image)
59
  data = data.to(device).unsqueeze(0)
60
  preds = model(data)[0]
@@ -72,41 +73,44 @@ def predict(image: PIL.Image.Image, score_threshold: float,
72
  image_paths = load_sample_image_paths()
73
  examples = [[path.as_posix(), 0.4] for path in image_paths]
74
 
75
- device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
76
  model = load_model(device)
77
  labels = load_labels()
78
 
79
- transform = T.Compose([
80
- T.Resize(360),
81
- T.ToTensor(),
82
- T.Normalize(mean=[0.7137, 0.6628, 0.6519], std=[0.2970, 0.3017, 0.2979]),
83
- ])
 
 
84
 
85
- fn = functools.partial(predict,
86
- transform=transform,
87
- device=device,
88
- model=model,
89
- labels=labels)
90
 
91
- with gr.Blocks(css='style.css') as demo:
92
  gr.Markdown(DESCRIPTION)
93
  with gr.Row():
94
  with gr.Column():
95
- image = gr.Image(label='Input', type='pil')
96
- threshold = gr.Slider(label='Score Threshold',
97
- minimum=0,
98
- maximum=1,
99
- step=0.05,
100
- value=0.4)
101
- run_button = gr.Button('Run')
102
  with gr.Column():
103
- result = gr.Label(label='Output')
104
 
105
  inputs = [image, threshold]
106
- gr.Examples(examples=examples,
107
- inputs=inputs,
108
- outputs=result,
109
- fn=fn,
110
- cache_examples=os.getenv('CACHE_EXAMPLES') == '1')
111
- run_button.click(fn=fn, inputs=inputs, outputs=result, api_name='predict')
112
- demo.queue(max_size=15).launch()
 
 
 
 
 
 
 
 
 
 
15
  import torch
16
  import torchvision.transforms as T
17
 
18
+ DESCRIPTION = "# [RF5/danbooru-pretrained](https://github.com/RF5/danbooru-pretrained)"
19
 
20
+ MODEL_REPO = "public-data/danbooru-pretrained"
21
 
22
 
23
  def load_sample_image_paths() -> list[pathlib.Path]:
24
+ image_dir = pathlib.Path("images")
25
  if not image_dir.exists():
26
+ dataset_repo = "hysts/sample-images-TADNE"
27
+ path = huggingface_hub.hf_hub_download(dataset_repo, "images.tar.gz", repo_type="dataset")
 
 
28
  with tarfile.open(path) as f:
29
  f.extractall()
30
+ return sorted(image_dir.glob("*"))
31
 
32
 
33
  def load_model(device: torch.device) -> torch.nn.Module:
34
+ path = huggingface_hub.hf_hub_download(MODEL_REPO, "resnet50-13306192.pth")
35
  state_dict = torch.load(path)
36
+ model = torch.hub.load("RF5/danbooru-pretrained", "resnet50", pretrained=False)
 
 
37
  model.load_state_dict(state_dict)
38
  model.to(device)
39
  model.eval()
 
41
 
42
 
43
  def load_labels() -> list[str]:
44
+ path = huggingface_hub.hf_hub_download(MODEL_REPO, "class_names_6000.json")
45
  with open(path) as f:
46
  labels = json.load(f)
47
  return labels
48
 
49
 
50
  @torch.inference_mode()
51
+ def predict(
52
+ image: PIL.Image.Image,
53
+ score_threshold: float,
54
+ transform: Callable,
55
+ device: torch.device,
56
+ model: torch.nn.Module,
57
+ labels: list[str],
58
+ ) -> dict[str, float]:
59
  data = transform(image)
60
  data = data.to(device).unsqueeze(0)
61
  preds = model(data)[0]
 
73
  image_paths = load_sample_image_paths()
74
  examples = [[path.as_posix(), 0.4] for path in image_paths]
75
 
76
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
77
  model = load_model(device)
78
  labels = load_labels()
79
 
80
+ transform = T.Compose(
81
+ [
82
+ T.Resize(360),
83
+ T.ToTensor(),
84
+ T.Normalize(mean=[0.7137, 0.6628, 0.6519], std=[0.2970, 0.3017, 0.2979]),
85
+ ]
86
+ )
87
 
88
+ fn = functools.partial(predict, transform=transform, device=device, model=model, labels=labels)
 
 
 
 
89
 
90
+ with gr.Blocks(css="style.css") as demo:
91
  gr.Markdown(DESCRIPTION)
92
  with gr.Row():
93
  with gr.Column():
94
+ image = gr.Image(label="Input", type="pil")
95
+ threshold = gr.Slider(label="Score Threshold", minimum=0, maximum=1, step=0.05, value=0.4)
96
+ run_button = gr.Button()
 
 
 
 
97
  with gr.Column():
98
+ result = gr.Label(label="Output")
99
 
100
  inputs = [image, threshold]
101
+ gr.Examples(
102
+ examples=examples,
103
+ inputs=inputs,
104
+ outputs=result,
105
+ fn=fn,
106
+ cache_examples=os.getenv("CACHE_EXAMPLES") == "1",
107
+ )
108
+ run_button.click(
109
+ fn=fn,
110
+ inputs=inputs,
111
+ outputs=result,
112
+ api_name="predict",
113
+ )
114
+
115
+ if __name__ == "__main__":
116
+ demo.queue(max_size=15).launch()
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- torch==1.13.1
2
- torchvision==0.14.1
 
1
+ torch==2.0.1
2
+ torchvision==0.15.2
style.css CHANGED
@@ -1,3 +1,11 @@
1
  h1 {
2
  text-align: center;
 
 
 
 
 
 
 
 
3
  }
 
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
  }