hysts HF staff commited on
Commit
ed7463d
1 Parent(s): 881b5be
Files changed (5) hide show
  1. .pre-commit-config.yaml +35 -0
  2. .style.yapf +5 -0
  3. README.md +1 -29
  4. app.py +28 -66
  5. requirements.txt +3 -2
.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,35 +4,7 @@ emoji: 🏃
4
  colorFrom: red
5
  colorTo: green
6
  sdk: gradio
7
- sdk_version: 3.0.5
8
  app_file: app.py
9
  pinned: false
10
  ---
11
-
12
- # Configuration
13
-
14
- `title`: _string_
15
- Display title for the Space
16
-
17
- `emoji`: _string_
18
- Space emoji (emoji-only character allowed)
19
-
20
- `colorFrom`: _string_
21
- Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
22
-
23
- `colorTo`: _string_
24
- Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
25
-
26
- `sdk`: _string_
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_
34
- Path to your main application file (which contains either `gradio` or `streamlit` Python code, or `static` html code).
35
- Path is relative to the root of the repository.
36
-
37
- `pinned`: _boolean_
38
- Whether the Space stays on top of your list.
 
4
  colorFrom: red
5
  colorTo: green
6
  sdk: gradio
7
+ sdk_version: 3.19.1
8
  app_file: app.py
9
  pinned: false
10
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
@@ -21,30 +20,13 @@ from _util.twodee_v0 import I as ImageWrapper
21
 
22
  TITLE = 'ShuhongChen/bizarre-pose-estimator (tagger)'
23
  DESCRIPTION = 'This is an unofficial demo for https://github.com/ShuhongChen/bizarre-pose-estimator.'
24
- ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.bizarre-pose-estimator-tagger" alt="visitor badge"/></center>'
25
 
26
- TOKEN = os.environ['TOKEN']
27
  MODEL_REPO = 'hysts/bizarre-pose-estimator-models'
28
  MODEL_FILENAME = 'tagger.pth'
29
  LABEL_FILENAME = 'tags.txt'
30
 
31
 
32
- def parse_args() -> argparse.Namespace:
33
- parser = argparse.ArgumentParser()
34
- parser.add_argument('--device', type=str, default='cpu')
35
- parser.add_argument('--score-slider-step', type=float, default=0.05)
36
- parser.add_argument('--score-threshold', type=float, default=0.5)
37
- parser.add_argument('--theme', type=str, default='dark-grass')
38
- parser.add_argument('--live', action='store_true')
39
- parser.add_argument('--share', action='store_true')
40
- parser.add_argument('--port', type=int)
41
- parser.add_argument('--disable-queue',
42
- dest='enable_queue',
43
- action='store_false')
44
- parser.add_argument('--allow-flagging', type=str, default='never')
45
- return parser.parse_args()
46
-
47
-
48
  def load_sample_image_paths() -> list[pathlib.Path]:
49
  image_dir = pathlib.Path('images')
50
  if not image_dir.exists():
@@ -52,7 +34,7 @@ def load_sample_image_paths() -> list[pathlib.Path]:
52
  path = huggingface_hub.hf_hub_download(dataset_repo,
53
  'images.tar.gz',
54
  repo_type='dataset',
55
- use_auth_token=TOKEN)
56
  with tarfile.open(path) as f:
57
  f.extractall()
58
  return sorted(image_dir.glob('*'))
@@ -61,7 +43,7 @@ def load_sample_image_paths() -> list[pathlib.Path]:
61
  def load_model(device: torch.device) -> torch.nn.Module:
62
  path = huggingface_hub.hf_hub_download(MODEL_REPO,
63
  MODEL_FILENAME,
64
- use_auth_token=TOKEN)
65
  state_dict = torch.load(path)
66
  model = torchvision.models.resnet50(num_classes=1062)
67
  model.load_state_dict(state_dict)
@@ -73,7 +55,7 @@ def load_model(device: torch.device) -> torch.nn.Module:
73
  def load_labels() -> list[str]:
74
  label_path = huggingface_hub.hf_hub_download(MODEL_REPO,
75
  LABEL_FILENAME,
76
- use_auth_token=TOKEN)
77
  with open(label_path) as f:
78
  labels = [line.strip() for line in f.readlines()]
79
  return labels
