hysts HF staff commited on
Commit
0635e35
·
1 Parent(s): 7737782
.pre-commit-config.yaml CHANGED
@@ -1,6 +1,6 @@
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
@@ -13,48 +13,21 @@ repos:
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"]
 
1
  repos:
2
  - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v5.0.0
4
  hooks:
5
  - id: check-executables-have-shebangs
6
  - id: check-json
 
13
  args: ["--fix=lf"]
14
  - id: requirements-txt-fixer
15
  - id: trailing-whitespace
16
+ - repo: https://github.com/astral-sh/ruff-pre-commit
17
+ rev: v0.9.9
18
  hooks:
19
+ - id: ruff
20
+ args: ["--fix"]
21
+ - id: ruff-format
 
 
 
 
22
  - repo: https://github.com/pre-commit/mirrors-mypy
23
+ rev: v1.15.0
24
  hooks:
25
  - id: mypy
26
  args: ["--ignore-missing-imports"]
27
  additional_dependencies:
28
  [
29
  "types-python-slugify",
 
 
30
  "types-pytz",
31
+ "types-PyYAML",
32
+ "types-requests",
33
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.10
.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/extensions.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "recommendations": [
3
+ "ms-python.python",
4
+ "charliermarsh.ruff",
5
+ "streetsidesoftware.code-spell-checker",
6
+ "tamasfe.even-better-toml"
7
+ ]
8
+ }
.vscode/settings.json CHANGED
@@ -2,29 +2,16 @@
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
  }
 
2
  "editor.formatOnSave": true,
3
  "files.insertFinalNewline": false,
4
  "[python]": {
5
+ "editor.defaultFormatter": "charliermarsh.ruff",
6
  "editor.formatOnType": true,
7
  "editor.codeActionsOnSave": {
8
+ "source.fixAll.ruff": "explicit",
9
  "source.organizeImports": "explicit"
10
  }
11
  },
12
  "[jupyter]": {
13
  "files.insertFinalNewline": false
14
  },
 
 
 
 
 
 
 
 
 
 
15
  "notebook.output.scrolling": true,
16
+ "notebook.formatOnSave.enabled": true
 
 
 
 
17
  }
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🌍
4
  colorFrom: green
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 4.36.1
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: green
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 5.20.0
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -1,8 +1,5 @@
1
  #!/usr/bin/env python
2
 
3
- from __future__ import annotations
4
-
5
- import functools
6
  import os
7
  import pathlib
8
  import shlex
@@ -10,8 +7,9 @@ import subprocess
10
  import tarfile
11
 
12
  if os.getenv("SYSTEM") == "spaces":
13
- subprocess.run(shlex.split("pip install git+https://github.com/facebookresearch/[email protected]"))
14
- subprocess.run(shlex.split("pip install git+https://github.com/aim-uofa/AdelaiDet@7bf9d87"))
 
15
 
16
  import gradio as gr
17
  import huggingface_hub
@@ -33,7 +31,7 @@ def load_sample_image_paths() -> list[pathlib.Path]:
33
  dataset_repo = "hysts/sample-images-TADNE"
34
  path = huggingface_hub.hf_hub_download(dataset_repo, "images.tar.gz", repo_type="dataset")
35
  with tarfile.open(path) as f:
36
- f.extractall()
37
  return sorted(image_dir.glob("*"))
38
 
39
 
@@ -48,8 +46,12 @@ def load_model(device: torch.device) -> DefaultPredictor:
48
  return DefaultPredictor(cfg)
49
 
50
 
 
 
 
 
51
  def predict(
52
- image_path: str, class_score_threshold: float, mask_score_threshold: float, model: DefaultPredictor
53
  ) -> tuple[np.ndarray, np.ndarray]:
54
  model.score_threshold = class_score_threshold
55
  model.mask_threshold = mask_score_threshold
