Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -166,36 +166,28 @@ custom_css = """
|
|
166 |
|
167 |
_js_functions = """
|
168 |
function copyToClipboard(text) {
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
feedback.style.backgroundColor = '#D32F2F';
|
180 |
-
feedback.style.color = 'white';
|
181 |
-
feedback.style.padding = '10px 20px';
|
182 |
-
feedback.style.borderRadius = '5px';
|
183 |
-
feedback.style.zIndex = '10000';
|
184 |
-
feedback.style.transition = 'opacity 0.5s ease-out';
|
185 |
-
document.body.appendChild(feedback);
|
186 |
-
setTimeout(() => {
|
187 |
-
feedback.style.opacity = '0';
|
188 |
-
setTimeout(() => {
|
189 |
-
document.body.removeChild(feedback);
|
190 |
-
}, 500);
|
191 |
-
}, 3000);
|
192 |
return;
|
193 |
}
|
194 |
|
195 |
navigator.clipboard.writeText(text).then(() => {
|
|
|
196 |
const feedback = document.createElement('div');
|
197 |
-
|
198 |
-
|
|
|
|
|
|
|
|
|
199 |
feedback.style.position = 'fixed';
|
200 |
feedback.style.bottom = '20px';
|
201 |
feedback.style.left = '50%';
|
@@ -210,39 +202,42 @@ function copyToClipboard(text) {
|
|
210 |
setTimeout(() => {
|
211 |
feedback.style.opacity = '0';
|
212 |
setTimeout(() => {
|
213 |
-
document.body.
|
|
|
|
|
214 |
}, 500);
|
215 |
}, 1500);
|
216 |
}).catch(err => {
|
217 |
-
console.error('Failed to copy tag:
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
|
|
239 |
});
|
240 |
}
|
241 |
"""
|
242 |
|
243 |
with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=custom_css, js=_js_functions) as demo:
|
244 |
gr.Markdown("# 🖼️ AI 图像标签分析器")
|
245 |
-
gr.Markdown("
|
246 |
|
247 |
state_res = gr.State({})
|
248 |
state_translations_dict = gr.State({})
|
@@ -300,7 +295,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=cus
|
|
300 |
for i, tag in enumerate(tag_keys): # tag 应该是字符串
|
301 |
score = tags_dict[tag]
|
302 |
|
303 |
-
# 改进的转义方法:
|
304 |
if isinstance(tag, str):
|
305 |
js_safe_tag_content = json.dumps(tag)[1:-1]
|
306 |
else:
|
|
|
166 |
|
167 |
_js_functions = """
|
168 |
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 操作
|
187 |
+
let displayText = String(text); // 将 text 转换为字符串以防万一
|
188 |
+
displayText = displayText.substring(0, 30) + (displayText.length > 30 ? '...' : '');
|
189 |
+
|
190 |
+
feedback.textContent = '已复制: ' + displayText;
|
191 |
feedback.style.position = 'fixed';
|
192 |
feedback.style.bottom = '20px';
|
193 |
feedback.style.left = '50%';
|
|
|
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';
|
223 |
+
errorFeedback.style.zIndex = '10000';
|
224 |
+
errorFeedback.style.transition = 'opacity 0.5s ease-out';
|
225 |
+
document.body.appendChild(errorFeedback);
|
226 |
+
setTimeout(() => {
|
227 |
+
errorFeedback.style.opacity = '0';
|
228 |
+
setTimeout(() => {
|
229 |
+
if (document.body.contains(errorFeedback)) {
|
230 |
+
document.body.removeChild(errorFeedback);
|
231 |
+
}
|
232 |
+
}, 500);
|
233 |
+
}, 2500);
|
234 |
});
|
235 |
}
|
236 |
"""
|
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("上传图片自动识别标签,支持中英文显示。[在线体验AI绘画](https://nai.idlecloud.cc/ "IDLE_CLOUD")")
|
241 |
|
242 |
state_res = gr.State({})
|
243 |
state_translations_dict = gr.State({})
|
|
|
295 |
for i, tag in enumerate(tag_keys): # tag 应该是字符串
|
296 |
score = tags_dict[tag]
|
297 |
|
|
|
298 |
if isinstance(tag, str):
|
299 |
js_safe_tag_content = json.dumps(tag)[1:-1]
|
300 |
else:
|