@@ -99,47 +81,27 @@ def predict(image: PIL.Image.Image, score_threshold: float,
99
  return res
100
 
101
 
102
- def main():
103
- args = parse_args()
104
- device = torch.device(args.device)
105
-
106
- image_paths = load_sample_image_paths()
107
- examples = [[path.as_posix(), args.score_threshold]
108
- for path in image_paths]
109
-
110
- model = load_model(device)
111
- labels = load_labels()
112
-
113
- func = functools.partial(predict,
114
- device=device,
115
- model=model,
116
- labels=labels)
117
- func = functools.update_wrapper(func, predict)
118
-
119
- gr.Interface(
120
- func,
121
- [
122
- gr.inputs.Image(type='pil', label='Input'),
123
- gr.inputs.Slider(0,
124
- 1,
125
- step=args.score_slider_step,
126
- default=args.score_threshold,
127
- label='Score Threshold'),
128
- ],
129
- gr.outputs.Label(label='Output'),
130
- examples=examples,
131
- title=TITLE,
132
- description=DESCRIPTION,
133
- article=ARTICLE,
134
- theme=args.theme,
135
- allow_flagging=args.allow_flagging,
136
- live=args.live,
137
- ).launch(
138
- enable_queue=args.enable_queue,
139
- server_port=args.port,
140
- share=args.share,
141
- )
142
-
143
-
144
- if __name__ == '__main__':
145
- main()
 
2
 
3
  from __future__ import annotations
4
 
 
5
  import functools
6
  import os
7
  import pathlib
 
20
 
21
  TITLE = 'ShuhongChen/bizarre-pose-estimator (tagger)'
22
  DESCRIPTION = 'This is an unofficial demo for https://github.com/ShuhongChen/bizarre-pose-estimator.'
 
23
 
24
+ HF_TOKEN = os.getenv('HF_TOKEN')
25
  MODEL_REPO = 'hysts/bizarre-pose-estimator-models'
26
  MODEL_FILENAME = 'tagger.pth'
27
  LABEL_FILENAME = 'tags.txt'
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def load_sample_image_paths() -> list[pathlib.Path]:
31
  image_dir = pathlib.Path('images')
32
  if not image_dir.exists():
 
34
  path = huggingface_hub.hf_hub_download(dataset_repo,
35
  'images.tar.gz',
36
  repo_type='dataset',
37
+ use_auth_token=HF_TOKEN)
38
  with tarfile.open(path) as f:
39
  f.extractall()
40
  return sorted(image_dir.glob('*'))
 
43
  def load_model(device: torch.device) -> torch.nn.Module:
44
  path = huggingface_hub.hf_hub_download(MODEL_REPO,
45
  MODEL_FILENAME,
46
+ use_auth_token=HF_TOKEN)
47
  state_dict = torch.load(path)
48
  model = torchvision.models.resnet50(num_classes=1062)
49
  model.load_state_dict(state_dict)
 
55
  def load_labels() -> list[str]:
56
  label_path = huggingface_hub.hf_hub_download(MODEL_REPO,
57
  LABEL_FILENAME,
58
+ use_auth_token=HF_TOKEN)
59
  with open(label_path) as f:
60
  labels = [line.strip() for line in f.readlines()]
61
  return labels
 
81
  return res
82
 
83
 
84
+ image_paths = load_sample_image_paths()
85
+ examples = [[path.as_posix(), 0.5] for path in image_paths]
86
+
87
+ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
88
+ model = load_model(device)
89
+ labels = load_labels()
90
+
91
+ func = functools.partial(predict, device=device, model=model, labels=labels)
92
+
93
+ gr.Interface(
94
+ fn=func,
95
+ inputs=[
96
+ gr.Image(label='Input', type='pil'),
97
+ gr.Slider(label='Score Threshold',
98
+ minimum=0,
99
+ maximum=1,
100
+ step=0.05,
101
+ value=0.5),
102
+ ],
103
+ outputs=gr.Label(label='Output'),
104
+ examples=examples,
105
+ title=TITLE,
106
+ description=DESCRIPTION,
107
+ ).queue().launch(show_api=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
- torch>=1.10.1
2
- torchvision>=0.11.2
 
 
1
+ numpy==1.23.5
2
+ torch==1.13.1
3
+ torchvision==0.14.1