narugo1992 commited on
Commit
21e723b
1 Parent(s): 32ef351

dev(narugo): upload chsex models

Browse files
Files changed (2) hide show
  1. app.py +18 -0
  2. chsex.py +42 -0
app.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import gradio as gr
4
 
5
  from aicheck import _gr_aicheck, _DEFAULT_AICHECK_MODEL, _AICHECK_MODELS
 
6
  from cls import _CLS_MODELS, _DEFAULT_CLS_MODEL, _gr_classification
7
  from monochrome import _gr_monochrome, _DEFAULT_MONO_MODEL, _MONO_MODELS
8
  from rating import _RATING_MODELS, _DEFAULT_RATING_MODEL, _gr_rating
@@ -78,4 +79,21 @@ if __name__ == '__main__':
78
  outputs=[gr_rating_output],
79
  )
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  demo.queue(os.cpu_count()).launch()
 
3
  import gradio as gr
4
 
5
  from aicheck import _gr_aicheck, _DEFAULT_AICHECK_MODEL, _AICHECK_MODELS
6
+ from chsex import _gr_chsex, _CHSEX_MODELS, _DEFAULT_CHSEX_MODEL
7
  from cls import _CLS_MODELS, _DEFAULT_CLS_MODEL, _gr_classification
8
  from monochrome import _gr_monochrome, _DEFAULT_MONO_MODEL, _MONO_MODELS
9
  from rating import _RATING_MODELS, _DEFAULT_RATING_MODEL, _gr_rating
 
79
  outputs=[gr_rating_output],
80
  )
81
 
82
+ with gr.Tab('Character Sex'):
83
+ with gr.Row():
84
+ with gr.Column():
85
+ gr_chsex_input_image = gr.Image(type='pil', label='Original Image')
86
+ gr_chsex_model = gr.Dropdown(_CHSEX_MODELS, value=_DEFAULT_CHSEX_MODEL, label='Model')
87
+ gr_chsex_infer_size = gr.Slider(224, 640, value=384, step=32, label='Infer Size')
88
+ gr_chsex_submit = gr.Button(value='Submit', variant='primary')
89
+
90
+ with gr.Column():
91
+ gr_chsex_output = gr.Label(label='Classes')
92
+
93
+ gr_chsex_submit.click(
94
+ _gr_chsex,
95
+ inputs=[gr_chsex_input_image, gr_chsex_model, gr_chsex_infer_size],
96
+ outputs=[gr_chsex_output],
97
+ )
98
+
99
  demo.queue(os.cpu_count()).launch()
chsex.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ from functools import lru_cache
4
+ from typing import Mapping, List
5
+
6
+ from huggingface_hub import HfFileSystem
7
+ from huggingface_hub import hf_hub_download
8
+ from imgutils.data import ImageTyping, load_image
9
+ from natsort import natsorted
10
+
11
+ from onnx_ import _open_onnx_model
12
+ from preprocess import _img_encode
13
+
14
+ hfs = HfFileSystem()
15
+
16
+ _REPO = 'deepghs/anime_ch_sex'
17
+ _CHSEX_MODELS = natsorted([
18
+ os.path.dirname(os.path.relpath(file, _REPO))
19
+ for file in hfs.glob(f'{_REPO}/*/model.onnx')
20
+ ])
21
+ _DEFAULT_CHSEX_MODEL = 'caformer_s36_v1'
22
+
23
+
24
+ @lru_cache()
25
+ def _open_anime_chsex_model(model_name):
26
+ return _open_onnx_model(hf_hub_download(_REPO, f'{model_name}/model.onnx'))
27
+
28
+
29
+ @lru_cache()
30
+ def _get_tags(model_name) -> List[str]:
31
+ with open(hf_hub_download(_REPO, f'{model_name}/meta.json'), 'r') as f:
32
+ return json.load(f)['labels']
33
+
34
+
35
+ def _gr_chsex(image: ImageTyping, model_name: str, size=384) -> Mapping[str, float]:
36
+ image = load_image(image, mode='RGB')
37
+ input_ = _img_encode(image, size=(size, size))[None, ...]
38
+ output, = _open_anime_chsex_model(model_name).run(['output'], {'input': input_})
39
+
40
+ labels = _get_tags(model_name)
41
+ values = dict(zip(labels, map(lambda x: x.item(), output[0])))
42
+ return values