NoCrypt commited on
Commit
fc1cddb
1 Parent(s): 0fd6a68

Upload 5 files

Browse files
Files changed (5) hide show
  1. .gitattributes +2 -6
  2. .gitignore +1 -0
  3. README.md +31 -5
  4. app.py +131 -0
  5. requirements.txt +3 -0
.gitattributes CHANGED
@@ -1,6 +1,7 @@
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
  *.bz2 filter=lfs diff=lfs merge=lfs -text
5
  *.ftz filter=lfs diff=lfs merge=lfs -text
6
  *.gz filter=lfs diff=lfs merge=lfs -text
@@ -9,13 +10,9 @@
9
  *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
  *.model filter=lfs diff=lfs merge=lfs -text
11
  *.msgpack filter=lfs diff=lfs merge=lfs -text
12
- *.npy filter=lfs diff=lfs merge=lfs -text
13
- *.npz filter=lfs diff=lfs merge=lfs -text
14
  *.onnx filter=lfs diff=lfs merge=lfs -text
15
  *.ot filter=lfs diff=lfs merge=lfs -text
16
  *.parquet filter=lfs diff=lfs merge=lfs -text
17
- *.pickle filter=lfs diff=lfs merge=lfs -text
18
- *.pkl filter=lfs diff=lfs merge=lfs -text
19
  *.pb filter=lfs diff=lfs merge=lfs -text
20
  *.pt filter=lfs diff=lfs merge=lfs -text
21
  *.pth filter=lfs diff=lfs merge=lfs -text
@@ -24,8 +21,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
24
  *.tar.* filter=lfs diff=lfs merge=lfs -text
25
  *.tflite filter=lfs diff=lfs merge=lfs -text
26
  *.tgz filter=lfs diff=lfs merge=lfs -text
27
- *.wasm filter=lfs diff=lfs merge=lfs -text
28
  *.xz filter=lfs diff=lfs merge=lfs -text
29
  *.zip filter=lfs diff=lfs merge=lfs -text
30
- *.zst filter=lfs diff=lfs merge=lfs -text
31
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
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
 
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
 
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
README.md CHANGED
@@ -1,12 +1,38 @@
1
  ---
2
  title: DeepDanbooru String
3
- emoji: 📊
4
- colorFrom: pink
5
- colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 3.5
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: DeepDanbooru String
3
+ emoji: 💬
4
+ colorFrom: blue
5
+ colorTo: red
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.
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import functools
7
+ import os
8
+ import pathlib
9
+ import tarfile
10
+
11
+ import deepdanbooru as dd
12
+ import gradio as gr
13
+ import huggingface_hub
14
+ import numpy as np
15
+ import PIL.Image
16
+ import tensorflow as tf
17
+
18
+ TITLE = 'NoCrypt/DeepDanbooru_string'
19
+ DESCRIPTION = 'Cloned from: https://huggingface.co/spaces/hysts/DeepDanbooru'
20
+
21
+ TOKEN = os.environ['TOKEN']
22
+ MODEL_REPO = 'NoCrypt/DeepDanbooru_string'
23
+ MODEL_FILENAME = 'model-resnet_custom_v3.h5'
24
+ LABEL_FILENAME = 'tags.txt'
25
+
26
+
27
+ def parse_args() -> argparse.Namespace:
28
+ parser = argparse.ArgumentParser()
29
+ parser.add_argument('--score-slider-step', type=float, default=0.05)
30
+ parser.add_argument('--score-threshold', type=float, default=0.5)
31
+ parser.add_argument('--theme', type=str, default='dark-grass')
32
+ parser.add_argument('--live', action='store_true')
33
+ parser.add_argument('--share', action='store_true')
34
+ parser.add_argument('--port', type=int)
35
+ parser.add_argument('--disable-queue',
36
+ dest='enable_queue',
37
+ action='store_false')
38
+ parser.add_argument('--allow-flagging', type=str, default='never')
39
+ return parser.parse_args()
40
+
41
+
42
+ def load_sample_image_paths() -> list[pathlib.Path]:
43
+ image_dir = pathlib.Path('images')
44
+ if not image_dir.exists():
45
+ dataset_repo = 'hysts/sample-images-TADNE'
46
+ path = huggingface_hub.hf_hub_download(dataset_repo,
47
+ 'images.tar.gz',
48
+ repo_type='dataset',
49
+ use_auth_token=TOKEN)
50
+ with tarfile.open(path) as f:
51
+ f.extractall()
52
+ return sorted(image_dir.glob('*'))
53
+
54
+
55
+ def load_model() -> tf.keras.Model:
56
+ path = huggingface_hub.hf_hub_download(MODEL_REPO,
57
+ MODEL_FILENAME,
58
+ use_auth_token=TOKEN)
59
+ model = tf.keras.models.load_model(path)
60
+ return model
61
+
62
+
63
+ def load_labels() -> list[str]:
64
+ path = huggingface_hub.hf_hub_download(MODEL_REPO,
65
+ LABEL_FILENAME,
66
+ use_auth_token=TOKEN)
67
+ with open(path) as f:
68
+ labels = [line.strip() for line in f.readlines()]
69
+ return labels
70
+
71
+
72
+ def predict(image: PIL.Image.Image, score_threshold: float,
73
+ model: tf.keras.Model, labels: list[str]) -> dict[str, float]:
74
+ _, height, width, _ = model.input_shape
75
+ image = np.asarray(image)
76
+ image = tf.image.resize(image,
77
+ size=(height, width),
78
+ method=tf.image.ResizeMethod.AREA,
79
+ preserve_aspect_ratio=True)
80
+ image = image.numpy()
81
+ image = dd.image.transform_and_pad_image(image, width, height)
82
+ image = image / 255.
83
+ probs = model.predict(image[None, ...])[0]
84
+ probs = probs.astype(float)
85
+ res = dict()
86
+ for prob, label in zip(probs.tolist(), labels):
87
+ if prob < score_threshold:
88
+ continue
89
+ res[label] = prob
90
+ return res
91
+
92
+
93
+ def main():
94
+ args = parse_args()
95
+
96
+ image_paths = load_sample_image_paths()
97
+ examples = [[path.as_posix(), args.score_threshold]
98
+ for path in image_paths]
99
+
100
+ model = load_model()
101
+ labels = load_labels()
102
+
103
+ func = functools.partial(predict, model=model, labels=labels)
104
+ func = functools.update_wrapper(func, predict)
105
+
106
+ gr.Interface(
107
+ func,
108
+ [
109
+ gr.inputs.Image(type='pil', label='Input'),
110
+ gr.inputs.Slider(0,
111
+ 1,
112
+ step=args.score_slider_step,
113
+ default=args.score_threshold,
114
+ label='Score Threshold'),
115
+ ],
116
+ gr.outputs.Textbox(label='Output'),
117
+ examples=examples,
118
+ title=TITLE,
119
+ description=DESCRIPTION,
120
+ theme=args.theme,
121
+ allow_flagging=args.allow_flagging,
122
+ live=args.live,
123
+ ).launch(
124
+ enable_queue=args.enable_queue,
125
+ server_port=args.port,
126
+ share=args.share,
127
+ )
128
+
129
+
130
+ if __name__ == '__main__':
131
+ main()
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