Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -101,6 +101,10 @@ custom_css = """
|
|
101 |
padding: 2px 5px;
|
102 |
border-radius: 3px;
|
103 |
background-color: #fff;
|
|
|
|
|
|
|
|
|
104 |
}
|
105 |
.tag-en {
|
106 |
font-weight: bold;
|
@@ -117,16 +121,90 @@ custom_css = """
|
|
117 |
.btn-container {
|
118 |
margin-top: 20px;
|
119 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
"""
|
121 |
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
gr.Markdown("# 🖼️ AI 图像标签分析器")
|
124 |
gr.Markdown("上传图片自动识别标签,并可一键翻译成中文")
|
|
|
125 |
|
126 |
with gr.Row():
|
127 |
with gr.Column(scale=1):
|
128 |
img_in = gr.Image(type="pil", label="上传图片")
|
129 |
-
with gr.Accordion("⚙️ 高级设置", open=
|
130 |
gen_slider = gr.Slider(0, 1, 0.35,
|
131 |
label="通用标签阈值", info="越高→标签更少更准")
|
132 |
char_slider = gr.Slider(0, 1, 0.85,
|
@@ -153,26 +231,42 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
153 |
out_rating = gr.HTML(label="Rating Tags")
|
154 |
|
155 |
gr.Markdown("### 标签汇总")
|
156 |
-
|
157 |
-
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
# ----------------- 处理回调 -----------------
|
161 |
def format_tags_html(tags_dict, translations, show_translation=True):
|
162 |
-
"""格式化标签为HTML
|
163 |
if not tags_dict:
|
164 |
return "<p>暂无标签</p>"
|
165 |
|
166 |
html = '<div class="label-container">'
|
167 |
for i, (tag, score) in enumerate(tags_dict.items()):
|
168 |
-
|
169 |
-
|
|
|
170 |
if show_translation and i < len(translations):
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
html += tag_html
|
176 |
html += '</div>'
|
177 |
return html
|
178 |
|
@@ -223,33 +317,22 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
223 |
char_html = format_tags_html(res["characters"], translations_dict["characters"], show_zh)
|
224 |
rating_html = format_tags_html(res["ratings"], translations_dict["ratings"], show_zh)
|
225 |
|
226 |
-
# 生成汇总文本
|
227 |
summary_parts = []
|
228 |
separators = {"逗号": ", ", "换行": "\n", "空格": " "}
|
229 |
separator = separators[sep_type]
|
230 |
|
|
|
231 |
if sum_gen and res["general"]:
|
232 |
-
|
233 |
-
gen_tags = [f"{en}({zh})" for en, zh in zip(res["general"].keys(), translations_dict["general"])]
|
234 |
-
else:
|
235 |
-
gen_tags = list(res["general"].keys())
|
236 |
-
summary_parts.append("通用标签: " + separator.join(gen_tags))
|
237 |
|
238 |
if sum_char and res["characters"]:
|
239 |
-
|
240 |
-
char_tags = [f"{en}({zh})" for en, zh in zip(res["characters"].keys(), translations_dict["characters"])]
|
241 |
-
else:
|
242 |
-
char_tags = list(res["characters"].keys())
|
243 |
-
summary_parts.append("角色标签: " + separator.join(char_tags))
|
244 |
|
245 |
if sum_rat and res["ratings"]:
|
246 |
-
|
247 |
-
rat_tags = [f"{en}({zh})" for en, zh in zip(res["ratings"].keys(), translations_dict["ratings"])]
|
248 |
-
else:
|
249 |
-
rat_tags = list(res["ratings"].keys())
|
250 |
-
summary_parts.append("评分标签: " + separator.join(rat_tags))
|
251 |
|
252 |
-
summary_text =
|
253 |
|
254 |
# 完成处理,返回最终结果
|
255 |
yield (
|
|
|
101 |
padding: 2px 5px;
|
102 |
border-radius: 3px;
|
103 |
background-color: #fff;
|
104 |
+
cursor: pointer;
|
105 |
+
}
|
106 |
+
.tag-item:hover {
|
107 |
+
background-color: #f0f0f0;
|
108 |
}
|
109 |
.tag-en {
|
110 |
font-weight: bold;
|
|
|
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 |
+
background-color: #fff5f5;
|
156 |
+
}
|
157 |
"""
|
158 |
|
159 |
+
js_code = """
|
160 |
+
function setupCopyFunctions() {
|
161 |
+
// 为标签项设置点击复制
|
162 |
+
document.querySelectorAll('.tag-item').forEach(item => {
|
163 |
+
item.addEventListener('click', function() {
|
164 |
+
const tagText = this.querySelector('.tag-en').textContent;
|
165 |
+
navigator.clipboard.writeText(tagText).then(() => {
|
166 |
+
// 显示临时复制成功提示
|
167 |
+
const msg = document.createElement('span');
|
168 |
+
msg.textContent = '已复制!';
|
169 |
+
msg.style.color = '#4CAF50';
|
170 |
+
msg.style.marginLeft = '5px';
|
171 |
+
msg.style.fontSize = '0.8em';
|
172 |
+
this.appendChild(msg);
|
173 |
+
setTimeout(() => msg.remove(), 1000);
|
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 |
+
copiedMsg.style.display = 'none';
|
186 |
+
}, 2000);
|
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=js_code) as demo:
|
200 |
gr.Markdown("# 🖼️ AI 图像标签分析器")
|
201 |
gr.Markdown("上传图片自动识别标签,并可一键翻译成中文")
|
202 |
+
gr.Markdown("<div class='note-text'>⚠️ 注意:角色识别仅支持推测2024年2月以前的角色</div>", elem_id="character-notice")
|
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 = gr.Slider(0, 1, 0.35,
|
209 |
label="通用标签阈值", info="越高→标签更少更准")
|
210 |
char_slider = gr.Slider(0, 1, 0.85,
|
|
|
231 |
out_rating = gr.HTML(label="Rating Tags")
|
232 |
|
233 |
gr.Markdown("### 标签汇总")
|
234 |
+
with gr.Row():
|
235 |
+
out_summary = gr.Textbox(label="标签汇总",
|
236 |
+
placeholder="选择需要汇总的标签类别...",
|
237 |
+
lines=3,
|
238 |
+
elem_id="summary-text")
|
239 |
+
|
240 |
+
# 添加复制按钮的HTML
|
241 |
+
copy_btn_html = gr.HTML("""
|
242 |
+
<div style="display: flex; align-items: center;">
|
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 |
for i, (tag, score) in enumerate(tags_dict.items()):
|
262 |
+
# 添加可点击复制的标签项
|
263 |
+
html += f'<div class="tag-item" title="点击复制标签">'
|
264 |
+
html += f'<div><span class="tag-en">{tag}</span>'
|
265 |
if show_translation and i < len(translations):
|
266 |
+
html += f'<span class="tag-zh">({translations[i]})</span>'
|
267 |
+
html += '</div>'
|
268 |
+
html += f'<span class="tag-score">{score:.3f}</span>'
|
269 |
+
html += '</div>'
|
|
|
270 |
html += '</div>'
|
271 |
return html
|
272 |
|
|
|
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 |
summary_parts = []
|
322 |
separators = {"逗号": ", ", "换行": "\n", "空格": " "}
|
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 |
if sum_char and res["characters"]:
|
330 |
+
all_tags.extend(list(res["characters"].keys()))
|
|
|
|
|
|
|
|
|
331 |
|
332 |
if sum_rat and res["ratings"]:
|
333 |
+
all_tags.extend(list(res["ratings"].keys()))
|
|
|
|
|
|
|
|
|
334 |
|
335 |
+
summary_text = separator.join(all_tags) if all_tags else "请选择要汇总的标签类别"
|
336 |
|
337 |
# 完成处理,返回最终结果
|
338 |
yield (
|