Eimaen hysts HF staff commited on
Commit
5bfc531
0 Parent(s):

Duplicate from hysts/DeepDanbooru

Browse files

Co-authored-by: hysts <hysts@users.noreply.huggingface.co>

Files changed (8) hide show
  1. .gitattributes +27 -0
  2. .gitignore +1 -0
  3. .pre-commit-config.yaml +36 -0
  4. .style.yapf +5 -0
  5. README.md +11 -0
  6. app.py +97 -0
  7. requirements.txt +3 -0
  8. style.css +3 -0
.gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bin.* filter=lfs diff=lfs merge=lfs -text
5
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.model filter=lfs diff=lfs merge=lfs -text
12
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
13
+ *.onnx filter=lfs diff=lfs merge=lfs -text
14
+ *.ot filter=lfs diff=lfs merge=lfs -text
15
+ *.parquet filter=lfs diff=lfs merge=lfs -text
16
+ *.pb filter=lfs diff=lfs merge=lfs -text
17
+ *.pt filter=lfs diff=lfs merge=lfs -text
18
+ *.pth filter=lfs diff=lfs merge=lfs -text
19
+ *.rar filter=lfs diff=lfs merge=lfs -text
20
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
21
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
22
+ *.tflite filter=lfs diff=lfs merge=lfs -text
23
+ *.tgz filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ images
.pre-commit-config.yaml ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ additional_dependencies: ['types-python-slugify']
32
+ - repo: https://github.com/google/yapf
33
+ rev: v0.32.0
34
+ hooks:
35
+ - id: yapf
36
+ 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 ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: DeepDanbooru
3
+ emoji: 🏃
4
+ colorFrom: gray
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 3.34.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: hysts/DeepDanbooru
11
+ ---
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import pathlib
7
+ import tarfile
8
+
9
+ import deepdanbooru as dd
10
+ import gradio as gr
11
+ import huggingface_hub
12
+ import numpy as np
13
+ import PIL.Image
14
+ import tensorflow as tf
15
+
16
+ DESCRIPTION = '# [KichangKim/DeepDanbooru](https://github.com/KichangKim/DeepDanbooru)'
17
+
18
+
19
+ def load_sample_image_paths() -> list[pathlib.Path]:
20
+ image_dir = pathlib.Path('images')
21
+ if not image_dir.exists():
22
+ path = huggingface_hub.hf_hub_download(
23
+ 'public-data/sample-images-TADNE',
24
+ 'images.tar.gz',
25
+ repo_type='dataset')
26
+ with tarfile.open(path) as f:
27
+ f.extractall()
28
+ return sorted(image_dir.glob('*'))
29
+
30
+
31
+ def load_model() -> tf.keras.Model:
32
+ path = huggingface_hub.hf_hub_download('public-data/DeepDanbooru',
33
+ 'model-resnet_custom_v3.h5')
34
+ model = tf.keras.models.load_model(path)
35
+ return model
36
+
37
+
38
+ def load_labels() -> list[str]:
39
+ path = huggingface_hub.hf_hub_download('public-data/DeepDanbooru',
40
+ 'tags.txt')
41
+ with open(path) as f:
42
+ labels = [line.strip() for line in f.readlines()]
43
+ return labels
44
+
45
+
46
+ model = load_model()
47
+ labels = load_labels()
48
+
49
+
50
+ def predict(image: PIL.Image.Image,
51
+ score_threshold: float) -> dict[str, float]:
52
+ _, height, width, _ = model.input_shape
53
+ image = np.asarray(image)
54
+ image = tf.image.resize(image,
55
+ size=(height, width),
56
+ method=tf.image.ResizeMethod.AREA,
57
+ preserve_aspect_ratio=True)
58
+ image = image.numpy()
59
+ image = dd.image.transform_and_pad_image(image, width, height)
60
+ image = image / 255.
61
+ probs = model.predict(image[None, ...])[0]
62
+ probs = probs.astype(float)
63
+ res = dict()
64
+ for prob, label in zip(probs.tolist(), labels):
65
+ if prob < score_threshold:
66
+ continue
67
+ res[label] = prob
68
+ return res
69
+
70
+
71
+ image_paths = load_sample_image_paths()
72
+ examples = [[path.as_posix(), 0.5] for path in image_paths]
73
+
74
+ with gr.Blocks(css='style.css') as demo:
75
+ gr.Markdown(DESCRIPTION)
76
+ with gr.Row():
77
+ with gr.Column():
78
+ image = gr.Image(label='Input', type='pil')
79
+ score_threshold = gr.Slider(label='Score threshold',
80
+ minimum=0,
81
+ maximum=1,
82
+ step=0.05,
83
+ value=0.5)
84
+ run_button = gr.Button('Run')
85
+ with gr.Column():
86
+ result = gr.Label(label='Output')
87
+ gr.Examples(examples=examples,
88
+ inputs=[image, score_threshold],
89
+ outputs=result,
90
+ fn=predict,
91
+ cache_examples=os.getenv('CACHE_EXAMPLES') == '1')
92
+
93
+ run_button.click(fn=predict,
94
+ inputs=[image, score_threshold],
95
+ outputs=result,
96
+ api_name='predict')
97
+ demo.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ pillow>=9.0.0
2
+ tensorflow>=2.7.0
3
+ git+https://github.com/KichangKim/DeepDanbooru@v3-20200915-sgd-e30#egg=deepdanbooru
style.css ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }