hysts HF staff commited on
Commit
7852ca7
1 Parent(s): 720032c
Files changed (4) hide show
  1. .pre-commit-config.yaml +35 -0
  2. .style.yapf +5 -0
  3. README.md +2 -2
  4. app.py +31 -69
.pre-commit-config.yaml ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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']
.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,7 +4,7 @@ emoji: 📊
4
  colorFrom: gray
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 3.0.5
8
  app_file: app.py
9
  pinned: false
10
  ---
@@ -27,7 +27,7 @@ Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gr
27
  Can be either `gradio`, `streamlit`, or `static`
28
 
29
  `sdk_version` : _string_
30
- Only applicable for `streamlit` SDK.
31
  See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
32
 
33
  `app_file`: _string_
 
4
  colorFrom: gray
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 3.19.1
8
  app_file: app.py
9
  pinned: false
10
  ---
 
27
  Can be either `gradio`, `streamlit`, or `static`
28
 
29
  `sdk_version` : _string_
30
+ Only applicable for `streamlit` SDK.
31
  See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
32
 
33
  `app_file`: _string_
app.py CHANGED
@@ -2,7 +2,6 @@
2
 
3
  from __future__ import annotations
4
 
5
- import argparse
6
  import functools
7
  import os
8
  import pathlib
@@ -24,32 +23,13 @@ from utils.general import non_max_suppression, scale_coords
24
 
25
  TITLE = 'zymk9/yolov5_anime'
26
  DESCRIPTION = 'This is an unofficial demo for https://github.com/zymk9/yolov5_anime.'
27
- ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.yolov5_anime" alt="visitor badge"/></center>'
28
 
29
- TOKEN = os.environ['TOKEN']
30
  MODEL_REPO = 'hysts/yolov5_anime'
31
  MODEL_FILENAME = 'yolov5x_anime.pth'
32
  CONFIG_FILENAME = 'yolov5x.yaml'
33
 
34
 
35
- def parse_args() -> argparse.Namespace:
36
- parser = argparse.ArgumentParser()
37
- parser.add_argument('--device', type=str, default='cpu')
38
- parser.add_argument('--score-slider-step', type=float, default=0.05)
39
- parser.add_argument('--score-threshold', type=float, default=0.4)
40
- parser.add_argument('--iou-slider-step', type=float, default=0.05)
41
- parser.add_argument('--iou-threshold', type=float, default=0.5)
42
- parser.add_argument('--theme', type=str)
43
- parser.add_argument('--live', action='store_true')
44
- parser.add_argument('--share', action='store_true')
45
- parser.add_argument('--port', type=int)
46
- parser.add_argument('--disable-queue',
47
- dest='enable_queue',
48
- action='store_false')
49
- parser.add_argument('--allow-flagging', type=str, default='never')
50
- return parser.parse_args()
51
-
52
-
53
  def load_sample_image_paths() -> list[pathlib.Path]:
54
  image_dir = pathlib.Path('images')
55
  if not image_dir.exists():
@@ -57,7 +37,7 @@ def load_sample_image_paths() -> list[pathlib.Path]:
57
  path = huggingface_hub.hf_hub_download(dataset_repo,
58
  'images.tar.gz',
59
  repo_type='dataset',
60
- use_auth_token=TOKEN)
61
  with tarfile.open(path) as f:
62
  f.extractall()
63
  return sorted(image_dir.glob('*'))
@@ -67,10 +47,10 @@ def load_model(device: torch.device) -> torch.nn.Module:
67
  torch.set_grad_enabled(False)
68
  model_path = huggingface_hub.hf_hub_download(MODEL_REPO,
69
  MODEL_FILENAME,
70
- use_auth_token=TOKEN)
71
  config_path = huggingface_hub.hf_hub_download(MODEL_REPO,
72
  CONFIG_FILENAME,
73
- use_auth_token=TOKEN)
74
  state_dict = torch.load(model_path)
75
  model = Model(cfg=config_path)
76
  model.load_state_dict(state_dict)
