Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,29 @@
|
|
1 |
-
import os
|
|
|
2 |
import gradio as gr
|
3 |
-
import huggingface_hub
|
|
|
|
|
|
|
4 |
from PIL import Image
|
5 |
from huggingface_hub import login
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# ------------------------------------------------------------------
|
10 |
# 模型配置
|
11 |
# ------------------------------------------------------------------
|
12 |
-
MODEL_REPO
|
13 |
-
MODEL_FILENAME
|
14 |
-
LABEL_FILENAME
|
15 |
|
16 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
17 |
if HF_TOKEN:
|
@@ -20,66 +32,106 @@ else:
|
|
20 |
print("⚠️ 未检测到 HF_TOKEN,私有模型可能下载失败")
|
21 |
|
22 |
# ------------------------------------------------------------------
|
23 |
-
# Tagger 类
|
24 |
# ------------------------------------------------------------------
|
25 |
class Tagger:
|
26 |
def __init__(self):
|
27 |
-
self.hf_token
|
|
|
|
|
|
|
|
|
28 |
self._load_model_and_labels()
|
29 |
|
30 |
def _load_model_and_labels(self):
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
tags_df = pd.read_csv(label_path)
|
39 |
-
self.tag_names = tags_df["name"].tolist()
|
40 |
-
self.categories = {
|
41 |
-
"rating": np.where(tags_df["category"] == 9)[0],
|
42 |
-
"general": np.where(tags_df["category"] == 0)[0],
|
43 |
-
"character": np.where(tags_df["category"] == 4)[0],
|
44 |
-
}
|
45 |
-
self.model = rt.InferenceSession(model_path)
|
46 |
-
self.input_size = self.model.get_inputs()[0].shape[1]
|
47 |
|
48 |
# ------------------------- preprocess -------------------------
|
49 |
def _preprocess(self, img: Image.Image) -> np.ndarray:
|
|
|
|
|
50 |
if img.mode != "RGB":
|
51 |
img = img.convert("RGB")
|
52 |
-
size
|
53 |
canvas = Image.new("RGB", (size, size), (255, 255, 255))
|
54 |
-
canvas.paste(img, ((size - img.width)//2, (size - img.height)//2))
|
55 |
if size != self.input_size:
|
56 |
canvas = canvas.resize((self.input_size, self.input_size), Image.BICUBIC)
|
57 |
return np.array(canvas)[:, :, ::-1].astype(np.float32) # to BGR
|
58 |
|
59 |
# --------------------------- predict --------------------------
|
60 |
-
def predict(self, img: Image.Image,
|
61 |
-
|
62 |
-
|
63 |
-
inp_name
|
64 |
-
outputs
|
65 |
|
66 |
res = {"ratings": {}, "general": {}, "characters": {}}
|
|
|
67 |
|
68 |
for idx in self.categories["rating"]:
|
69 |
-
|
|
|
|
|
|
|
70 |
|
71 |
for idx in self.categories["general"]:
|
72 |
if outputs[idx] > gen_th:
|
73 |
-
|
|
|
|
|
|
|
74 |
|
75 |
for idx in self.categories["character"]:
|
76 |
if outputs[idx] > char_th:
|
77 |
-
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
# ------------------------------------------------------------------
|
85 |
# Gradio UI
|
@@ -101,7 +153,7 @@ custom_css = """
|
|
101 |
padding: 2px 5px;
|
102 |
border-radius: 3px;
|
103 |
background-color: #fff;
|
104 |
-
|
105 |
}
|
106 |
.tag-item:hover {
|
107 |
background-color: #f0f0f0;
|
@@ -109,6 +161,7 @@ custom_css = """
|
|
109 |
.tag-en {
|
110 |
font-weight: bold;
|
111 |
color: #333;
|
|
|
112 |
}
|
113 |
.tag-zh {
|
114 |
color: #666;
|
@@ -118,107 +171,72 @@ custom_css = """
|
|
118 |
color: #999;
|
119 |
font-size: 0.9em;
|
120 |
}
|
121 |
-
.btn-container {
|
122 |
-
margin-top: 20px;
|
123 |
-
}
|
124 |
-
.copy-btn {
|
125 |
-
margin-top: 10px;
|
126 |
-
padding: 5px 10px;
|
127 |
-
background-color: #f0f0f0;
|
128 |
-
border: 1px solid #ddd;
|
129 |
-
border-radius: 4px;
|
130 |
-
cursor: pointer;
|
131 |
-
display: inline-flex;
|
132 |
-
align-items: center;
|
133 |
-
font-size: 0.9em;
|
134 |
-
}
|
135 |
-
.copy-btn:hover {
|
136 |
-
background-color: #e0e0e0;
|
137 |
-
}
|
138 |
-
.copy-icon {
|
139 |
-
margin-right: 5px;
|
140 |
-
width: 16px;
|
141 |
-
height: 16px;
|
142 |
-
}
|
143 |
-
.copied-message {
|
144 |
-
display: none;
|
145 |
-
color: #4CAF50;
|
146 |
-
margin-left: 10px;
|
147 |
-
font-size: 0.9em;
|
148 |
-
}
|
149 |
-
.note-text {
|
150 |
-
color: #ff6b6b;
|
151 |
-
font-size: 0.9em;
|
152 |
-
padding: 5px;
|
153 |
-
border-left: 3px solid #ff6b6b;
|
154 |
margin-top: 15px;
|
155 |
-
|
156 |
}
|
157 |
"""
|
158 |
|
159 |
-
|
160 |
-
function
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
// 为汇总区域的复制按钮设置功能
|
179 |
-
document.getElementById('copy-tags-btn').addEventListener('click', function() {
|
180 |
-
const tagsText = document.getElementById('summary-text').value;
|
181 |
-
navigator.clipboard.writeText(tagsText).then(() => {
|
182 |
-
const copiedMsg = document.getElementById('copied-message');
|
183 |
-
copiedMsg.style.display = 'inline';
|
184 |
setTimeout(() => {
|
185 |
-
|
186 |
-
},
|
187 |
-
});
|
|
|
|
|
|
|
188 |
});
|
189 |
}
|
190 |
-
|
191 |
-
// 在DOM加载完成或更新后调用设置函数
|
192 |
-
function onUiUpdate() {
|
193 |
-
setupCopyFunctions();
|
194 |
-
}
|
195 |
-
|
196 |
-
document.addEventListener('DOMContentLoaded', onUiUpdate);
|
197 |
"""
|
198 |
|
199 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=custom_css, js=
|
200 |
gr.Markdown("# 🖼️ AI 图像标签分析器")
|
201 |
-
gr.Markdown("
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
with gr.Row():
|
205 |
with gr.Column(scale=1):
|
206 |
-
img_in = gr.Image(type="pil", label="上传图片")
|
|
|
|
|
|
|
207 |
with gr.Accordion("⚙️ 高级设置", open=False):
|
208 |
-
gen_slider
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
gr.Markdown("### 汇总设置")
|
215 |
with gr.Row():
|
216 |
-
sum_general = gr.Checkbox(True, label="通用标签")
|
217 |
-
sum_char = gr.Checkbox(True, label="角色标签")
|
218 |
-
sum_rating = gr.Checkbox(False, label="评分标签")
|
219 |
-
sum_sep = gr.Dropdown(["逗号", "换行", "空格"], value="逗号", label="
|
|
|
220 |
|
221 |
-
btn = gr.Button("开始分析", variant="primary", elem_classes=["btn-container"])
|
222 |
processing_info = gr.Markdown("", visible=False)
|
223 |
|
224 |
with gr.Column(scale=2):
|
@@ -226,142 +244,247 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
226 |
with gr.TabItem("🏷️ 通用标签"):
|
227 |
out_general = gr.HTML(label="General Tags")
|
228 |
with gr.TabItem("👤 角色标签"):
|
|
|
229 |
out_char = gr.HTML(label="Character Tags")
|
230 |
with gr.TabItem("⭐ 评分标签"):
|
231 |
out_rating = gr.HTML(label="Rating Tags")
|
232 |
|
233 |
-
gr.Markdown("###
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
<button id="copy-tags-btn" class="copy-btn">
|
244 |
-
<svg class="copy-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
245 |
-
<rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
|
246 |
-
<path d="M5 15H4a2 2 0 01-2-2V4a2 2 0 012-2h9a2 2 0 012 2v1"></path>
|
247 |
-
</svg>
|
248 |
-
复制标签
|
249 |
-
</button>
|
250 |
-
<span id="copied-message" class="copied-message">已复制!</span>
|
251 |
-
</div>
|
252 |
-
""")
|
253 |
-
|
254 |
-
# ----------------- 处理回调 -----------------
|
255 |
-
def format_tags_html(tags_dict, translations, show_translation=True):
|
256 |
-
"""格式化标签为HTML格式,添加点击复制功能"""
|
257 |
if not tags_dict:
|
258 |
return "<p>暂无标签</p>"
|
259 |
|
260 |
html = '<div class="label-container">'
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
269 |
html += '</div>'
|
270 |
html += '</div>'
|
271 |
return html
|
272 |
|
273 |
-
def
|
274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
275 |
yield (
|
276 |
-
gr.update(interactive=False, value="处理中..."),
|
277 |
-
gr.update(visible=True, value="🔄
|
278 |
-
"",
|
|
|
|
|
|
|
|
|
279 |
)
|
280 |
|
281 |
try:
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
# 收集所有需要翻译的标签
|
286 |
-
all_tags = []
|
287 |
-
tag_categories = {
|
288 |
-
"general": list(res["general"].keys()),
|
289 |
-
"characters": list(res["characters"].keys()),
|
290 |
-
"ratings": list(res["ratings"].keys())
|
291 |
-
}
|
292 |
-
|
293 |
-
if show_zh:
|
294 |
-
for tags in tag_categories.values():
|
295 |
-
all_tags.extend(tags)
|
296 |
-
|
297 |
-
# 批量翻译
|
298 |
-
if all_tags:
|
299 |
-
translations = translate_texts(all_tags, src_lang="auto", tgt_lang="zh")
|
300 |
-
else:
|
301 |
-
translations = []
|
302 |
-
else:
|
303 |
-
translations = []
|
304 |
|
305 |
-
#
|
306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
offset = 0
|
308 |
-
for
|
309 |
-
|
310 |
-
|
311 |
-
|
|
|
|
|
312 |
else:
|
313 |
-
|
314 |
|
315 |
-
# 生成HTML输出
|
316 |
-
general_html = format_tags_html(res["general"], translations_dict["general"], show_zh)
|
317 |
-
char_html = format_tags_html(res["characters"], translations_dict["characters"], show_zh)
|
318 |
-
rating_html = format_tags_html(res["ratings"], translations_dict["ratings"], show_zh)
|
319 |
|
320 |
-
#
|
321 |
-
|
322 |
-
|
323 |
-
separator = separators[sep_type]
|
324 |
-
|
325 |
-
all_tags = []
|
326 |
-
if sum_gen and res["general"]:
|
327 |
-
all_tags.extend(list(res["general"].keys()))
|
328 |
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
|
|
|
|
336 |
|
337 |
-
# 完成处理,返回最终结果
|
338 |
yield (
|
339 |
-
gr.update(interactive=True, value="开始分析"),
|
340 |
-
gr.update(visible=
|
341 |
general_html,
|
342 |
char_html,
|
343 |
rating_html,
|
344 |
-
summary_text
|
|
|
|
|
|
|
345 |
)
|
346 |
|
347 |
except Exception as e:
|
348 |
-
|
|
|
|
|
349 |
yield (
|
350 |
-
gr.update(interactive=True, value="开始分析"),
|
351 |
gr.update(visible=True, value=f"❌ 处理失败: {str(e)}"),
|
352 |
-
"", "", "",
|
|
|
|
|
353 |
)
|
354 |
|
355 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
356 |
btn.click(
|
357 |
-
|
358 |
-
inputs=[
|
359 |
-
|
360 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
361 |
)
|
362 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
363 |
# ------------------------------------------------------------------
|
364 |
# 启动
|
365 |
# ------------------------------------------------------------------
|
366 |
if __name__ == "__main__":
|
|
|
|
|
367 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
import gradio as gr
|
4 |
+
import huggingface_hub
|
5 |
+
import numpy as np
|
6 |
+
import onnxruntime as rt
|
7 |
+
import pandas as pd
|
8 |
from PIL import Image
|
9 |
from huggingface_hub import login
|
10 |
|
11 |
+
# 假设 translator.py 中的 translate_texts 函数已正确定义
|
12 |
+
# from translator import translate_texts
|
13 |
+
# Mock translator for a standalone example if translator.py is not available
|
14 |
+
def translate_texts(texts, src_lang="auto", tgt_lang="zh"):
|
15 |
+
print(f"Mock translating: {texts} from {src_lang} to {tgt_lang}")
|
16 |
+
if not texts:
|
17 |
+
return []
|
18 |
+
# 返回一个简单的模拟翻译结果,实际使用时请确保 translator.py 可用且功能正确
|
19 |
+
return [f"{text}_译" for text in texts]
|
20 |
|
21 |
# ------------------------------------------------------------------
|
22 |
# 模型配置
|
23 |
# ------------------------------------------------------------------
|
24 |
+
MODEL_REPO = "SmilingWolf/wd-eva02-large-tagger-v3"
|
25 |
+
MODEL_FILENAME = "model.onnx"
|
26 |
+
LABEL_FILENAME = "selected_tags.csv"
|
27 |
|
28 |
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
29 |
if HF_TOKEN:
|
|
|
32 |
print("⚠️ 未检测到 HF_TOKEN,私有模型可能下载失败")
|
33 |
|
34 |
# ------------------------------------------------------------------
|
35 |
+
# Tagger 类 (全局实例化)
|
36 |
# ------------------------------------------------------------------
|
37 |
class Tagger:
|
38 |
def __init__(self):
|
39 |
+
self.hf_token = HF_TOKEN
|
40 |
+
self.tag_names = []
|
41 |
+
self.categories = {}
|
42 |
+
self.model = None
|
43 |
+
self.input_size = 0
|
44 |
self._load_model_and_labels()
|
45 |
|
46 |
def _load_model_and_labels(self):
|
47 |
+
try:
|
48 |
+
label_path = huggingface_hub.hf_hub_download(
|
49 |
+
MODEL_REPO, LABEL_FILENAME, token=self.hf_token, resume_download=True
|
50 |
+
)
|
51 |
+
model_path = huggingface_hub.hf_hub_download(
|
52 |
+
MODEL_REPO, MODEL_FILENAME, token=self.hf_token, resume_download=True
|
53 |
+
)
|
54 |
+
|
55 |
+
tags_df = pd.read_csv(label_path)
|
56 |
+
self.tag_names = tags_df["name"].tolist()
|
57 |
+
self.categories = {
|
58 |
+
"rating": np.where(tags_df["category"] == 9)[0],
|
59 |
+
"general": np.where(tags_df["category"] == 0)[0],
|
60 |
+
"character": np.where(tags_df["category"] == 4)[0],
|
61 |
+
}
|
62 |
+
self.model = rt.InferenceSession(model_path)
|
63 |
+
self.input_size = self.model.get_inputs()[0].shape[1]
|
64 |
+
print("✅ 模型和标签加载成功")
|
65 |
+
except Exception as e:
|
66 |
+
print(f"❌ 模型或标签加载失败: {e}")
|
67 |
+
# 可以选择抛出异常或设置一个标志,让应用知道模型未就绪
|
68 |
+
raise RuntimeError(f"模型初始化失败: {e}")
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
# ------------------------- preprocess -------------------------
|
72 |
def _preprocess(self, img: Image.Image) -> np.ndarray:
|
73 |
+
if img is None:
|
74 |
+
raise ValueError("输入图像不能为空")
|
75 |
if img.mode != "RGB":
|
76 |
img = img.convert("RGB")
|
77 |
+
size = max(img.size)
|
78 |
canvas = Image.new("RGB", (size, size), (255, 255, 255))
|
79 |
+
canvas.paste(img, ((size - img.width) // 2, (size - img.height) // 2))
|
80 |
if size != self.input_size:
|
81 |
canvas = canvas.resize((self.input_size, self.input_size), Image.BICUBIC)
|
82 |
return np.array(canvas)[:, :, ::-1].astype(np.float32) # to BGR
|
83 |
|
84 |
# --------------------------- predict --------------------------
|
85 |
+
def predict(self, img: Image.Image, gen_th: float = 0.35, char_th: float = 0.85):
|
86 |
+
if self.model is None:
|
87 |
+
raise RuntimeError("模型未成功加载,无法进行预测。")
|
88 |
+
inp_name = self.model.get_inputs()[0].name
|
89 |
+
outputs = self.model.run(None, {inp_name: self._preprocess(img)[None, ...]})[0][0]
|
90 |
|
91 |
res = {"ratings": {}, "general": {}, "characters": {}}
|
92 |
+
tag_categories_for_translation = {"ratings": [], "general": [], "characters": []}
|
93 |
|
94 |
for idx in self.categories["rating"]:
|
95 |
+
tag_name = self.tag_names[idx].replace("_", " ")
|
96 |
+
res["ratings"][tag_name] = float(outputs[idx])
|
97 |
+
tag_categories_for_translation["ratings"].append(tag_name)
|
98 |
+
|
99 |
|
100 |
for idx in self.categories["general"]:
|
101 |
if outputs[idx] > gen_th:
|
102 |
+
tag_name = self.tag_names[idx].replace("_", " ")
|
103 |
+
res["general"][tag_name] = float(outputs[idx])
|
104 |
+
tag_categories_for_translation["general"].append(tag_name)
|
105 |
+
|
106 |
|
107 |
for idx in self.categories["character"]:
|
108 |
if outputs[idx] > char_th:
|
109 |
+
tag_name = self.tag_names[idx].replace("_", " ")
|
110 |
+
res["characters"][tag_name] = float(outputs[idx])
|
111 |
+
tag_categories_for_translation["character"].append(tag_name)
|
112 |
|
113 |
+
|
114 |
+
# Sort general tags by score
|
115 |
+
res["general"] = dict(sorted(res["general"].items(), key=lambda kv: kv[1], reverse=True))
|
116 |
+
# Sort character tags by score (optional, but good for consistency)
|
117 |
+
res["characters"] = dict(sorted(res["characters"].items(), key=lambda kv: kv[1], reverse=True))
|
118 |
+
# Ratings are usually fixed, but sorting doesn't hurt if order matters for display
|
119 |
+
res["ratings"] = dict(sorted(res["ratings"].items(), key=lambda kv: kv[1], reverse=True))
|
120 |
+
|
121 |
+
|
122 |
+
# Re-populate tag_categories_for_translation based on sorted and filtered results
|
123 |
+
tag_categories_for_translation["general"] = list(res["general"].keys())
|
124 |
+
tag_categories_for_translation["characters"] = list(res["characters"].keys())
|
125 |
+
tag_categories_for_translation["ratings"] = list(res["ratings"].keys()) # Order from sorted res
|
126 |
+
|
127 |
+
return res, tag_categories_for_translation
|
128 |
+
|
129 |
+
# 全局 Tagger 实例
|
130 |
+
try:
|
131 |
+
tagger_instance = Tagger()
|
132 |
+
except RuntimeError as e:
|
133 |
+
print(f"应用启动时Tagger初始化失败: {e}")
|
134 |
+
tagger_instance = None # 允许应用启动,但在处理时会失败
|
135 |
|
136 |
# ------------------------------------------------------------------
|
137 |
# Gradio UI
|
|
|
153 |
padding: 2px 5px;
|
154 |
border-radius: 3px;
|
155 |
background-color: #fff;
|
156 |
+
transition: background-color 0.2s;
|
157 |
}
|
158 |
.tag-item:hover {
|
159 |
background-color: #f0f0f0;
|
|
|
161 |
.tag-en {
|
162 |
font-weight: bold;
|
163 |
color: #333;
|
164 |
+
cursor: pointer; /* Indicates clickable */
|
165 |
}
|
166 |
.tag-zh {
|
167 |
color: #666;
|
|
|
171 |
color: #999;
|
172 |
font-size: 0.9em;
|
173 |
}
|
174 |
+
.btn-analyze-container { /* Custom class for analyze button container */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
margin-top: 15px;
|
176 |
+
margin-bottom: 15px;
|
177 |
}
|
178 |
"""
|
179 |
|
180 |
+
_js_functions = """
|
181 |
+
function copyToClipboard(text) {
|
182 |
+
navigator.clipboard.writeText(text).then(() => {
|
183 |
+
// console.log('Tag copied to clipboard: ' + text);
|
184 |
+
const feedback = document.createElement('div');
|
185 |
+
feedback.textContent = '已复制: ' + text.substring(0,30) + (text.length > 30 ? '...' : ''); // Show part of copied text
|
186 |
+
feedback.style.position = 'fixed';
|
187 |
+
feedback.style.bottom = '20px';
|
188 |
+
feedback.style.left = '50%';
|
189 |
+
feedback.style.transform = 'translateX(-50%)';
|
190 |
+
feedback.style.backgroundColor = '#4CAF50';
|
191 |
+
feedback.style.color = 'white';
|
192 |
+
feedback.style.padding = '10px 20px';
|
193 |
+
feedback.style.borderRadius = '5px';
|
194 |
+
feedback.style.zIndex = '10000'; // Ensure it's on top
|
195 |
+
feedback.style.transition = 'opacity 0.5s ease-out';
|
196 |
+
document.body.appendChild(feedback);
|
197 |
+
setTimeout(() => {
|
198 |
+
feedback.style.opacity = '0';
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
setTimeout(() => {
|
200 |
+
document.body.removeChild(feedback);
|
201 |
+
}, 500);
|
202 |
+
}, 1500);
|
203 |
+
}).catch(err => {
|
204 |
+
console.error('Failed to copy tag: ', err);
|
205 |
+
alert('复制失败: ' + err); // Fallback for browsers that might block it
|
206 |
});
|
207 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
"""
|
209 |
|
210 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=custom_css, js=_js_functions) as demo:
|
211 |
gr.Markdown("# 🖼️ AI 图像标签分析器")
|
212 |
+
gr.Markdown("上传图片自动识别标签,支持中英文显示和一键复制。")
|
213 |
+
|
214 |
+
# State variables to store results for re-processing summary without re-running model
|
215 |
+
state_res = gr.State({})
|
216 |
+
state_translations_dict = gr.State({})
|
217 |
+
state_tag_categories_for_translation = gr.State({})
|
218 |
+
|
219 |
|
220 |
with gr.Row():
|
221 |
with gr.Column(scale=1):
|
222 |
+
img_in = gr.Image(type="pil", label="上传图片", height=300)
|
223 |
+
|
224 |
+
btn = gr.Button("🚀 开始分析", variant="primary", elem_classes=["btn-analyze-container"])
|
225 |
+
|
226 |
with gr.Accordion("⚙️ 高级设置", open=False):
|
227 |
+
gen_slider = gr.Slider(0, 1, value=0.35, step=0.01, label="通用标签阈值", info="越高 → 标签更少更准")
|
228 |
+
char_slider = gr.Slider(0, 1, value=0.85, step=0.01, label="角色标签阈值", info="推荐保持较高阈值")
|
229 |
+
show_tag_scores = gr.Checkbox(True, label="在列表中显示标签置信度")
|
230 |
+
|
231 |
+
with gr.Accordion("📊 标签汇总设置", open=True):
|
232 |
+
gr.Markdown("选择要包含在下方汇总文本框中的标签类别:")
|
|
|
233 |
with gr.Row():
|
234 |
+
sum_general = gr.Checkbox(True, label="通用标签", min_width=50)
|
235 |
+
sum_char = gr.Checkbox(True, label="角色标签", min_width=50)
|
236 |
+
sum_rating = gr.Checkbox(False, label="评分标签", min_width=50)
|
237 |
+
sum_sep = gr.Dropdown(["逗号", "换行", "空格"], value="逗号", label="标签之间的分隔符")
|
238 |
+
sum_show_zh = gr.Checkbox(False, label="在汇总中显示中文翻译")
|
239 |
|
|
|
240 |
processing_info = gr.Markdown("", visible=False)
|
241 |
|
242 |
with gr.Column(scale=2):
|
|
|
244 |
with gr.TabItem("🏷️ 通用标签"):
|
245 |
out_general = gr.HTML(label="General Tags")
|
246 |
with gr.TabItem("👤 角色标签"):
|
247 |
+
gr.Markdown("<p style='color:gray; font-size:small;'>提示:角色标签推测基于截至2024年2月的数据。</p>")
|
248 |
out_char = gr.HTML(label="Character Tags")
|
249 |
with gr.TabItem("⭐ 评分标签"):
|
250 |
out_rating = gr.HTML(label="Rating Tags")
|
251 |
|
252 |
+
gr.Markdown("### 标签汇总结果")
|
253 |
+
out_summary = gr.Textbox(
|
254 |
+
label="标签汇总(仅英文,可通过上方设置添加中文)",
|
255 |
+
placeholder="分析完成后,此处将显示汇总的英文标签...",
|
256 |
+
lines=5,
|
257 |
+
show_copy_button=True
|
258 |
+
)
|
259 |
+
|
260 |
+
# ----------------- 辅助函数 -----------------
|
261 |
+
def format_tags_html(tags_dict, translations_list, category_name, show_scores=True, show_translation_in_list=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
if not tags_dict:
|
263 |
return "<p>暂无标签</p>"
|
264 |
|
265 |
html = '<div class="label-container">'
|
266 |
+
# Ensure translations_list is a list and matches length, or provide empty strings if not.
|
267 |
+
# This assumes translations_list corresponds to the order in tags_dict.keys()
|
268 |
+
# For dictionaries, keys() order is insertion order from Python 3.7+
|
269 |
+
|
270 |
+
if not isinstance(translations_list, list): # defensive check
|
271 |
+
translations_list = []
|
272 |
+
|
273 |
+
tag_keys = list(tags_dict.keys())
|
274 |
+
|
275 |
+
for i, tag in enumerate(tag_keys):
|
276 |
+
score = tags_dict[tag]
|
277 |
+
escaped_tag = tag.replace("'", "\\'") # Escape for JS
|
278 |
+
|
279 |
+
html += '<div class="tag-item">'
|
280 |
+
tag_display_html = f'<span class="tag-en" onclick="copyToClipboard(\'{escaped_tag}\')">{tag}</span>'
|
281 |
+
|
282 |
+
if show_translation_in_list and i < len(translations_list) and translations_list[i]:
|
283 |
+
tag_display_html += f'<span class="tag-zh">({translations_list[i]})</span>'
|
284 |
+
|
285 |
+
html += f'<div>{tag_display_html}</div>'
|
286 |
+
if show_scores:
|
287 |
+
html += f'<span class="tag-score">{score:.3f}</span>'
|
288 |
html += '</div>'
|
289 |
html += '</div>'
|
290 |
return html
|
291 |
|
292 |
+
def generate_summary_text_content(
|
293 |
+
current_res, current_translations_dict,
|
294 |
+
s_gen, s_char, s_rat, s_sep_type, s_show_zh
|
295 |
+
):
|
296 |
+
if not current_res:
|
297 |
+
return "请先分析图像或选择要汇总的标签类别。"
|
298 |
+
|
299 |
+
summary_parts = []
|
300 |
+
separators = {"逗号": ", ", "换行": "\n", "空格": " "}
|
301 |
+
separator = separators.get(s_sep_type, ", ")
|
302 |
+
|
303 |
+
categories_to_summarize = []
|
304 |
+
if s_gen: categories_to_summarize.append("general")
|
305 |
+
if s_char: categories_to_summarize.append("characters")
|
306 |
+
if s_rat: categories_to_summarize.append("ratings")
|
307 |
+
|
308 |
+
if not categories_to_summarize:
|
309 |
+
return "请至少选择一个标签类别进行汇总。"
|
310 |
+
|
311 |
+
for cat_key in categories_to_summarize:
|
312 |
+
if current_res.get(cat_key):
|
313 |
+
tags_to_join = []
|
314 |
+
cat_tags_en = list(current_res[cat_key].keys())
|
315 |
+
cat_translations = current_translations_dict.get(cat_key, [])
|
316 |
+
|
317 |
+
for i, en_tag in enumerate(cat_tags_en):
|
318 |
+
if s_show_zh and i < len(cat_translations) and cat_translations[i]:
|
319 |
+
tags_to_join.append(f"{en_tag}({cat_translations[i]})")
|
320 |
+
else:
|
321 |
+
tags_to_join.append(en_tag)
|
322 |
+
if tags_to_join: # only add if there are tags for this category
|
323 |
+
summary_parts.append(separator.join(tags_to_join))
|
324 |
+
|
325 |
+
# Join parts with double newline for readability if multiple categories present and separator is not newline
|
326 |
+
joiner = "\n\n" if separator != "\n" and len(summary_parts) > 1 else separator if separator == "\n" else " "
|
327 |
+
|
328 |
+
final_summary = joiner.join(summary_parts)
|
329 |
+
return final_summary if final_summary else "选定的类别中没有找到标签。"
|
330 |
+
|
331 |
+
|
332 |
+
# ----------------- 主要处理回调 -----------------
|
333 |
+
def process_image_and_generate_outputs(
|
334 |
+
img, g_th, c_th, s_scores, # Main inputs
|
335 |
+
s_gen, s_char, s_rat, s_sep, s_zh_in_sum # Summary controls from UI
|
336 |
+
):
|
337 |
+
if img is None:
|
338 |
+
yield (
|
339 |
+
gr.update(interactive=True, value="🚀 开始分析"),
|
340 |
+
gr.update(visible=True, value="❌ 请先上传图片。"),
|
341 |
+
"", "", "", "", # HTML outputs
|
342 |
+
gr.update(placeholder="请先上传图片并开始分析..."), # Summary text
|
343 |
+
{}, {}, {} # States
|
344 |
+
)
|
345 |
+
return
|
346 |
+
|
347 |
+
if tagger_instance is None:
|
348 |
+
yield (
|
349 |
+
gr.update(interactive=True, value="🚀 开始分析"),
|
350 |
+
gr.update(visible=True, value="❌ 分析器未成功初始化,请检查控制台错误。"),
|
351 |
+
"", "", "", "",
|
352 |
+
gr.update(placeholder="分析器初始化失败..."),
|
353 |
+
{}, {}, {}
|
354 |
+
)
|
355 |
+
return
|
356 |
+
|
357 |
yield (
|
358 |
+
gr.update(interactive=False, value="🔄 处理中..."),
|
359 |
+
gr.update(visible=True, value="🔄 正在分析图像,请稍候..."),
|
360 |
+
gr.HTML(value="<p>分析中...</p>"), # General
|
361 |
+
gr.HTML(value="<p>分析中...</p>"), # Character
|
362 |
+
gr.HTML(value="<p>分析中...</p>"), # Rating
|
363 |
+
gr.update(value="分析中,请稍候..."), # Summary
|
364 |
+
{}, {}, {} # Clear states initially
|
365 |
)
|
366 |
|
367 |
try:
|
368 |
+
# 1. Predict tags
|
369 |
+
# The predict method now returns res and tag_categories_for_translation
|
370 |
+
res, tag_categories_original_order = tagger_instance.predict(img, g_th, c_th)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
371 |
|
372 |
+
# 2. Translate all tags that will be displayed in lists
|
373 |
+
# The `show_zh_in_list_checkbox` now controls if we translate for lists.
|
374 |
+
# For summary, translation is controlled by `s_zh_in_sum`.
|
375 |
+
# We should always translate all potential tags to have them ready.
|
376 |
+
|
377 |
+
all_tags_to_translate = []
|
378 |
+
for cat_key in ["general", "characters", "ratings"]:
|
379 |
+
all_tags_to_translate.extend(tag_categories_original_order.get(cat_key, []))
|
380 |
+
|
381 |
+
all_translations_flat = []
|
382 |
+
if all_tags_to_translate: # Only call translate if there's something to translate
|
383 |
+
all_translations_flat = translate_texts(all_tags_to_translate, src_lang="auto", tgt_lang="zh")
|
384 |
+
|
385 |
+
current_translations_dict = {}
|
386 |
offset = 0
|
387 |
+
for cat_key in ["general", "characters", "ratings"]:
|
388 |
+
cat_original_tags = tag_categories_original_order.get(cat_key, [])
|
389 |
+
num_tags_in_cat = len(cat_original_tags)
|
390 |
+
if num_tags_in_cat > 0:
|
391 |
+
current_translations_dict[cat_key] = all_translations_flat[offset : offset + num_tags_in_cat]
|
392 |
+
offset += num_tags_in_cat
|
393 |
else:
|
394 |
+
current_translations_dict[cat_key] = []
|
395 |
|
|
|
|
|
|
|
|
|
396 |
|
397 |
+
# 3. Format HTML outputs (always show English, translations if available and `show_zh_in_list` is true)
|
398 |
+
# Let's assume `show_zh_in_list` is a new checkbox or fixed to true for list display.
|
399 |
+
# For simplicity, let's assume list translations are always prepared if `current_translations_dict` has them.
|
|
|
|
|
|
|
|
|
|
|
400 |
|
401 |
+
general_html = format_tags_html(res.get("general", {}), current_translations_dict.get("general", []), "general", s_scores, True)
|
402 |
+
char_html = format_tags_html(res.get("characters", {}), current_translations_dict.get("characters", []), "characters", s_scores, True)
|
403 |
+
rating_html = format_tags_html(res.get("ratings", {}), current_translations_dict.get("ratings", []), "ratings", s_scores, True)
|
404 |
+
|
405 |
+
# 4. Generate initial summary text (based on current summary settings from UI)
|
406 |
+
summary_text = generate_summary_text_content(
|
407 |
+
res, current_translations_dict,
|
408 |
+
s_gen, s_char, s_rat, s_sep, s_zh_in_sum # Use summary specific checkbox for zh
|
409 |
+
)
|
410 |
|
|
|
411 |
yield (
|
412 |
+
gr.update(interactive=True, value="🚀 开始分析"),
|
413 |
+
gr.update(visible=True, value="✅ 分析完成!"), # Success message
|
414 |
general_html,
|
415 |
char_html,
|
416 |
rating_html,
|
417 |
+
gr.update(value=summary_text),
|
418 |
+
res, # Store full results in state
|
419 |
+
current_translations_dict, # Store translations in state
|
420 |
+
tag_categories_original_order # Store original order for consistency if needed
|
421 |
)
|
422 |
|
423 |
except Exception as e:
|
424 |
+
import traceback
|
425 |
+
tb_str = traceback.format_exc()
|
426 |
+
print(f"处理时发生错误: {e}\n{tb_str}")
|
427 |
yield (
|
428 |
+
gr.update(interactive=True, value="🚀 开始分析"),
|
429 |
gr.update(visible=True, value=f"❌ 处理失败: {str(e)}"),
|
430 |
+
"<p>处理出错</p>", "<p>处理出错</p>", "<p>处理出错</p>", # Clear HTML
|
431 |
+
gr.update(value=f"错误: {str(e)}", placeholder="分析失败..."), # Update summary
|
432 |
+
{}, {}, {} # Clear states
|
433 |
)
|
434 |
|
435 |
+
# ----------------- 更新汇总文本的回调 -----------------
|
436 |
+
def update_summary_display(
|
437 |
+
s_gen, s_char, s_rat, s_sep, s_zh_in_sum, # UI controls for summary
|
438 |
+
current_res_from_state, current_translations_from_state # Data from state
|
439 |
+
):
|
440 |
+
if not current_res_from_state: # No analysis done yet
|
441 |
+
return gr.update(placeholder="请先完成一次图像分析以生成汇总。", value="")
|
442 |
+
|
443 |
+
new_summary_text = generate_summary_text_content(
|
444 |
+
current_res_from_state, current_translations_from_state,
|
445 |
+
s_gen, s_char, s_rat, s_sep, s_zh_in_sum
|
446 |
+
)
|
447 |
+
return gr.update(value=new_summary_text)
|
448 |
+
|
449 |
+
# ----------------- 绑定事件 -----------------
|
450 |
btn.click(
|
451 |
+
process_image_and_generate_outputs,
|
452 |
+
inputs=[
|
453 |
+
img_in, gen_slider, char_slider, show_tag_scores,
|
454 |
+
sum_general, sum_char, sum_rating, sum_sep, sum_show_zh # Pass summary controls directly
|
455 |
+
],
|
456 |
+
outputs=[
|
457 |
+
btn, processing_info,
|
458 |
+
out_general, out_char, out_rating,
|
459 |
+
out_summary,
|
460 |
+
state_res, state_translations_dict, state_tag_categories_for_translation
|
461 |
+
],
|
462 |
+
# show_progress="full" # Gradio's built-in progress
|
463 |
)
|
464 |
|
465 |
+
# Bind summary update controls to the update_summary_display function
|
466 |
+
summary_controls = [sum_general, sum_char, sum_rating, sum_sep, sum_show_zh]
|
467 |
+
for ctrl in summary_controls:
|
468 |
+
ctrl.change(
|
469 |
+
fn=update_summary_display,
|
470 |
+
inputs=summary_controls + [state_res, state_translations_dict], # All controls + state data
|
471 |
+
outputs=[out_summary],
|
472 |
+
# show_progress=False # Typically fast, no need for progress indicator
|
473 |
+
)
|
474 |
+
|
475 |
+
# If tag score display in lists is changed, re-render HTMLs
|
476 |
+
# This requires storing the raw data or re-processing parts of it.
|
477 |
+
# For simplicity, we can make the list HTML generation also dependent on state if needed,
|
478 |
+
# or re-trigger a lighter version of 'process' that only updates HTML.
|
479 |
+
# Current implementation: score display is set at 'analyze' time.
|
480 |
+
# To make 'show_tag_scores' dynamic for lists *after* analysis without re-analyzing:
|
481 |
+
# We would need a new callback that re-runs `format_tags_html` for each category
|
482 |
+
# using data from `state_res` and `state_translations_dict`.
|
483 |
+
|
484 |
# ------------------------------------------------------------------
|
485 |
# 启动
|
486 |
# ------------------------------------------------------------------
|
487 |
if __name__ == "__main__":
|
488 |
+
if tagger_instance is None:
|
489 |
+
print("CRITICAL: Tagger 未能初始化,应用功能将受限。请检查之前的错误信息。")
|
490 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|