@@ -69,21 +71,17 @@ def predict(
69
 
70
 
71
  image_paths = load_sample_image_paths()
72
- examples = [[path.as_posix(), 0.1, 0.5] for path in image_paths]
73
-
74
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
75
- model = load_model(device)
76
 
77
- fn = functools.partial(predict, model=model)
78
 
79
- with gr.Blocks(css="style.css") as demo:
80
  gr.Markdown(DESCRIPTION)
81
  with gr.Row():
82
  with gr.Column():
83
  image = gr.Image(label="Input", type="filepath")
84
  class_score_threshold = gr.Slider(label="Score Threshold", minimum=0, maximum=1, step=0.05, value=0.1)
85
  mask_score_threshold = gr.Slider(label="Mask Score Threshold", minimum=0, maximum=1, step=0.05, value=0.5)
86
- run_button = gr.Button("Run")
87
  with gr.Column():
88
  result_instances = gr.Image(label="Instances")
89
  result_masked = gr.Image(label="Masked")
@@ -94,14 +92,13 @@ with gr.Blocks(css="style.css") as demo:
94
  examples=examples,
95
  inputs=inputs,
96
  outputs=outputs,
97
- fn=fn,
98
  cache_examples=os.getenv("CACHE_EXAMPLES") == "1",
99
  )
100
  run_button.click(
101
- fn=fn,
102
  inputs=inputs,
103
  outputs=outputs,
104
- api_name="predict",
105
  )
106
 
107
  if __name__ == "__main__":
 
1
  #!/usr/bin/env python
2
 
 
 
 
3
  import os
4
  import pathlib
5
  import shlex
 
7
  import tarfile
8
 
9
  if os.getenv("SYSTEM") == "spaces":
10
+ subprocess.run(shlex.split("pip install git+https://github.com/facebookresearch/[email protected]"), check=True) # noqa: S603
11
+ subprocess.run(shlex.split("pip install git+https://github.com/aim-uofa/AdelaiDet@7bf9d87"), check=True) # noqa: S603
12
+ subprocess.run(shlex.split("pip install Pillow==9.5.0"), check=True) # noqa: S603
13
 
14
  import gradio as gr
15
  import huggingface_hub
 
31
  dataset_repo = "hysts/sample-images-TADNE"
32
  path = huggingface_hub.hf_hub_download(dataset_repo, "images.tar.gz", repo_type="dataset")
33
  with tarfile.open(path) as f:
34
+ f.extractall() # noqa: S202
35
  return sorted(image_dir.glob("*"))
36
 
37
 
 
46
  return DefaultPredictor(cfg)
47
 
48
 
49
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
50
+ model = load_model(device)
51
+
52
+
53
  def predict(
54
+ image_path: str, class_score_threshold: float, mask_score_threshold: float
55
  ) -> tuple[np.ndarray, np.ndarray]:
56
  model.score_threshold = class_score_threshold
57
  model.mask_threshold = mask_score_threshold
 
71
 
72
 
73
  image_paths = load_sample_image_paths()
74
+ examples = [[path, 0.1, 0.5] for path in image_paths]
 
 
 
75
 
 
76
 
77
+ with gr.Blocks(css_paths="style.css") as demo:
78
  gr.Markdown(DESCRIPTION)
79
  with gr.Row():
80
  with gr.Column():
81
  image = gr.Image(label="Input", type="filepath")
82
  class_score_threshold = gr.Slider(label="Score Threshold", minimum=0, maximum=1, step=0.05, value=0.1)
83
  mask_score_threshold = gr.Slider(label="Mask Score Threshold", minimum=0, maximum=1, step=0.05, value=0.5)
84
+ run_button = gr.Button()
85
  with gr.Column():
86
  result_instances = gr.Image(label="Instances")
87
  result_masked = gr.Image(label="Masked")
 
92
  examples=examples,
93
  inputs=inputs,
94
  outputs=outputs,
95
+ fn=predict,
96
  cache_examples=os.getenv("CACHE_EXAMPLES") == "1",
97
  )
98
  run_button.click(
99
+ fn=predict,
100
  inputs=inputs,
101
  outputs=outputs,
 
102
  )
103
 
104
  if __name__ == "__main__":
