IdlecloudX commited on
Commit
da15f0e
·
verified ·
1 Parent(s): c4ebff6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -50
app.py CHANGED
@@ -166,36 +166,28 @@ custom_css = """
166
 
167
  _js_functions = """
168
  function copyToClipboard(text) {
169
- console.log("copyToClipboard called with text:", text, "Type:", typeof text);
170
-
171
- if (typeof text !== 'string') {
172
- console.error('copyToClipboard Error: text is not a string. Received type:', typeof text, 'value:', text);
173
- const feedback = document.createElement('div');
174
- feedback.textContent = '复制错误: 无效的标签数据 (类型: ' + typeof text + ')';
175
- feedback.style.position = 'fixed';
176
- feedback.style.bottom = '20px';
177
- feedback.style.left = '50%';
178
- feedback.style.transform = 'translateX(-50%)';
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
- const displayCopiedText = text.length > 40 ? text.substring(0, 37) + '...' : text;
198
- feedback.textContent = '已复制: ' + displayCopiedText;
 
 
 
 
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.removeChild(feedback);
 
 
214
  }, 500);
215
  }, 1500);
216
  }).catch(err => {
217
- console.error('Failed to copy tag: ', text, err);
218
- let errorMsg = '复制失败';
219
- if (err.name === 'NotAllowedError') {
220
- errorMsg = '复制失败: 未授予剪贴板权限。请检查浏览器设置。';
221
- } else if (err.message && (err.message.includes("Clipboard write was denied") || err.message.includes("transient activation"))) {
222
- errorMsg = '复制失败: 请先点击页面任意位置以激活,然后再试。';
223
- } else if (err.message) {
224
- errorMsg = `复制失败: ${err.message.substring(0, 70)}`;
225
- }
226
- const feedback = document.createElement('div');
227
- feedback.textContent = errorMsg;
228
- feedback.style.position = 'fixed';
229
- feedback.style.bottom = '20px';
230
- feedback.style.left = '50%';
231
- feedback.style.transform = 'translateX(-50%)';
232
- feedback.style.backgroundColor = '#D32F2F';
233
- feedback.style.color = 'white';
234
- feedback.style.padding = '10px 20px';
235
- feedback.style.borderRadius = '5px';
236
- feedback.style.zIndex = '10000';
237
- document.body.appendChild(feedback);
238
- setTimeout(() => { document.body.removeChild(feedback); }, 3000);
 
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: