Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -169,18 +169,18 @@ function copyToClipboard(text) {
|
|
169 |
// --- 调试信息 ---
|
170 |
console.log('copyToClipboard function was called.');
|
171 |
console.log('Received text:', text);
|
172 |
-
// console.trace();
|
173 |
|
174 |
// --- 保护性检查 ---
|
175 |
// 如果 text 未定义或为 null,则不执行后续操作,并打印警告
|
176 |
if (typeof text === 'undefined' || text === null) {
|
177 |
console.warn('copyToClipboard was called with undefined or null text. Aborting this specific copy operation.');
|
178 |
-
//
|
179 |
return;
|
180 |
}
|
181 |
|
182 |
navigator.clipboard.writeText(text).then(() => {
|
183 |
-
// console.log('Tag copied to clipboard: ' + text);
|
184 |
const feedback = document.createElement('div');
|
185 |
|
186 |
// 确保 text 是字符串类型,再进行 substring 操作
|
@@ -202,21 +202,22 @@ function copyToClipboard(text) {
|
|
202 |
setTimeout(() => {
|
203 |
feedback.style.opacity = '0';
|
204 |
setTimeout(() => {
|
205 |
-
if (document.body.contains(feedback)) {
|
206 |
document.body.removeChild(feedback);
|
207 |
}
|
208 |
}, 500);
|
209 |
}, 1500);
|
210 |
}).catch(err => {
|
211 |
console.error('Failed to copy tag. Error:', err, 'Attempted to copy text:', text);
|
|
|
212 |
// alert('复制失败: ' + err);
|
213 |
const errorFeedback = document.createElement('div');
|
214 |
-
errorFeedback.textContent = '复制操作失败!'; //
|
215 |
errorFeedback.style.position = 'fixed';
|
216 |
errorFeedback.style.bottom = '20px';
|
217 |
errorFeedback.style.left = '50%';
|
218 |
errorFeedback.style.transform = 'translateX(-50%)';
|
219 |
-
errorFeedback.style.backgroundColor = '#D32F2F'; //
|
220 |
errorFeedback.style.color = 'white';
|
221 |
errorFeedback.style.padding = '10px 20px';
|
222 |
errorFeedback.style.borderRadius = '5px';
|
@@ -237,7 +238,7 @@ function copyToClipboard(text) {
|
|
237 |
|
238 |
with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=custom_css, js=_js_functions) as demo:
|
239 |
gr.Markdown("# 🖼️ AI 图像标签分析器")
|
240 |
-
gr.Markdown("
|
241 |
|
242 |
state_res = gr.State({})
|
243 |
state_translations_dict = gr.State({})
|
@@ -278,7 +279,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
278 |
|
279 |
gr.Markdown("### 标签汇总结果")
|
280 |
out_summary = gr.Textbox(
|
281 |
-
label="
|
282 |
placeholder="分析完成后,此处将显示汇总的英文标签...",
|
283 |
lines=5,
|
284 |
show_copy_button=True
|
@@ -290,19 +291,18 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
290 |
return "<p>暂无标签</p>"
|
291 |
|
292 |
html = '<div class="label-container">'
|
|
|
|
|
|
|
|
|
293 |
tag_keys = list(tags_dict.keys())
|
294 |
-
|
295 |
-
for i, tag in enumerate(tag_keys):
|
296 |
score = tags_dict[tag]
|
297 |
-
|
298 |
-
if isinstance(tag, str):
|
299 |
-
js_safe_tag_content = json.dumps(tag)[1:-1]
|
300 |
-
else:
|
301 |
-
js_safe_tag_content = "INVALID_TAG_DATA"
|
302 |
-
print(f"警告: 在format_tags_html中遇到非字符串标签: {tag} (类型: {type(tag)})")
|
303 |
|
304 |
html += '<div class="tag-item">'
|
305 |
-
tag_display_html = f'<span class="tag-en"
|
306 |
|
307 |
if show_translation_in_list and i < len(translations_list) and translations_list[i]:
|
308 |
tag_display_html += f'<span class="tag-zh">({translations_list[i]})</span>'
|
|
|
169 |
// --- 调试信息 ---
|
170 |
console.log('copyToClipboard function was called.');
|
171 |
console.log('Received text:', text);
|
172 |
+
// console.trace(); // 如果需要更详细的调用栈信息,可以取消这行注释
|
173 |
|
174 |
// --- 保护性检查 ---
|
175 |
// 如果 text 未定义或为 null,则不执行后续操作,并打印警告
|
176 |
if (typeof text === 'undefined' || text === null) {
|
177 |
console.warn('copyToClipboard was called with undefined or null text. Aborting this specific copy operation.');
|
178 |
+
// 在这种情况下,我们不应该尝试复制,也不应该显示“已复制”的提示
|
179 |
return;
|
180 |
}
|
181 |
|
182 |
navigator.clipboard.writeText(text).then(() => {
|
183 |
+
// console.log('Tag copied to clipboard: ' + text); // 成功复制的日志(可选)
|
184 |
const feedback = document.createElement('div');
|
185 |
|
186 |
// 确保 text 是字符串类型,再进行 substring 操作
|
|
|
202 |
setTimeout(() => {
|
203 |
feedback.style.opacity = '0';
|
204 |
setTimeout(() => {
|
205 |
+
if (document.body.contains(feedback)) { // 确保元素还在DOM中
|
206 |
document.body.removeChild(feedback);
|
207 |
}
|
208 |
}, 500);
|
209 |
}, 1500);
|
210 |
}).catch(err => {
|
211 |
console.error('Failed to copy tag. Error:', err, 'Attempted to copy text:', text);
|
212 |
+
// 可以考虑也给用户一个错误提示,但原版 alert 可能体验不佳
|
213 |
// alert('复制失败: ' + err);
|
214 |
const errorFeedback = document.createElement('div');
|
215 |
+
errorFeedback.textContent = '复制操作失败!'; // 更友好的错误提示
|
216 |
errorFeedback.style.position = 'fixed';
|
217 |
errorFeedback.style.bottom = '20px';
|
218 |
errorFeedback.style.left = '50%';
|
219 |
errorFeedback.style.transform = 'translateX(-50%)';
|
220 |
+
errorFeedback.style.backgroundColor = '#D32F2F'; // 红色背景表示错误
|
221 |
errorFeedback.style.color = 'white';
|
222 |
errorFeedback.style.padding = '10px 20px';
|
223 |
errorFeedback.style.borderRadius = '5px';
|
|
|
238 |
|
239 |
with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=custom_css, js=_js_functions) as demo:
|
240 |
gr.Markdown("# 🖼️ AI 图像标签分析器")
|
241 |
+
gr.Markdown("上传图片自动识别标签,支持中英文显示和一键复制。[NovelAI在线绘画](https://nai.idlecloud.cc/)")
|
242 |
|
243 |
state_res = gr.State({})
|
244 |
state_translations_dict = gr.State({})
|
|
|
279 |
|
280 |
gr.Markdown("### 标签汇总结果")
|
281 |
out_summary = gr.Textbox(
|
282 |
+
label="标签汇总",
|
283 |
placeholder="分析完成后,此处将显示汇总的英文标签...",
|
284 |
lines=5,
|
285 |
show_copy_button=True
|
|
|
291 |
return "<p>暂无标签</p>"
|
292 |
|
293 |
html = '<div class="label-container">'
|
294 |
+
|
295 |
+
if not isinstance(translations_list, list):
|
296 |
+
translations_list = []
|
297 |
+
|
298 |
tag_keys = list(tags_dict.keys())
|
299 |
+
|
300 |
+
for i, tag in enumerate(tag_keys):
|
301 |
score = tags_dict[tag]
|
302 |
+
escaped_tag = tag.replace("'", "\\'") # Escape for JS
|
|
|
|
|
|
|
|
|
|
|
303 |
|
304 |
html += '<div class="tag-item">'
|
305 |
+
tag_display_html = f'<span class="tag-en" onclick="copyToClipboard(\'{escaped_tag}\')">{tag}</span>'
|
306 |
|
307 |
if show_translation_in_list and i < len(translations_list) and translations_list[i]:
|
308 |
tag_display_html += f'<span class="tag-zh">({translations_list[i]})</span>'
|