pyproject.toml ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "yet-another-anime-segmenter"
3
+ version = "0.1.0"
4
+ description = ""
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+ dependencies = [
8
+ "gradio>=5.20.0",
9
+ "hf-transfer>=0.1.9",
10
+ "numpy<2",
11
+ "opencv-python-headless>=4.11.0.86",
12
+ "pillow==9.5.0",
13
+ "torch==1.13.1",
14
+ "torchvision>=0.14.1",
15
+ ]
16
+
17
+ [tool.ruff]
18
+ line-length = 119
19
+
20
+ [tool.ruff.lint]
21
+ select = ["ALL"]
22
+ ignore = [
23
+ "COM812", # missing-trailing-comma
24
+ "D203", # one-blank-line-before-class
25
+ "D213", # multi-line-summary-second-line
26
+ "E501", # line-too-long
27
+ "SIM117", # multiple-with-statements
28
+ ]
29
+ extend-ignore = [
30
+ "D100", # undocumented-public-module
31
+ "D101", # undocumented-public-class
32
+ "D102", # undocumented-public-method
33
+ "D103", # undocumented-public-function
34
+ "D104", # undocumented-public-package
35
+ "D105", # undocumented-magic-method
36
+ "D107", # undocumented-public-init
37
+ "EM101", # raw-string-in-exception
38
+ "FBT001", # boolean-type-hint-positional-argument
39
+ "FBT002", # boolean-default-value-positional-argument
40
+ "PD901", # pandas-df-variable-name
41
+ "PGH003", # blanket-type-ignore
42
+ "PLR0913", # too-many-arguments
43
+ "PLR0915", # too-many-statements
44
+ "TRY003", # raise-vanilla-args
45
+ ]
46
+ unfixable = [
47
+ "F401", # unused-import
48
+ ]
49
+
50
+ [tool.ruff.lint.pydocstyle]
51
+ convention = "google"
52
+
53
+ [tool.ruff.lint.per-file-ignores]
54
+ "*.ipynb" = ["T201", "T203"]
55
+
56
+ [tool.ruff.format]
57
+ docstring-code-format = true
58
+
59
+ [tool.uv.sources]
60
+ adelaidet = { git = "https://github.com/aim-uofa/AdelaiDet", rev = "7bf9d87" }
61
+ detectron2 = { git = "https://github.com/facebookresearch/detectron2", rev = "v0.6" }
62
+
63
+ [dependency-groups]
64
+ dev = [
65
+ "adelaidet",
66
+ "detectron2",
67
+ ]
requirements.txt CHANGED
@@ -1,5 +1,189 @@
1
- gradio==4.36.1
2
- opencv-python-headless==4.8.0.74
3
- Pillow==9.5.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  torch==1.13.1
 
 
 
5
  torchvision==0.14.1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file was autogenerated by uv via the following command:
