Spaces:
Running
Running
Commit
·
1b58573
0
Parent(s):
Duplicate from NoCrypt/DeepDanbooru_string
Browse filesCo-authored-by: NoCRYPT <[email protected]>
- .gitattributes +27 -0
- .gitignore +1 -0
- README.md +39 -0
- app.py +185 -0
- miku.jpg +0 -0
- miku2.jpg +0 -0
- requirements.txt +4 -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
|
README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
duplicated_from: NoCrypt/DeepDanbooru_string
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# Configuration
|
| 14 |
+
|
| 15 |
+
`title`: _string_
|
| 16 |
+
Display title for the Space
|
| 17 |
+
|
| 18 |
+
`emoji`: _string_
|
| 19 |
+
Space emoji (emoji-only character allowed)
|
| 20 |
+
|
| 21 |
+
`colorFrom`: _string_
|
| 22 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
| 23 |
+
|
| 24 |
+
`colorTo`: _string_
|
| 25 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
| 26 |
+
|
| 27 |
+
`sdk`: _string_
|
| 28 |
+
Can be either `gradio`, `streamlit`, or `static`
|
| 29 |
+
|
| 30 |
+
`sdk_version` : _string_
|
| 31 |
+
Only applicable for `streamlit` SDK.
|
| 32 |
+
See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
|
| 33 |
+
|
| 34 |
+
`app_file`: _string_
|
| 35 |
+
Path to your main application file (which contains either `gradio` or `streamlit` Python code, or `static` html code).
|
| 36 |
+
Path is relative to the root of the repository.
|
| 37 |
+
|
| 38 |
+
`pinned`: _boolean_
|
| 39 |
+
Whether the Space stays on top of your list.
|
app.py
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import argparse
|
| 6 |
+
import functools
|
| 7 |
+
import os
|
| 8 |
+
import html
|
| 9 |
+
import pathlib
|
| 10 |
+
import tarfile
|
| 11 |
+
|
| 12 |
+
import deepdanbooru as dd
|
| 13 |
+
import gradio as gr
|
| 14 |
+
import huggingface_hub
|
| 15 |
+
import numpy as np
|
| 16 |
+
import PIL.Image
|
| 17 |
+
import tensorflow as tf
|
| 18 |
+
import piexif
|
| 19 |
+
import piexif.helper
|
| 20 |
+
|
| 21 |
+
TITLE = 'DeepDanbooru String'
|
| 22 |
+
|
| 23 |
+
TOKEN = os.environ['TOKEN']
|
| 24 |
+
MODEL_REPO = 'NoCrypt/DeepDanbooru_string'
|
| 25 |
+
MODEL_FILENAME = 'model-resnet_custom_v3.h5'
|
| 26 |
+
LABEL_FILENAME = 'tags.txt'
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def parse_args() -> argparse.Namespace:
|
| 30 |
+
parser = argparse.ArgumentParser()
|
| 31 |
+
parser.add_argument('--score-slider-step', type=float, default=0.05)
|
| 32 |
+
parser.add_argument('--score-threshold', type=float, default=0.5)
|
| 33 |
+
parser.add_argument('--theme', type=str, default='dark-grass')
|
| 34 |
+
parser.add_argument('--live', action='store_true')
|
| 35 |
+
parser.add_argument('--share', action='store_true')
|
| 36 |
+
parser.add_argument('--port', type=int)
|
| 37 |
+
parser.add_argument('--disable-queue',
|
| 38 |
+
dest='enable_queue',
|
| 39 |
+
action='store_false')
|
| 40 |
+
parser.add_argument('--allow-flagging', type=str, default='never')
|
| 41 |
+
return parser.parse_args()
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def load_sample_image_paths() -> list[pathlib.Path]:
|
| 45 |
+
image_dir = pathlib.Path('images')
|
| 46 |
+
if not image_dir.exists():
|
| 47 |
+
dataset_repo = 'hysts/sample-images-TADNE'
|
| 48 |
+
path = huggingface_hub.hf_hub_download(dataset_repo,
|
| 49 |
+
'images.tar.gz',
|
| 50 |
+
repo_type='dataset',
|
| 51 |
+
use_auth_token=TOKEN)
|
| 52 |
+
with tarfile.open(path) as f:
|
| 53 |
+
f.extractall()
|
| 54 |
+
return sorted(image_dir.glob('*'))
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def load_model() -> tf.keras.Model:
|
| 58 |
+
path = huggingface_hub.hf_hub_download(MODEL_REPO,
|
| 59 |
+
MODEL_FILENAME,
|
| 60 |
+
use_auth_token=TOKEN)
|
| 61 |
+
model = tf.keras.models.load_model(path)
|
| 62 |
+
return model
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def load_labels() -> list[str]:
|
| 66 |
+
path = huggingface_hub.hf_hub_download(MODEL_REPO,
|
| 67 |
+
LABEL_FILENAME,
|
| 68 |
+
use_auth_token=TOKEN)
|
| 69 |
+
with open(path) as f:
|
| 70 |
+
labels = [line.strip() for line in f.readlines()]
|
| 71 |
+
return labels
|
| 72 |
+
|
| 73 |
+
def plaintext_to_html(text):
|
| 74 |
+
text = "<p>" + "<br>\n".join([f"{html.escape(x)}" for x in text.split('\n')]) + "</p>"
|
| 75 |
+
return text
|
| 76 |
+
|
| 77 |
+
def predict(image: PIL.Image.Image, score_threshold: float,
|
| 78 |
+
model: tf.keras.Model, labels: list[str]) -> dict[str, float]:
|
| 79 |
+
rawimage = image
|
| 80 |
+
_, height, width, _ = model.input_shape
|
| 81 |
+
image = np.asarray(image)
|
| 82 |
+
image = tf.image.resize(image,
|
| 83 |
+
size=(height, width),
|
| 84 |
+
method=tf.image.ResizeMethod.AREA,
|
| 85 |
+
preserve_aspect_ratio=True)
|
| 86 |
+
image = image.numpy()
|
| 87 |
+
image = dd.image.transform_and_pad_image(image, width, height)
|
| 88 |
+
image = image / 255.
|
| 89 |
+
probs = model.predict(image[None, ...])[0]
|
| 90 |
+
probs = probs.astype(float)
|
| 91 |
+
res = dict()
|
| 92 |
+
for prob, label in zip(probs.tolist(), labels):
|
| 93 |
+
if prob < score_threshold:
|
| 94 |
+
continue
|
| 95 |
+
res[label] = prob
|
| 96 |
+
b = dict(sorted(res.items(),key=lambda item:item[1], reverse=True))
|
| 97 |
+
a = ', '.join(list(b.keys())).replace('_',' ').replace('(','\(').replace(')','\)')
|
| 98 |
+
c = ', '.join(list(b.keys()))
|
| 99 |
+
|
| 100 |
+
items = rawimage.info
|
| 101 |
+
geninfo = ''
|
| 102 |
+
|
| 103 |
+
if "exif" in rawimage.info:
|
| 104 |
+
exif = piexif.load(rawimage.info["exif"])
|
| 105 |
+
exif_comment = (exif or {}).get("Exif", {}).get(piexif.ExifIFD.UserComment, b'')
|
| 106 |
+
try:
|
| 107 |
+
exif_comment = piexif.helper.UserComment.load(exif_comment)
|
| 108 |
+
except ValueError:
|
| 109 |
+
exif_comment = exif_comment.decode('utf8', errors="ignore")
|
| 110 |
+
|
| 111 |
+
items['exif comment'] = exif_comment
|
| 112 |
+
geninfo = exif_comment
|
| 113 |
+
|
| 114 |
+
for field in ['jfif', 'jfif_version', 'jfif_unit', 'jfif_density', 'dpi', 'exif',
|
| 115 |
+
'loop', 'background', 'timestamp', 'duration']:
|
| 116 |
+
items.pop(field, None)
|
| 117 |
+
|
| 118 |
+
geninfo = items.get('parameters', geninfo)
|
| 119 |
+
|
| 120 |
+
info = f"""
|
| 121 |
+
<p><h4>PNG Info</h4></p>
|
| 122 |
+
"""
|
| 123 |
+
for key, text in items.items():
|
| 124 |
+
info += f"""
|
| 125 |
+
<div>
|
| 126 |
+
<p><b>{plaintext_to_html(str(key))}</b></p>
|
| 127 |
+
<p>{plaintext_to_html(str(text))}</p>
|
| 128 |
+
</div>
|
| 129 |
+
""".strip()+"\n"
|
| 130 |
+
|
| 131 |
+
if len(info) == 0:
|
| 132 |
+
message = "Nothing found in the image."
|
| 133 |
+
info = f"<div><p>{message}<p></div>"
|
| 134 |
+
|
| 135 |
+
return (a,c,res,info)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def main():
|
| 139 |
+
args = parse_args()
|
| 140 |
+
model = load_model()
|
| 141 |
+
labels = load_labels()
|
| 142 |
+
|
| 143 |
+
func = functools.partial(predict, model=model, labels=labels)
|
| 144 |
+
func = functools.update_wrapper(func, predict)
|
| 145 |
+
|
| 146 |
+
gr.Interface(
|
| 147 |
+
func,
|
| 148 |
+
[
|
| 149 |
+
gr.inputs.Image(type='pil', label='Input'),
|
| 150 |
+
gr.inputs.Slider(0,
|
| 151 |
+
1,
|
| 152 |
+
step=args.score_slider_step,
|
| 153 |
+
default=args.score_threshold,
|
| 154 |
+
label='Score Threshold'),
|
| 155 |
+
],
|
| 156 |
+
[
|
| 157 |
+
gr.outputs.Textbox(label='Output (string)'),
|
| 158 |
+
gr.outputs.Textbox(label='Output (raw string)'),
|
| 159 |
+
gr.outputs.Label(label='Output (label)'),
|
| 160 |
+
gr.outputs.HTML()
|
| 161 |
+
],
|
| 162 |
+
examples=[
|
| 163 |
+
['miku.jpg',0.5],
|
| 164 |
+
['miku2.jpg',0.5]
|
| 165 |
+
],
|
| 166 |
+
title=TITLE,
|
| 167 |
+
description='''
|
| 168 |
+
Demo for [KichangKim/DeepDanbooru](https://github.com/KichangKim/DeepDanbooru) with "ready to copy" prompt and a prompt analyzer.
|
| 169 |
+
|
| 170 |
+
Modified from [hysts/DeepDanbooru](https://huggingface.co/spaces/hysts/DeepDanbooru)
|
| 171 |
+
|
| 172 |
+
PNG Info code forked from [AUTOMATIC1111/stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui)
|
| 173 |
+
''',
|
| 174 |
+
theme=args.theme,
|
| 175 |
+
allow_flagging=args.allow_flagging,
|
| 176 |
+
live=args.live,
|
| 177 |
+
).launch(
|
| 178 |
+
enable_queue=args.enable_queue,
|
| 179 |
+
server_port=args.port,
|
| 180 |
+
share=args.share,
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
if __name__ == '__main__':
|
| 185 |
+
main()
|
miku.jpg
ADDED
|
miku2.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pillow>=9.0.0
|
| 2 |
+
tensorflow>=2.7.0
|
| 3 |
+
git+https://github.com/KichangKim/DeepDanbooru@v3-20200915-sgd-e30#egg=deepdanbooru
|
| 4 |
+
piexif>=1.1.3
|