Spaces:
Running
Running
Commit
·
c3fca62
0
Parent(s):
Duplicate from SmilingWolf/wd-v1-4-tags
Browse filesCo-authored-by: Smiling Wolf <[email protected]>
- .gitattributes +27 -0
- .gitignore +1 -0
- README.md +39 -0
- Utils/dbimutils.py +54 -0
- app.py +267 -0
- power.jpg +0 -0
- requirements.txt +5 -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: WaifuDiffusion v1.4 Tags
|
| 3 |
+
emoji: 💬
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.16.2
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
duplicated_from: SmilingWolf/wd-v1-4-tags
|
| 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.
|
Utils/dbimutils.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# DanBooru IMage Utility functions
|
| 2 |
+
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def smart_imread(img, flag=cv2.IMREAD_UNCHANGED):
|
| 9 |
+
if img.endswith(".gif"):
|
| 10 |
+
img = Image.open(img)
|
| 11 |
+
img = img.convert("RGB")
|
| 12 |
+
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
| 13 |
+
else:
|
| 14 |
+
img = cv2.imread(img, flag)
|
| 15 |
+
return img
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def smart_24bit(img):
|
| 19 |
+
if img.dtype is np.dtype(np.uint16):
|
| 20 |
+
img = (img / 257).astype(np.uint8)
|
| 21 |
+
|
| 22 |
+
if len(img.shape) == 2:
|
| 23 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 24 |
+
elif img.shape[2] == 4:
|
| 25 |
+
trans_mask = img[:, :, 3] == 0
|
| 26 |
+
img[trans_mask] = [255, 255, 255, 255]
|
| 27 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
|
| 28 |
+
return img
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def make_square(img, target_size):
|
| 32 |
+
old_size = img.shape[:2]
|
| 33 |
+
desired_size = max(old_size)
|
| 34 |
+
desired_size = max(desired_size, target_size)
|
| 35 |
+
|
| 36 |
+
delta_w = desired_size - old_size[1]
|
| 37 |
+
delta_h = desired_size - old_size[0]
|
| 38 |
+
top, bottom = delta_h // 2, delta_h - (delta_h // 2)
|
| 39 |
+
left, right = delta_w // 2, delta_w - (delta_w // 2)
|
| 40 |
+
|
| 41 |
+
color = [255, 255, 255]
|
| 42 |
+
new_im = cv2.copyMakeBorder(
|
| 43 |
+
img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color
|
| 44 |
+
)
|
| 45 |
+
return new_im
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def smart_resize(img, size):
|
| 49 |
+
# Assumes the image has already gone through make_square
|
| 50 |
+
if img.shape[0] > size:
|
| 51 |
+
img = cv2.resize(img, (size, size), interpolation=cv2.INTER_AREA)
|
| 52 |
+
elif img.shape[0] < size:
|
| 53 |
+
img = cv2.resize(img, (size, size), interpolation=cv2.INTER_CUBIC)
|
| 54 |
+
return img
|
app.py
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import argparse
|
| 4 |
+
import functools
|
| 5 |
+
import html
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import huggingface_hub
|
| 10 |
+
import numpy as np
|
| 11 |
+
import onnxruntime as rt
|
| 12 |
+
import pandas as pd
|
| 13 |
+
import piexif
|
| 14 |
+
import piexif.helper
|
| 15 |
+
import PIL.Image
|
| 16 |
+
|
| 17 |
+
from Utils import dbimutils
|
| 18 |
+
|
| 19 |
+
TITLE = "WaifuDiffusion v1.4 Tags"
|
| 20 |
+
DESCRIPTION = """
|
| 21 |
+
Demo for:
|
| 22 |
+
- [SmilingWolf/wd-v1-4-swinv2-tagger-v2](https://huggingface.co/SmilingWolf/wd-v1-4-convnext-tagger-v2)
|
| 23 |
+
- [SmilingWolf/wd-v1-4-convnext-tagger-v2](https://huggingface.co/SmilingWolf/wd-v1-4-convnext-tagger-v2)
|
| 24 |
+
- [SmilingWolf/wd-v1-4-vit-tagger-v2](https://huggingface.co/SmilingWolf/wd-v1-4-vit-tagger-v2)
|
| 25 |
+
|
| 26 |
+
Includes "ready to copy" prompt and a prompt analyzer.
|
| 27 |
+
|
| 28 |
+
Modified from [NoCrypt/DeepDanbooru_string](https://huggingface.co/spaces/NoCrypt/DeepDanbooru_string)
|
| 29 |
+
Modified from [hysts/DeepDanbooru](https://huggingface.co/spaces/hysts/DeepDanbooru)
|
| 30 |
+
|
| 31 |
+
PNG Info code forked from [AUTOMATIC1111/stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui)
|
| 32 |
+
|
| 33 |
+
Example image by [ほし☆☆☆](https://www.pixiv.net/en/users/43565085)
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
| 37 |
+
SWIN_MODEL_REPO = "SmilingWolf/wd-v1-4-swinv2-tagger-v2"
|
| 38 |
+
CONV_MODEL_REPO = "SmilingWolf/wd-v1-4-convnext-tagger-v2"
|
| 39 |
+
VIT_MODEL_REPO = "SmilingWolf/wd-v1-4-vit-tagger-v2"
|
| 40 |
+
MODEL_FILENAME = "model.onnx"
|
| 41 |
+
LABEL_FILENAME = "selected_tags.csv"
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def parse_args() -> argparse.Namespace:
|
| 45 |
+
parser = argparse.ArgumentParser()
|
| 46 |
+
parser.add_argument("--score-slider-step", type=float, default=0.05)
|
| 47 |
+
parser.add_argument("--score-general-threshold", type=float, default=0.35)
|
| 48 |
+
parser.add_argument("--score-character-threshold", type=float, default=0.85)
|
| 49 |
+
parser.add_argument("--share", action="store_true")
|
| 50 |
+
return parser.parse_args()
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def load_model(model_repo: str, model_filename: str) -> rt.InferenceSession:
|
| 54 |
+
path = huggingface_hub.hf_hub_download(
|
| 55 |
+
model_repo, model_filename, use_auth_token=HF_TOKEN
|
| 56 |
+
)
|
| 57 |
+
model = rt.InferenceSession(path)
|
| 58 |
+
return model
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def change_model(model_name):
|
| 62 |
+
global loaded_models
|
| 63 |
+
|
| 64 |
+
if model_name == "SwinV2":
|
| 65 |
+
model = load_model(SWIN_MODEL_REPO, MODEL_FILENAME)
|
| 66 |
+
elif model_name == "ConvNext":
|
| 67 |
+
model = load_model(CONV_MODEL_REPO, MODEL_FILENAME)
|
| 68 |
+
elif model_name == "ViT":
|
| 69 |
+
model = load_model(VIT_MODEL_REPO, MODEL_FILENAME)
|
| 70 |
+
|
| 71 |
+
loaded_models[model_name] = model
|
| 72 |
+
return loaded_models[model_name]
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def load_labels() -> list[str]:
|
| 76 |
+
path = huggingface_hub.hf_hub_download(
|
| 77 |
+
SWIN_MODEL_REPO, LABEL_FILENAME, use_auth_token=HF_TOKEN
|
| 78 |
+
)
|
| 79 |
+
df = pd.read_csv(path)
|
| 80 |
+
|
| 81 |
+
tag_names = df["name"].tolist()
|
| 82 |
+
rating_indexes = list(np.where(df["category"] == 9)[0])
|
| 83 |
+
general_indexes = list(np.where(df["category"] == 0)[0])
|
| 84 |
+
character_indexes = list(np.where(df["category"] == 4)[0])
|
| 85 |
+
return tag_names, rating_indexes, general_indexes, character_indexes
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def plaintext_to_html(text):
|
| 89 |
+
text = (
|
| 90 |
+
"<p>" + "<br>\n".join([f"{html.escape(x)}" for x in text.split("\n")]) + "</p>"
|
| 91 |
+
)
|
| 92 |
+
return text
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def predict(
|
| 96 |
+
image: PIL.Image.Image,
|
| 97 |
+
model_name: str,
|
| 98 |
+
general_threshold: float,
|
| 99 |
+
character_threshold: float,
|
| 100 |
+
tag_names: list[str],
|
| 101 |
+
rating_indexes: list[np.int64],
|
| 102 |
+
general_indexes: list[np.int64],
|
| 103 |
+
character_indexes: list[np.int64],
|
| 104 |
+
):
|
| 105 |
+
global loaded_models
|
| 106 |
+
|
| 107 |
+
rawimage = image
|
| 108 |
+
|
| 109 |
+
model = loaded_models[model_name]
|
| 110 |
+
if model is None:
|
| 111 |
+
model = change_model(model_name)
|
| 112 |
+
|
| 113 |
+
_, height, width, _ = model.get_inputs()[0].shape
|
| 114 |
+
|
| 115 |
+
# Alpha to white
|
| 116 |
+
image = image.convert("RGBA")
|
| 117 |
+
new_image = PIL.Image.new("RGBA", image.size, "WHITE")
|
| 118 |
+
new_image.paste(image, mask=image)
|
| 119 |
+
image = new_image.convert("RGB")
|
| 120 |
+
image = np.asarray(image)
|
| 121 |
+
|
| 122 |
+
# PIL RGB to OpenCV BGR
|
| 123 |
+
image = image[:, :, ::-1]
|
| 124 |
+
|
| 125 |
+
image = dbimutils.make_square(image, height)
|
| 126 |
+
image = dbimutils.smart_resize(image, height)
|
| 127 |
+
image = image.astype(np.float32)
|
| 128 |
+
image = np.expand_dims(image, 0)
|
| 129 |
+
|
| 130 |
+
input_name = model.get_inputs()[0].name
|
| 131 |
+
label_name = model.get_outputs()[0].name
|
| 132 |
+
probs = model.run([label_name], {input_name: image})[0]
|
| 133 |
+
|
| 134 |
+
labels = list(zip(tag_names, probs[0].astype(float)))
|
| 135 |
+
|
| 136 |
+
# First 4 labels are actually ratings: pick one with argmax
|
| 137 |
+
ratings_names = [labels[i] for i in rating_indexes]
|
| 138 |
+
rating = dict(ratings_names)
|
| 139 |
+
|
| 140 |
+
# Then we have general tags: pick any where prediction confidence > threshold
|
| 141 |
+
general_names = [labels[i] for i in general_indexes]
|
| 142 |
+
general_res = [x for x in general_names if x[1] > general_threshold]
|
| 143 |
+
general_res = dict(general_res)
|
| 144 |
+
|
| 145 |
+
# Everything else is characters: pick any where prediction confidence > threshold
|
| 146 |
+
character_names = [labels[i] for i in character_indexes]
|
| 147 |
+
character_res = [x for x in character_names if x[1] > character_threshold]
|
| 148 |
+
character_res = dict(character_res)
|
| 149 |
+
|
| 150 |
+
b = dict(sorted(general_res.items(), key=lambda item: item[1], reverse=True))
|
| 151 |
+
a = (
|
| 152 |
+
", ".join(list(b.keys()))
|
| 153 |
+
.replace("_", " ")
|
| 154 |
+
.replace("(", "\(")
|
| 155 |
+
.replace(")", "\)")
|
| 156 |
+
)
|
| 157 |
+
c = ", ".join(list(b.keys()))
|
| 158 |
+
|
| 159 |
+
items = rawimage.info
|
| 160 |
+
geninfo = ""
|
| 161 |
+
|
| 162 |
+
if "exif" in rawimage.info:
|
| 163 |
+
exif = piexif.load(rawimage.info["exif"])
|
| 164 |
+
exif_comment = (exif or {}).get("Exif", {}).get(piexif.ExifIFD.UserComment, b"")
|
| 165 |
+
try:
|
| 166 |
+
exif_comment = piexif.helper.UserComment.load(exif_comment)
|
| 167 |
+
except ValueError:
|
| 168 |
+
exif_comment = exif_comment.decode("utf8", errors="ignore")
|
| 169 |
+
|
| 170 |
+
items["exif comment"] = exif_comment
|
| 171 |
+
geninfo = exif_comment
|
| 172 |
+
|
| 173 |
+
for field in [
|
| 174 |
+
"jfif",
|
| 175 |
+
"jfif_version",
|
| 176 |
+
"jfif_unit",
|
| 177 |
+
"jfif_density",
|
| 178 |
+
"dpi",
|
| 179 |
+
"exif",
|
| 180 |
+
"loop",
|
| 181 |
+
"background",
|
| 182 |
+
"timestamp",
|
| 183 |
+
"duration",
|
| 184 |
+
]:
|
| 185 |
+
items.pop(field, None)
|
| 186 |
+
|
| 187 |
+
geninfo = items.get("parameters", geninfo)
|
| 188 |
+
|
| 189 |
+
info = f"""
|
| 190 |
+
<p><h4>PNG Info</h4></p>
|
| 191 |
+
"""
|
| 192 |
+
for key, text in items.items():
|
| 193 |
+
info += (
|
| 194 |
+
f"""
|
| 195 |
+
<div>
|
| 196 |
+
<p><b>{plaintext_to_html(str(key))}</b></p>
|
| 197 |
+
<p>{plaintext_to_html(str(text))}</p>
|
| 198 |
+
</div>
|
| 199 |
+
""".strip()
|
| 200 |
+
+ "\n"
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
if len(info) == 0:
|
| 204 |
+
message = "Nothing found in the image."
|
| 205 |
+
info = f"<div><p>{message}<p></div>"
|
| 206 |
+
|
| 207 |
+
return (a, c, rating, character_res, general_res, info)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def main():
|
| 211 |
+
global loaded_models
|
| 212 |
+
loaded_models = {"SwinV2": None, "ConvNext": None, "ViT": None}
|
| 213 |
+
|
| 214 |
+
args = parse_args()
|
| 215 |
+
|
| 216 |
+
change_model("SwinV2")
|
| 217 |
+
|
| 218 |
+
tag_names, rating_indexes, general_indexes, character_indexes = load_labels()
|
| 219 |
+
|
| 220 |
+
func = functools.partial(
|
| 221 |
+
predict,
|
| 222 |
+
tag_names=tag_names,
|
| 223 |
+
rating_indexes=rating_indexes,
|
| 224 |
+
general_indexes=general_indexes,
|
| 225 |
+
character_indexes=character_indexes,
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
gr.Interface(
|
| 229 |
+
fn=func,
|
| 230 |
+
inputs=[
|
| 231 |
+
gr.Image(type="pil", label="Input"),
|
| 232 |
+
gr.Radio(["SwinV2", "ConvNext", "ViT"], value="SwinV2", label="Model"),
|
| 233 |
+
gr.Slider(
|
| 234 |
+
0,
|
| 235 |
+
1,
|
| 236 |
+
step=args.score_slider_step,
|
| 237 |
+
value=args.score_general_threshold,
|
| 238 |
+
label="General Tags Threshold",
|
| 239 |
+
),
|
| 240 |
+
gr.Slider(
|
| 241 |
+
0,
|
| 242 |
+
1,
|
| 243 |
+
step=args.score_slider_step,
|
| 244 |
+
value=args.score_character_threshold,
|
| 245 |
+
label="Character Tags Threshold",
|
| 246 |
+
),
|
| 247 |
+
],
|
| 248 |
+
outputs=[
|
| 249 |
+
gr.Textbox(label="Output (string)"),
|
| 250 |
+
gr.Textbox(label="Output (raw string)"),
|
| 251 |
+
gr.Label(label="Rating"),
|
| 252 |
+
gr.Label(label="Output (characters)"),
|
| 253 |
+
gr.Label(label="Output (tags)"),
|
| 254 |
+
gr.HTML(),
|
| 255 |
+
],
|
| 256 |
+
examples=[["power.jpg", "SwinV2", 0.35, 0.85]],
|
| 257 |
+
title=TITLE,
|
| 258 |
+
description=DESCRIPTION,
|
| 259 |
+
allow_flagging="never",
|
| 260 |
+
).launch(
|
| 261 |
+
enable_queue=True,
|
| 262 |
+
share=args.share,
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
if __name__ == "__main__":
|
| 267 |
+
main()
|
power.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pillow>=9.0.0
|
| 2 |
+
piexif>=1.1.3
|
| 3 |
+
onnxruntime>=1.12.0
|
| 4 |
+
opencv-python
|
| 5 |
+
huggingface-hub
|