@@ -113,48 +93,30 @@ def predict(image: PIL.Image.Image, score_threshold: float,
113
  return res
114
 
115
 
116
- def main():
117
- args = parse_args()
118
- device = torch.device(args.device)
119
-
120
- image_paths = load_sample_image_paths()
121
- examples = [[path.as_posix(), args.score_threshold, args.iou_threshold]
122
- for path in image_paths]
123
-
124
- model = load_model(device)
125
-
126
- func = functools.partial(predict, device=device, model=model)
127
- func = functools.update_wrapper(func, predict)
128
-
129
- gr.Interface(
130
- func,
131
- [
132
- gr.inputs.Image(type='pil', label='Input'),
133
- gr.inputs.Slider(0,
134
- 1,
135
- step=args.score_slider_step,
136
- default=args.score_threshold,
137
- label='Score Threshold'),
138
- gr.inputs.Slider(0,
139
- 1,
140
- step=args.iou_slider_step,
141
- default=args.iou_threshold,
142
- label='IoU Threshold'),
143
- ],
144
- gr.outputs.Image(label='Output'),
145
- examples=examples,
146
- title=TITLE,
147
- description=DESCRIPTION,
148
- article=ARTICLE,
149
- theme=args.theme,
150
- allow_flagging=args.allow_flagging,
151
- live=args.live,
152
- ).launch(
153
- enable_queue=args.enable_queue,
154
- server_port=args.port,
155
- share=args.share,
156
- )
157
-
158
-
159
- if __name__ == '__main__':
160
- main()
 
2
 
3
  from __future__ import annotations
4
 
 
5
  import functools
6
  import os
7
  import pathlib
 
23
 
24
  TITLE = 'zymk9/yolov5_anime'
25
  DESCRIPTION = 'This is an unofficial demo for https://github.com/zymk9/yolov5_anime.'
 
26
 
27
+ HF_TOKEN = os.getenv('HF_TOKEN')
28
  MODEL_REPO = 'hysts/yolov5_anime'
29
  MODEL_FILENAME = 'yolov5x_anime.pth'
30
  CONFIG_FILENAME = 'yolov5x.yaml'
31
 
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def load_sample_image_paths() -> list[pathlib.Path]:
34
  image_dir = pathlib.Path('images')
35
  if not image_dir.exists():
 
37
  path = huggingface_hub.hf_hub_download(dataset_repo,
38
  'images.tar.gz',
39
  repo_type='dataset',
40
+ use_auth_token=HF_TOKEN)
41
  with tarfile.open(path) as f:
42
  f.extractall()
43
  return sorted(image_dir.glob('*'))
 
47
  torch.set_grad_enabled(False)
48
  model_path = huggingface_hub.hf_hub_download(MODEL_REPO,
49
  MODEL_FILENAME,
50
+ use_auth_token=HF_TOKEN)
51
  config_path = huggingface_hub.hf_hub_download(MODEL_REPO,
52
  CONFIG_FILENAME,
53
+ use_auth_token=HF_TOKEN)
54
  state_dict = torch.load(model_path)
55
  model = Model(cfg=config_path)
56
  model.load_state_dict(state_dict)
 
93
  return res
94
 
95
 
96
+ image_paths = load_sample_image_paths()
97
+ examples = [[path.as_posix(), 0.4, 0.5] for path in image_paths]
98
+
99
+ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
100
+ model = load_model(device)
101
+ func = functools.partial(predict, device=device, model=model)
102
+
103
+ gr.Interface(
104
+ fn=func,
105
+ inputs=[
106
+ gr.Image(label='Input', type='pil'),
107
+ gr.Slider(label='Score Threshold',
108
+ minimum=0,
109
+ maximum=1,
110
+ step=0.05,
111
+ value=0.4),
112
+ gr.Slider(label='IoU Threshold',
113
+ minimum=0,
114
+ maximum=1,
115
+ step=0.05,
116
+ value=0.5),
117
+ ],
118
+ outputs=gr.Image(label='Output'),
119
+ examples=examples,
120
+ title=TITLE,
121
+ description=DESCRIPTION,
122
+ ).queue().launch(show_api=False)