Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
# Daftar model dan ControlNet
|
4 |
models = ["Model A", "Model B", "Model C"]
|
5 |
vae = ["VAE A", "VAE B", "VAE C"]
|
@@ -22,6 +35,7 @@ def controlnet_process(image, controlnet_type, model):
|
|
22 |
return f"Proses gambar dengan ControlNet '{controlnet_type}' dan model '{model}'"
|
23 |
|
24 |
#wd tagger
|
|
|
25 |
# Dataset v3 series of models:
|
26 |
SWINV2_MODEL_DSV3_REPO = "SmilingWolf/wd-swinv2-tagger-v3"
|
27 |
CONV_MODEL_DSV3_REPO = "SmilingWolf/wd-convnext-tagger-v3"
|
@@ -36,7 +50,181 @@ CONV_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-convnext-tagger-v2"
|
|
36 |
CONV2_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-convnextv2-tagger-v2"
|
37 |
VIT_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-vit-tagger-v2"
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
SWINV2_MODEL_DSV3_REPO,
|
41 |
CONV_MODEL_DSV3_REPO,
|
42 |
VIT_MODEL_DSV3_REPO,
|
@@ -47,7 +235,7 @@ dropdown_list = [
|
|
47 |
CONV_MODEL_DSV2_REPO,
|
48 |
CONV2_MODEL_DSV2_REPO,
|
49 |
VIT_MODEL_DSV2_REPO,
|
50 |
-
]
|
51 |
|
52 |
with gr.Blocks(css= "style.css") as app:
|
53 |
# Dropdown untuk memilih model di luar tab dengan lebar kecil
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
import argparse
|
4 |
+
import os
|
5 |
+
|
6 |
+
import gradio as gr
|
7 |
+
import huggingface_hub
|
8 |
+
import numpy as np
|
9 |
+
import onnxruntime as rt
|
10 |
+
import pandas as pd
|
11 |
+
from PIL import Image
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
# Daftar model dan ControlNet
|
17 |
models = ["Model A", "Model B", "Model C"]
|
18 |
vae = ["VAE A", "VAE B", "VAE C"]
|
|
|
35 |
return f"Proses gambar dengan ControlNet '{controlnet_type}' dan model '{model}'"
|
36 |
|
37 |
#wd tagger
|
38 |
+
|
39 |
# Dataset v3 series of models:
|
40 |
SWINV2_MODEL_DSV3_REPO = "SmilingWolf/wd-swinv2-tagger-v3"
|
41 |
CONV_MODEL_DSV3_REPO = "SmilingWolf/wd-convnext-tagger-v3"
|
|
|
50 |
CONV2_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-convnextv2-tagger-v2"
|
51 |
VIT_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-vit-tagger-v2"
|
52 |
|
53 |
+
# Files to download from the repos
|
54 |
+
MODEL_FILENAME = "model.onnx"
|
55 |
+
LABEL_FILENAME = "selected_tags.csv"
|
56 |
+
|
57 |
+
# https://github.com/toriato/stable-diffusion-webui-wd14-tagger/blob/a9eacb1eff904552d3012babfa28b57e1d3e295c/tagger/ui.py#L368
|
58 |
+
kaomojis = [ "0_0", "(o)_(o)", "+_+", "+_-", "._.", "<o>_<o>", "<|>_<|>", "=_=", ">_<", "3_3", "6_9", ">_o", "@_@", "^_^", "o_o", "u_u", "x_x", "|_|", "||_||", ]
|
59 |
+
|
60 |
+
def parse_args() -> argparse.Namespace:
|
61 |
+
parser = argparse.ArgumentParser()
|
62 |
+
parser.add_argument("--score-slider-step", type=float, default=0.05)
|
63 |
+
parser.add_argument("--score-general-threshold", type=float, default=0.35)
|
64 |
+
parser.add_argument("--score-character-threshold", type=float, default=0.85)
|
65 |
+
parser.add_argument("--share", action="store_true")
|
66 |
+
return parser.parse_args()
|
67 |
+
|
68 |
+
|
69 |
+
def load_labels(dataframe) -> list[str]:
|
70 |
+
name_series = dataframe["name"]
|
71 |
+
name_series = name_series.map(
|
72 |
+
lambda x: x.replace("_", " ") if x not in kaomojis else x
|
73 |
+
)
|
74 |
+
tag_names = name_series.tolist()
|
75 |
+
|
76 |
+
rating_indexes = list(np.where(dataframe["category"] == 9)[0])
|
77 |
+
general_indexes = list(np.where(dataframe["category"] == 0)[0])
|
78 |
+
character_indexes = list(np.where(dataframe["category"] == 4)[0])
|
79 |
+
return tag_names, rating_indexes, general_indexes, character_indexes
|
80 |
+
|
81 |
+
|
82 |
+
def mcut_threshold(probs):
|
83 |
+
"""
|
84 |
+
Maximum Cut Thresholding (MCut)
|
85 |
+
Largeron, C., Moulin, C., & Gery, M. (2012). MCut: A Thresholding Strategy
|
86 |
+
for Multi-label Classification. In 11th International Symposium, IDA 2012
|
87 |
+
(pp. 172-183).
|
88 |
+
"""
|
89 |
+
sorted_probs = probs[probs.argsort()[::-1]]
|
90 |
+
difs = sorted_probs[:-1] - sorted_probs[1:]
|
91 |
+
t = difs.argmax()
|
92 |
+
thresh = (sorted_probs[t] + sorted_probs[t + 1]) / 2
|
93 |
+
return thresh
|
94 |
+
|
95 |
+
|
96 |
+
class Predictor:
|
97 |
+
def __init__(self):
|
98 |
+
self.model_target_size = None
|
99 |
+
self.last_loaded_repo = None
|
100 |
+
|
101 |
+
def download_model(self, model_repo):
|
102 |
+
csv_path = huggingface_hub.hf_hub_download(
|
103 |
+
model_repo,
|
104 |
+
LABEL_FILENAME,
|
105 |
+
)
|
106 |
+
model_path = huggingface_hub.hf_hub_download(
|
107 |
+
model_repo,
|
108 |
+
MODEL_FILENAME,
|
109 |
+
)
|
110 |
+
return csv_path, model_path
|
111 |
+
|
112 |
+
def load_model(self, model_repo):
|
113 |
+
if model_repo == self.last_loaded_repo:
|
114 |
+
return
|
115 |
+
|
116 |
+
csv_path, model_path = self.download_model(model_repo)
|
117 |
+
|
118 |
+
tags_df = pd.read_csv(csv_path)
|
119 |
+
sep_tags = load_labels(tags_df)
|
120 |
+
|
121 |
+
self.tag_names = sep_tags[0]
|
122 |
+
self.rating_indexes = sep_tags[1]
|
123 |
+
self.general_indexes = sep_tags[2]
|
124 |
+
self.character_indexes = sep_tags[3]
|
125 |
+
|
126 |
+
model = rt.InferenceSession(model_path)
|
127 |
+
_, height, width, _ = model.get_inputs()[0].shape
|
128 |
+
self.model_target_size = height
|
129 |
+
|
130 |
+
self.last_loaded_repo = model_repo
|
131 |
+
self.model = model
|
132 |
+
|
133 |
+
def prepare_image(self, image):
|
134 |
+
target_size = self.model_target_size
|
135 |
+
|
136 |
+
canvas = Image.new("RGBA", image.size, (255, 255, 255))
|
137 |
+
canvas.alpha_composite(image)
|
138 |
+
image = canvas.convert("RGB")
|
139 |
+
|
140 |
+
# Pad image to square
|
141 |
+
image_shape = image.size
|
142 |
+
max_dim = max(image_shape)
|
143 |
+
pad_left = (max_dim - image_shape[0]) // 2
|
144 |
+
pad_top = (max_dim - image_shape[1]) // 2
|
145 |
+
|
146 |
+
padded_image = Image.new("RGB", (max_dim, max_dim), (255, 255, 255))
|
147 |
+
padded_image.paste(image, (pad_left, pad_top))
|
148 |
+
|
149 |
+
# Resize
|
150 |
+
if max_dim != target_size:
|
151 |
+
padded_image = padded_image.resize(
|
152 |
+
(target_size, target_size),
|
153 |
+
Image.BICUBIC,
|
154 |
+
)
|
155 |
+
|
156 |
+
# Convert to numpy array
|
157 |
+
image_array = np.asarray(padded_image, dtype=np.float32)
|
158 |
+
|
159 |
+
# Convert PIL-native RGB to BGR
|
160 |
+
image_array = image_array[:, :, ::-1]
|
161 |
+
|
162 |
+
return np.expand_dims(image_array, axis=0)
|
163 |
+
|
164 |
+
|
165 |
+
def predict(
|
166 |
+
self,
|
167 |
+
image,
|
168 |
+
model_repo,
|
169 |
+
general_thresh,
|
170 |
+
general_mcut_enabled,
|
171 |
+
character_thresh,
|
172 |
+
character_mcut_enabled,
|
173 |
+
):
|
174 |
+
self.load_model(model_repo)
|
175 |
+
|
176 |
+
image = self.prepare_image(image)
|
177 |
+
|
178 |
+
input_name = self.model.get_inputs()[0].name
|
179 |
+
label_name = self.model.get_outputs()[0].name
|
180 |
+
preds = self.model.run([label_name], {input_name: image})[0]
|
181 |
+
|
182 |
+
labels = list(zip(self.tag_names, preds[0].astype(float)))
|
183 |
+
|
184 |
+
# First 4 labels are actually ratings: pick one with argmax
|
185 |
+
ratings_names = [labels[i] for i in self.rating_indexes]
|
186 |
+
rating = dict(ratings_names)
|
187 |
+
|
188 |
+
# Then we have general tags: pick any where prediction confidence > threshold
|
189 |
+
general_names = [labels[i] for i in self.general_indexes]
|
190 |
+
|
191 |
+
if general_mcut_enabled:
|
192 |
+
general_probs = np.array([x[1] for x in general_names])
|
193 |
+
general_thresh = mcut_threshold(general_probs)
|
194 |
+
|
195 |
+
general_res = [x for x in general_names if x[1] > general_thresh]
|
196 |
+
general_res = dict(general_res)
|
197 |
+
|
198 |
+
# Everything else is characters: pick any where prediction confidence > threshold
|
199 |
+
character_names = [labels[i] for i in self.character_indexes]
|
200 |
+
|
201 |
+
if character_mcut_enabled:
|
202 |
+
character_probs = np.array([x[1] for x in character_names])
|
203 |
+
character_thresh = mcut_threshold(character_probs)
|
204 |
+
character_thresh = max(0.15, character_thresh)
|
205 |
+
|
206 |
+
character_res = [x for x in character_names if x[1] > character_thresh]
|
207 |
+
character_res = dict(character_res)
|
208 |
+
|
209 |
+
sorted_general_strings = sorted(
|
210 |
+
general_res.items(),
|
211 |
+
key=lambda x: x[1],
|
212 |
+
reverse=True,
|
213 |
+
)
|
214 |
+
sorted_general_strings = [x[0] for x in sorted_general_strings]
|
215 |
+
sorted_general_strings = (
|
216 |
+
", ".join(sorted_general_strings).replace("(", "\(").replace(")", "\)")
|
217 |
+
)
|
218 |
+
|
219 |
+
return sorted_general_strings, rating, character_res, general_res
|
220 |
+
|
221 |
+
|
222 |
+
def main():
|
223 |
+
args = parse_args()
|
224 |
+
|
225 |
+
predictor = Predictor()
|
226 |
+
|
227 |
+
dropdown_list = [
|
228 |
SWINV2_MODEL_DSV3_REPO,
|
229 |
CONV_MODEL_DSV3_REPO,
|
230 |
VIT_MODEL_DSV3_REPO,
|
|
|
235 |
CONV_MODEL_DSV2_REPO,
|
236 |
CONV2_MODEL_DSV2_REPO,
|
237 |
VIT_MODEL_DSV2_REPO,
|
238 |
+
]
|
239 |
|
240 |
with gr.Blocks(css= "style.css") as app:
|
241 |
# Dropdown untuk memilih model di luar tab dengan lebar kecil
|