2
+ # uv pip compile pyproject.toml -o requirements.txt
3
+ aiofiles==23.2.1
4
+ # via gradio
5
+ annotated-types==0.7.0
6
+ # via pydantic
7
+ anyio==4.8.0
8
+ # via
9
+ # gradio
10
+ # httpx
11
+ # starlette
12
+ certifi==2025.1.31
13
+ # via
14
+ # httpcore
15
+ # httpx
16
+ # requests
17
+ charset-normalizer==3.4.1
18
+ # via requests
19
+ click==8.1.8
20
+ # via
21
+ # typer
22
+ # uvicorn
23
+ exceptiongroup==1.2.2
24
+ # via anyio
25
+ fastapi==0.115.11
26
+ # via gradio
27
+ ffmpy==0.5.0
28
+ # via gradio
29
+ filelock==3.17.0
30
+ # via huggingface-hub
31
+ fsspec==2025.2.0
32
+ # via
33
+ # gradio-client
34
+ # huggingface-hub
35
+ gradio==5.20.0
36
+ # via yet-another-anime-segmenter (pyproject.toml)
37
+ gradio-client==1.7.2
38
+ # via gradio
39
+ groovy==0.1.2
40
+ # via gradio
41
+ h11==0.14.0
42
+ # via
43
+ # httpcore
44
+ # uvicorn
45
+ hf-transfer==0.1.9
46
+ # via yet-another-anime-segmenter (pyproject.toml)
47
+ httpcore==1.0.7
48
+ # via httpx
49
+ httpx==0.28.1
50
+ # via
51
+ # gradio
52
+ # gradio-client
53
+ # safehttpx
54
+ huggingface-hub==0.29.1
55
+ # via
56
+ # gradio
57
+ # gradio-client
58
+ idna==3.10
59
+ # via
60
+ # anyio
61
+ # httpx
62
+ # requests
63
+ jinja2==3.1.5
64
+ # via gradio
65
+ markdown-it-py==3.0.0
66
+ # via rich
67
+ markupsafe==2.1.5
68
+ # via
69
+ # gradio
70
+ # jinja2
71
+ mdurl==0.1.2
72
+ # via markdown-it-py
73
+ numpy==1.26.4
74
+ # via
75
+ # yet-another-anime-segmenter (pyproject.toml)
76
+ # gradio
77
+ # opencv-python-headless
78
+ # pandas
79
+ # torchvision
80
+ nvidia-cublas-cu11==11.10.3.66
81
+ # via
82
+ # nvidia-cudnn-cu11
83
+ # torch
84
+ nvidia-cuda-nvrtc-cu11==11.7.99
85
+ # via torch
86
+ nvidia-cuda-runtime-cu11==11.7.99
87
+ # via torch
88
+ nvidia-cudnn-cu11==8.5.0.96
89
+ # via torch
90
+ opencv-python-headless==4.11.0.86
91
+ # via yet-another-anime-segmenter (pyproject.toml)
92
+ orjson==3.10.15
93
+ # via gradio
94
+ packaging==24.2
95
+ # via
96
+ # gradio
97
+ # gradio-client
98
+ # huggingface-hub
99
+ pandas==2.2.3
100
+ # via gradio
101
+ pillow==9.5.0
102
+ # via
103
+ # yet-another-anime-segmenter (pyproject.toml)
104
+ # gradio
105
+ # torchvision
106
+ pydantic==2.10.6
107
+ # via
108
+ # fastapi
109
+ # gradio
110
+ pydantic-core==2.27.2
111
+ # via pydantic
112
+ pydub==0.25.1
113
+ # via gradio
114
+ pygments==2.19.1
115
+ # via rich
116
+ python-dateutil==2.9.0.post0
117
+ # via pandas
118
+ python-multipart==0.0.20
119
+ # via gradio
120
+ pytz==2025.1
121
+ # via pandas
122
+ pyyaml==6.0.2
123
+ # via
124
+ # gradio
125
+ # huggingface-hub
126
+ requests==2.32.3
127
+ # via
128
+ # huggingface-hub
129
+ # torchvision
130
+ rich==13.9.4
131
+ # via typer
132
+ ruff==0.9.9
133
+ # via gradio
134
+ safehttpx==0.1.6
135
+ # via gradio
136
+ semantic-version==2.10.0
137
+ # via gradio
138
+ setuptools==75.8.2
139
+ # via
140
+ # nvidia-cublas-cu11
141
+ # nvidia-cuda-runtime-cu11
142
+ shellingham==1.5.4
143
+ # via typer
144
+ six==1.17.0
145
+ # via python-dateutil
146
+ sniffio==1.3.1
147
+ # via anyio
148
+ starlette==0.46.0
149
+ # via
150
+ # fastapi
151
+ # gradio
152
+ tomlkit==0.13.2
153
+ # via gradio
154
  torch==1.13.1
155
+ # via
156
+ # yet-another-anime-segmenter (pyproject.toml)
157
+ # torchvision
158
  torchvision==0.14.1
159
+ # via yet-another-anime-segmenter (pyproject.toml)
160
+ tqdm==4.67.1
161
+ # via huggingface-hub
162
+ typer==0.15.2
163
+ # via gradio
164
+ typing-extensions==4.12.2
165
+ # via
166
+ # anyio
167
+ # fastapi
168
+ # gradio
169
+ # gradio-client
170
+ # huggingface-hub
171
+ # pydantic
172
+ # pydantic-core
173
+ # rich
174
+ # torch
175
+ # torchvision
176
+ # typer
177
+ # uvicorn
178
+ tzdata==2025.1
179
+ # via pandas
180
+ urllib3==2.3.0
181
+ # via requests
182
+ uvicorn==0.34.0
183
+ # via gradio
184
+ websockets==15.0
185
+ # via gradio-client
186
+ wheel==0.45.1
187
+ # via
188
+ # nvidia-cublas-cu11
189
+ # nvidia-cuda-runtime-cu11
uv.lock ADDED
The diff for this file is too large to render. See raw diff