MonkeyJuice commited on
Commit
8cdd8e4
1 Parent(s): bbe49e5
Files changed (4) hide show
  1. app.py +44 -49
  2. ignoreTag.txt +62 -0
  3. ignoreTag2.txt +3 -0
  4. style.css +22 -2
app.py CHANGED
@@ -2,10 +2,6 @@
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
@@ -13,21 +9,6 @@ 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')
@@ -47,9 +28,7 @@ model = load_model()
47
  labels = load_labels()
48
 
49
 
50
- def predict(
51
- image: PIL.Image.Image, score_threshold: float
52
- ) -> tuple[dict[str, float], dict[str, float], str]:
53
  _, height, width, _ = model.input_shape
54
  image = np.asarray(image)
55
  image = tf.image.resize(image,
@@ -65,24 +44,51 @@ def predict(
65
  indices = np.argsort(probs)[::-1]
66
  result_all = dict()
67
  result_threshold = dict()
 
68
  for index in indices:
69
  label = labels[index]
70
  prob = probs[index]
71
  result_all[label] = prob
72
  if prob < score_threshold:
73
  break
 
74
  result_threshold[label] = prob
75
- result_text = ', '.join(result_all.keys())
76
- return result_threshold, result_all, result_text
77
-
78
-
79
- image_paths = load_sample_image_paths()
80
- examples = [[path.as_posix(), 0.5] for path in image_paths]
81
-
82
- with gr.Blocks(css='style.css') as demo:
83
- gr.Markdown(DESCRIPTION)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  with gr.Row():
85
- with gr.Column():
86
  image = gr.Image(label='Input', type='pil')
87
  score_threshold = gr.Slider(label='Score threshold',
88
  minimum=0,
@@ -90,27 +96,16 @@ with gr.Blocks(css='style.css') as demo:
90
  step=0.05,
91
  value=0.5)
92
  run_button = gr.Button('Run')
93
- with gr.Column():
94
- with gr.Tabs():
95
- with gr.Tab(label='Output'):
96
- result = gr.Label(label='Output', show_label=False)
97
- with gr.Tab(label='JSON'):
98
- result_json = gr.JSON(label='JSON output',
99
- show_label=False)
100
- with gr.Tab(label='Text'):
101
- result_text = gr.Text(label='Text output',
102
- show_label=False,
103
- lines=5)
104
- gr.Examples(examples=examples,
105
- inputs=[image, score_threshold],
106
- outputs=[result, result_json, result_text],
107
- fn=predict,
108
- cache_examples=os.getenv('CACHE_EXAMPLES') == '1')
109
 
110
  run_button.click(
111
  fn=predict,
112
  inputs=[image, score_threshold],
113
- outputs=[result, result_json, result_text],
114
  api_name='predict',
115
  )
 
 
116
  demo.queue().launch()
 
2
 
3
  from __future__ import annotations
4
 
 
 
 
 
5
  import deepdanbooru as dd
6
  import gradio as gr
7
  import huggingface_hub
 
9
  import PIL.Image
10
  import tensorflow as tf
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def load_model() -> tf.keras.Model:
13
  path = huggingface_hub.hf_hub_download('public-data/DeepDanbooru',
14
  'model-resnet_custom_v3.h5')
 
28
  labels = load_labels()
29
 
30
 
31
+ def predict(image: PIL.Image.Image, score_threshold: float):
 
 
32
  _, height, width, _ = model.input_shape
33
  image = np.asarray(image)
34
  image = tf.image.resize(image,
 
44
  indices = np.argsort(probs)[::-1]
45
  result_all = dict()
46
  result_threshold = dict()
47
+ result_html = ''
48
  for index in indices:
49
  label = labels[index]
50
  prob = probs[index]
51
  result_all[label] = prob
52
  if prob < score_threshold:
53
  break
54
+ result_html = result_html + '<p class="m5dd_list use"><span>' + str(label) + '</span><span>' + str(round(prob, 3)) + '</span></p>'
55
  result_threshold[label] = prob
56
+ result_text = ', '.join(result_threshold.keys())
57
+ result_text = '<div id="m5dd_result">' + str(result_text) + '</div>'
58
+ result_html = '<div>' + str(result_html) + '</div>'
59
+ return result_html, result_text
60
+
61
+ js = """
62
+ async () => {
63
+ document.addEventListener('click', function(event) {
64
+ let tagItem = event.target.closest('.m5dd_list')
65
+ let resultArea = event.target.closest('#m5dd_result')
66
+ if (tagItem){
67
+ if (tagItem.classList.contains('use')){
68
+ tagItem.classList.remove('use')
69
+ }else{
70
+ tagItem.classList.add('use')
71
+ }
72
+ document.getElementById('m5dd_result').innerText =
73
+ Array.from(document.querySelectorAll('.m5dd_list.use>span:nth-child(1)'))
74
+ .map(v=>v.innerText)
75
+ .join(', ')
76
+ }else if (resultArea){
77
+ const selection = window.getSelection()
78
+ selection.removeAllRanges()
79
+ const range = document.createRange()
80
+ range.selectNodeContents(resultArea)
81
+ selection.addRange(range)
82
+ }else{
83
+ return
84
+ }
85
+ })
86
+ }
87
+ """
88
+
89
+ with gr.Blocks(css="style.css") as demo:
90
  with gr.Row():
91
+ with gr.Column(scale=1):
92
  image = gr.Image(label='Input', type='pil')
93
  score_threshold = gr.Slider(label='Score threshold',
94
  minimum=0,
 
96
  step=0.05,
97
  value=0.5)
98
  run_button = gr.Button('Run')
99
+ result_text = gr.HTML(value="<div></div>")
100
+ with gr.Column(scale=3):
101
+ result_html = gr.HTML(value="<div></div>")
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  run_button.click(
104
  fn=predict,
105
  inputs=[image, score_threshold],
106
+ outputs=[result_html, result_text],
107
  api_name='predict',
108
  )
109
+ demo.load(None,None,None,_js=js)
110
+
111
  demo.queue().launch()
ignoreTag.txt ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ uncensored,
2
+ mosaic_censoring,
3
+ pointless_censoring,
4
+ convenient_censoring,
5
+ bar_censor,
6
+ heart_censor,
7
+ censored,
8
+ twitter_username,
9
+ patreon_username,
10
+ signature,
11
+ watermark,
12
+ artist_name,
13
+ character_name,
14
+ copyright_name,
15
+ artist_name,
16
+ virtual_youtuber,
17
+ eyebrows_visible_through_hair,
18
+ eyes_visible_through_hair,
19
+ hair_between_eyes,
20
+ web_address,
21
+ bangs,
22
+ monochrome,
23
+ letterboxed,
24
+ bad_feet,
25
+ oekaki,
26
+ holding_hands,
27
+ nail_polish,
28
+ sandwiched,
29
+ symbol-shaped_pupils,
30
+ greyscale,
31
+ sketch,
32
+ speech_bubble,
33
+ spoken_heart,
34
+ spoken_musical_note,
35
+ spoken_question_mark,
36
+ spoken_sweatdrop,
37
+ spoken_squiggle,
38
+ spoken_object,
39
+ letterboxed,
40
+ spoken_interrobang,
41
+ spoken_exclamation_mark,
42
+ spoken_anger_vein,
43
+ spoken_blush,
44
+ thought_bubble,
45
+ toe_scrunch,
46
+ character_censor,
47
+ novelty_censor,
48
+ aqua_nails,
49
+ black_nails,
50
+ green_nails,
51
+ fingernails,
52
+ multicolored_nails,
53
+ nail_art,
54
+ orange_nails,
55
+ red_nails,
56
+ pink_nails,
57
+ purple_nails,
58
+ toenail_polish,
59
+ toenails,
60
+ yellow_nails,
61
+ blue_nails,
62
+ interlocked_fingers,
ignoreTag2.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ rating:safe,
2
+ rating:questionable,
3
+ rating:explicit,
style.css CHANGED
@@ -1,3 +1,23 @@
1
- h1 {
2
- text-align: center;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  }
 
1
+ .m5dd_list {
2
+ display: flex;
3
+ cursor: pointer;
4
+ font-size: 1.2em;
5
+ padding: 0.2em 0.5em;
6
+ }
7
+
8
+ .m5dd_list>span:nth-child(1) {
9
+ flex: 1;
10
+ }
11
+
12
+ .m5dd_list>span:nth-child(2) {
13
+ color: #aaa;
14
+ }
15
+
16
+ .m5dd_list:nth-child(even) {
17
+ background: #ecedf0;
18
+ }
19
+
20
+ .m5dd_list:not(.use)>span {
21
+ text-decoration: line-through;
22
+ color: #ccc;
23
  }