Spaces:
Running
Running
Update app.py
Browse files168 unmodified lines16917017117217317417517617717817918018118218318418518615 unmodified lines20220320420520620720820921021121221321421521621721821922022122214 unmodified lines23723823924024124224334 unmodified lines2782792802812822832845 unmodified lines290291292293294295296297298299300301302303304305306307308168 unmodified lines// --- 调试信息 ---console.log('copyToClipboard function was called.');console.log('Received text:', text);// console.trace();// --- 保护性检查 ---// 如果 text 未定义或为 null,则不执行后续操作,并打印警告if (typeof text === 'undefined' || text === null) {console.warn('copyToClipboard was called with undefined or null text. Aborting this specific copy operation.');// 在这种情况下,不应该尝试复制,也不应该显示“已复制”的提示return;}navigator.clipboard.writeText(text).then(() => {// console.log('Tag copied to clipboard: ' + text);const feedback = document.createElement('div');// 确保 text 是字符串类型,再进行 substring 操作15 unmodified linessetTimeout(() => {feedback.style.opacity = '0';setTimeout(() => {if (document.body.contains(feedback)) {document.body.removeChild(feedback);}}, 500);}, 1500);}).catch(err => {console.error('Failed to copy tag. Error:', err, 'Attempted to copy text:', text);// alert('复制失败: ' + err);const errorFeedback = document.createElement('div');errorFeedback.textContent = '复制操作失败!'; // 错误提示errorFeedback.style.position = 'fixed';errorFeedback.style.bottom = '20px';errorFeedback.style.left = '50%';errorFeedback.style.transform = 'translateX(-50%)';errorFeedback.style.backgroundColor = '#D32F2F'; // 红色背景errorFeedback.style.color = 'white';errorFeedback.style.padding = '10px 20px';errorFeedback.style.borderRadius = '5px';14 unmodified lineswith gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=custom_css, js=_js_functions) as demo:gr.Markdown("# 🖼️ AI 图像标签分析器")gr.Markdown("上传图片自动识别标签,支持中英文显示。[在线体验AI绘画](https://nai.idlecloud.cc/)")state_res = gr.State({})state_translations_dict = gr.State({})34 unmodified linesgr.Markdown("### 标签汇总结果")out_summary = gr.Textbox(label="标签汇总(仅英文,可通过上方设置添加中文)",placeholder="分析完成后,此处将显示汇总的英文标签...",lines=5,show_copy_button=True5 unmodified linesreturn "<p>暂无标签</p>"html = '<div class="label-container">'tag_keys = list(tags_dict.keys())for i, tag in enumerate(tag_keys): # tag 应该是字符串score = tags_dict[tag]if isinstance(tag, str):js_safe_tag_content = json.dumps(tag)[1:-1]else:js_safe_tag_content = "INVALID_TAG_DATA"print(f"警告: 在format_tags_html中遇到非字符串标签: {tag} (类型: {type(tag)})")html += '<div class="tag-item">'tag_display_html = f'<span class="tag-en" style="cursor:pointer;" onclick="copyToClipboard(\'{js_safe_tag_content}\')">{tag}</span>'if show_translation_in_list and i < len(translations_list) and translations_list[i]:tag_display_html += f'<span class="tag-zh">({translations_list[i]})</span>'168 unmodified lines16917017117217317417517617717817918018118218318418518615 unmodified lines20220320420520620720820921021121221321421521621721821922022122222314 unmodified lines23823924024124224324434 unmodified lines2792802812822832842855 unmodified lines291292293294295296297298299300301302303304305306307308168 unmodified lines// --- 调试信息 ---console.log('copyToClipboard function was called.');console.log('Received text:', text);// console.trace(); // 如果需要更详细的调用栈信息,可以取消这行注释// --- 保护性检查 ---// 如果 text 未定义或为 null,则不执行后续操作,并打印警告if (typeof text === 'undefined' || text === null) {console.warn('copyToClipboard was called with undefined or null text. Aborting this specific copy operation.');// 在这种情况下,我们不应该尝试复制,也不应该显示“已复制”的提示return;}navigator.clipboard.writeText(text).then(() => {// console.log('Tag copied to clipboard: ' + text); // 成功复制的日志(可选)const feedback = document.createElement('div');// 确保 text 是字符串类型,再进行 substring 操作15 unmodified linessetTimeout(() => {feedback.style.opacity = '0';setTimeout(() => {if (document.body.contains(feedback)) { // 确保元素还在DOM中document.body.removeChild(feedback);}}, 500);}, 1500);}).catch(err => {console.error('Failed to copy tag. Error:', err, 'Attempted to copy text:', text);// 可以考虑也给用户一个错误提示,但原版 alert 可能体验不佳// alert('复制失败: ' + err);const errorFeedback = document.createElement('div');errorFeedback.textContent = '复制操作失败!'; // 更友好的错误提示errorFeedback.style.position = 'fixed';errorFeedback.style.bottom = '20px';errorFeedback.style.left = '50%';errorFeedback.style.transform = 'translateX(-50%)';errorFeedback.style.backgroundColor = '#D32F2F'; // 红色背景表示错误errorFeedback.style.color = 'white';errorFeedback.style.padding = '10px 20px';errorFeedback.style.borderRadius = '5px';14 unmodified lineswith gr.Blocks(theme=gr.themes.Soft(), title="AI 图像标签分析器", css=custom_css, js=_js_functions) as demo:gr.Markdown("# 🖼️ AI 图像标签分析器")gr.Markdown("上传图片自动识别标签,支持中英文显示和一键复制。[NovelAI在线绘画](https://nai.idlecloud.cc/)")state_res = gr.State({})state_translations_dict = gr.State({})34 unmodified linesgr.Markdown("### 标签汇总结果")out_summary = gr.Textbox(label="标签汇总",placeholder="分析完成后,此处将显示汇总的英文标签...",lines=5,show_copy_button=True5 unmodified linesreturn "<p>暂无标签</p>"html = '<div class="label-container">'if not isinstance(translations_list, list):translations_list = []tag_keys = list(tags_dict.keys())for i, tag in enumerate(tag_keys):score = tags_dict[tag]escaped_tag = tag.replace("'", "\\'") # Escape for JShtml += '<div class="tag-item">'tag_display_html = f'<span class="tag-en" onclick="copyToClipboard(\'{escaped_tag}\')">{tag}</span>'if show_translation_in_list and i < len(translations_list) and translations_list[i]:tag_display_html += f'<span class="tag-zh">({translations_list[i]})</span>'
app.py+17-17