Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -230,11 +230,18 @@ with gr.Blocks(
|
|
230 |
outline: none !important;
|
231 |
}
|
232 |
|
233 |
-
/* 按鈕樣式 -
|
234 |
button,
|
235 |
.gr-button,
|
236 |
input[type="submit"],
|
237 |
-
input[type="button"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
239 |
border: none !important;
|
240 |
border-radius: 12px !important;
|
@@ -246,17 +253,29 @@ with gr.Blocks(
|
|
246 |
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4) !important;
|
247 |
cursor: pointer !important;
|
248 |
pointer-events: auto !important;
|
249 |
-
z-index:
|
250 |
position: relative !important;
|
251 |
display: inline-block !important;
|
252 |
min-height: 44px !important;
|
253 |
line-height: normal !important;
|
|
|
|
|
|
|
|
|
|
|
254 |
}
|
255 |
|
256 |
button:hover,
|
257 |
.gr-button:hover,
|
258 |
input[type="submit"]:hover,
|
259 |
-
input[type="button"]:hover
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
transform: translateY(-2px) !important;
|
261 |
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6) !important;
|
262 |
background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%) !important;
|
@@ -265,7 +284,14 @@ with gr.Blocks(
|
|
265 |
button:active,
|
266 |
.gr-button:active,
|
267 |
input[type="submit"]:active,
|
268 |
-
input[type="button"]:active
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
269 |
transform: translateY(0px) !important;
|
270 |
box-shadow: 0 3px 10px rgba(102, 126, 234, 0.4) !important;
|
271 |
}
|
@@ -273,27 +299,44 @@ with gr.Blocks(
|
|
273 |
/* 次要按鈕樣式 */
|
274 |
button[data-testid*="secondary"],
|
275 |
.gr-button.secondary,
|
276 |
-
button.secondary
|
|
|
|
|
|
|
|
|
277 |
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%) !important;
|
278 |
}
|
279 |
|
280 |
button[data-testid*="secondary"]:hover,
|
281 |
.gr-button.secondary:hover,
|
282 |
-
button.secondary:hover
|
|
|
|
|
|
|
|
|
283 |
background: linear-gradient(135deg, #e081e9 0%, #e3455a 100%) !important;
|
284 |
}
|
285 |
|
286 |
/* 修復 Gradio 特定的按鈕容器 */
|
|
|
287 |
.gr-form > div,
|
288 |
.gr-button-group,
|
289 |
-
div[data-testid="button"]
|
|
|
290 |
pointer-events: auto !important;
|
291 |
-
z-index:
|
292 |
}
|
293 |
|
294 |
-
/*
|
295 |
button span,
|
296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
pointer-events: none !important;
|
298 |
user-select: none !important;
|
299 |
}
|
@@ -381,29 +424,115 @@ with gr.Blocks(
|
|
381 |
</style>
|
382 |
<script>
|
383 |
document.addEventListener('DOMContentLoaded', function() {
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
392 |
});
|
|
|
|
|
393 |
}
|
394 |
|
395 |
-
//
|
396 |
-
|
397 |
|
398 |
-
//
|
399 |
-
setInterval(
|
400 |
|
401 |
// 監聽 DOM 變化
|
402 |
-
const observer = new MutationObserver(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
403 |
observer.observe(document.body, {
|
404 |
childList: true,
|
405 |
-
subtree: true
|
|
|
|
|
406 |
});
|
|
|
|
|
|
|
|
|
|
|
407 |
});
|
408 |
</script>
|
409 |
<style>
|
@@ -470,6 +599,13 @@ with gr.Blocks(
|
|
470 |
placeholder="請輸入您的 OpenAI API Key (sk-...)",
|
471 |
elem_classes="api-input"
|
472 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
473 |
api_key_status = gr.Textbox(
|
474 |
label="📊 API 連接狀態",
|
475 |
interactive=False,
|
@@ -484,6 +620,13 @@ with gr.Blocks(
|
|
484 |
value="gpt-4",
|
485 |
elem_classes="model-selector"
|
486 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
487 |
model_status = gr.Textbox(
|
488 |
label="🎯 當前模型",
|
489 |
interactive=False,
|
@@ -491,8 +634,17 @@ with gr.Blocks(
|
|
491 |
elem_classes="status-display"
|
492 |
)
|
493 |
|
494 |
-
#
|
495 |
-
gr.HTML("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
496 |
|
497 |
with gr.Tab("📄 PDF 處理", elem_classes="pdf-tab"):
|
498 |
gr.Markdown("### 📁 文件上傳與摘要生成")
|
@@ -509,12 +661,14 @@ with gr.Blocks(
|
|
509 |
summary_btn = gr.Button(
|
510 |
"🔄 開始生成摘要",
|
511 |
variant="primary",
|
512 |
-
|
|
|
513 |
)
|
514 |
clear_btn = gr.Button(
|
515 |
"🗑️ 清除所有資料",
|
516 |
variant="secondary",
|
517 |
-
|
|
|
518 |
)
|
519 |
|
520 |
with gr.Column():
|
@@ -538,7 +692,8 @@ with gr.Blocks(
|
|
538 |
question_btn = gr.Button(
|
539 |
"📤 發送問題",
|
540 |
variant="primary",
|
541 |
-
|
|
|
542 |
)
|
543 |
|
544 |
gr.Markdown("""
|
@@ -558,11 +713,22 @@ with gr.Blocks(
|
|
558 |
)
|
559 |
|
560 |
# 事件處理器
|
|
|
|
|
561 |
api_key_input.submit(set_api_key, inputs=api_key_input, outputs=api_key_status)
|
|
|
|
|
|
|
562 |
model_choice.change(set_model, inputs=model_choice, outputs=model_status)
|
|
|
|
|
563 |
summary_btn.click(generate_summary, inputs=pdf_upload, outputs=summary_output)
|
|
|
|
|
564 |
question_btn.click(ask_question, inputs=question_input, outputs=answer_output)
|
565 |
question_input.submit(ask_question, inputs=question_input, outputs=answer_output)
|
|
|
|
|
566 |
clear_btn.click(clear_all, outputs=[summary_output, question_input, answer_output])
|
567 |
|
568 |
if __name__ == "__main__":
|
|
|
230 |
outline: none !important;
|
231 |
}
|
232 |
|
233 |
+
/* 按鈕樣式 - 強力修復所有按鈕 */
|
234 |
button,
|
235 |
.gr-button,
|
236 |
input[type="submit"],
|
237 |
+
input[type="button"],
|
238 |
+
#summary-button,
|
239 |
+
#clear-button,
|
240 |
+
#question-button,
|
241 |
+
#api-key-button,
|
242 |
+
#model-button,
|
243 |
+
.action-button,
|
244 |
+
[data-testid*="button"] {
|
245 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
246 |
border: none !important;
|
247 |
border-radius: 12px !important;
|
|
|
253 |
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4) !important;
|
254 |
cursor: pointer !important;
|
255 |
pointer-events: auto !important;
|
256 |
+
z-index: 9999 !important;
|
257 |
position: relative !important;
|
258 |
display: inline-block !important;
|
259 |
min-height: 44px !important;
|
260 |
line-height: normal !important;
|
261 |
+
width: auto !important;
|
262 |
+
min-width: 120px !important;
|
263 |
+
text-align: center !important;
|
264 |
+
user-select: none !important;
|
265 |
+
margin: 10px 5px !important;
|
266 |
}
|
267 |
|
268 |
button:hover,
|
269 |
.gr-button:hover,
|
270 |
input[type="submit"]:hover,
|
271 |
+
input[type="button"]:hover,
|
272 |
+
#summary-button:hover,
|
273 |
+
#clear-button:hover,
|
274 |
+
#question-button:hover,
|
275 |
+
#api-key-button:hover,
|
276 |
+
#model-button:hover,
|
277 |
+
.action-button:hover,
|
278 |
+
[data-testid*="button"]:hover {
|
279 |
transform: translateY(-2px) !important;
|
280 |
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6) !important;
|
281 |
background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%) !important;
|
|
|
284 |
button:active,
|
285 |
.gr-button:active,
|
286 |
input[type="submit"]:active,
|
287 |
+
input[type="button"]:active,
|
288 |
+
#summary-button:active,
|
289 |
+
#clear-button:active,
|
290 |
+
#question-button:active,
|
291 |
+
#api-key-button:active,
|
292 |
+
#model-button:active,
|
293 |
+
.action-button:active,
|
294 |
+
[data-testid*="button"]:active {
|
295 |
transform: translateY(0px) !important;
|
296 |
box-shadow: 0 3px 10px rgba(102, 126, 234, 0.4) !important;
|
297 |
}
|
|
|
299 |
/* 次要按鈕樣式 */
|
300 |
button[data-testid*="secondary"],
|
301 |
.gr-button.secondary,
|
302 |
+
button.secondary,
|
303 |
+
#clear-button,
|
304 |
+
#model-button,
|
305 |
+
.secondary-btn,
|
306 |
+
button[variant="secondary"] {
|
307 |
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%) !important;
|
308 |
}
|
309 |
|
310 |
button[data-testid*="secondary"]:hover,
|
311 |
.gr-button.secondary:hover,
|
312 |
+
button.secondary:hover,
|
313 |
+
#clear-button:hover,
|
314 |
+
#model-button:hover,
|
315 |
+
.secondary-btn:hover,
|
316 |
+
button[variant="secondary"]:hover {
|
317 |
background: linear-gradient(135deg, #e081e9 0%, #e3455a 100%) !important;
|
318 |
}
|
319 |
|
320 |
/* 修復 Gradio 特定的按鈕容器 */
|
321 |
+
.gr-form,
|
322 |
.gr-form > div,
|
323 |
.gr-button-group,
|
324 |
+
div[data-testid="button"],
|
325 |
+
div[data-testid*="button"] {
|
326 |
pointer-events: auto !important;
|
327 |
+
z-index: 9998 !important;
|
328 |
}
|
329 |
|
330 |
+
/* 確保按鈕內的文字和圖標不會阻擋點擊 */
|
331 |
button span,
|
332 |
+
button svg,
|
333 |
+
button i,
|
334 |
+
.gr-button span,
|
335 |
+
.gr-button svg,
|
336 |
+
.gr-button i,
|
337 |
+
.action-button span,
|
338 |
+
.action-button svg,
|
339 |
+
.action-button i {
|
340 |
pointer-events: none !important;
|
341 |
user-select: none !important;
|
342 |
}
|
|
|
424 |
</style>
|
425 |
<script>
|
426 |
document.addEventListener('DOMContentLoaded', function() {
|
427 |
+
console.log('開始修復按鈕...');
|
428 |
+
|
429 |
+
// 強力修復所有按鈕
|
430 |
+
function fixAllButtons() {
|
431 |
+
// 所有可能的按鈕選擇器
|
432 |
+
const selectors = [
|
433 |
+
'button',
|
434 |
+
'input[type="submit"]',
|
435 |
+
'input[type="button"]',
|
436 |
+
'.gr-button',
|
437 |
+
'[data-testid*="button"]',
|
438 |
+
'#summary-button',
|
439 |
+
'#clear-button',
|
440 |
+
'#question-button',
|
441 |
+
'#api-key-button',
|
442 |
+
'#model-button',
|
443 |
+
'.action-button',
|
444 |
+
'.primary-btn',
|
445 |
+
'.secondary-btn'
|
446 |
+
];
|
447 |
+
|
448 |
+
let buttonCount = 0;
|
449 |
+
selectors.forEach(selector => {
|
450 |
+
const elements = document.querySelectorAll(selector);
|
451 |
+
elements.forEach(btn => {
|
452 |
+
if (btn) {
|
453 |
+
// 強制設定樣式
|
454 |
+
btn.style.pointerEvents = 'auto';
|
455 |
+
btn.style.cursor = 'pointer';
|
456 |
+
btn.style.zIndex = '9999';
|
457 |
+
btn.style.position = 'relative';
|
458 |
+
btn.style.display = 'inline-block';
|
459 |
+
btn.style.minHeight = '44px';
|
460 |
+
btn.style.minWidth = '120px';
|
461 |
+
btn.style.padding = '15px 30px';
|
462 |
+
btn.style.borderRadius = '12px';
|
463 |
+
btn.style.border = 'none';
|
464 |
+
btn.style.fontWeight = '600';
|
465 |
+
btn.style.fontSize = '16px';
|
466 |
+
btn.style.color = 'white';
|
467 |
+
btn.style.userSelect = 'none';
|
468 |
+
btn.style.margin = '10px 5px';
|
469 |
+
btn.style.textAlign = 'center';
|
470 |
+
|
471 |
+
// 設定背景色
|
472 |
+
if (btn.id === 'clear-button' ||
|
473 |
+
btn.id === 'model-button' ||
|
474 |
+
btn.classList.contains('secondary-btn')) {
|
475 |
+
btn.style.background = 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)';
|
476 |
+
} else {
|
477 |
+
btn.style.background = 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)';
|
478 |
+
}
|
479 |
+
|
480 |
+
// 移除可能阻止點擊的屬性
|
481 |
+
btn.removeAttribute('disabled');
|
482 |
+
|
483 |
+
// 確保子元素不會阻止點擊
|
484 |
+
const children = btn.querySelectorAll('*');
|
485 |
+
children.forEach(child => {
|
486 |
+
child.style.pointerEvents = 'none';
|
487 |
+
child.style.userSelect = 'none';
|
488 |
+
});
|
489 |
+
|
490 |
+
buttonCount++;
|
491 |
+
}
|
492 |
+
});
|
493 |
});
|
494 |
+
|
495 |
+
console.log(`修復了 ${buttonCount} 個按鈕`);
|
496 |
}
|
497 |
|
498 |
+
// 立即修復
|
499 |
+
fixAllButtons();
|
500 |
|
501 |
+
// 每秒檢查一次
|
502 |
+
setInterval(fixAllButtons, 1000);
|
503 |
|
504 |
// 監聽 DOM 變化
|
505 |
+
const observer = new MutationObserver(function(mutations) {
|
506 |
+
let shouldFix = false;
|
507 |
+
mutations.forEach(function(mutation) {
|
508 |
+
if (mutation.type === 'childList') {
|
509 |
+
mutation.addedNodes.forEach(function(node) {
|
510 |
+
if (node.nodeType === 1) { // 元素節點
|
511 |
+
if (node.tagName === 'BUTTON' ||
|
512 |
+
node.querySelector && node.querySelector('button')) {
|
513 |
+
shouldFix = true;
|
514 |
+
}
|
515 |
+
}
|
516 |
+
});
|
517 |
+
}
|
518 |
+
});
|
519 |
+
|
520 |
+
if (shouldFix) {
|
521 |
+
setTimeout(fixAllButtons, 100);
|
522 |
+
}
|
523 |
+
});
|
524 |
+
|
525 |
observer.observe(document.body, {
|
526 |
childList: true,
|
527 |
+
subtree: true,
|
528 |
+
attributes: true,
|
529 |
+
attributeFilter: ['style', 'class']
|
530 |
});
|
531 |
+
|
532 |
+
// 添加全域點擊事件監聽
|
533 |
+
document.addEventListener('click', function(e) {
|
534 |
+
console.log('點擊事件:', e.target);
|
535 |
+
}, true);
|
536 |
});
|
537 |
</script>
|
538 |
<style>
|
|
|
599 |
placeholder="請輸入您的 OpenAI API Key (sk-...)",
|
600 |
elem_classes="api-input"
|
601 |
)
|
602 |
+
# 添加確認按鈕
|
603 |
+
api_key_btn = gr.Button(
|
604 |
+
"✅ 確認設定 API Key",
|
605 |
+
variant="primary",
|
606 |
+
elem_id="api-key-button",
|
607 |
+
elem_classes="action-button primary-btn"
|
608 |
+
)
|
609 |
api_key_status = gr.Textbox(
|
610 |
label="📊 API 連接狀態",
|
611 |
interactive=False,
|
|
|
620 |
value="gpt-4",
|
621 |
elem_classes="model-selector"
|
622 |
)
|
623 |
+
# 添加模型確認按鈕
|
624 |
+
model_btn = gr.Button(
|
625 |
+
"🎯 確認選擇模型",
|
626 |
+
variant="secondary",
|
627 |
+
elem_id="model-button",
|
628 |
+
elem_classes="action-button secondary-btn"
|
629 |
+
)
|
630 |
model_status = gr.Textbox(
|
631 |
label="🎯 當前模型",
|
632 |
interactive=False,
|
|
|
634 |
elem_classes="status-display"
|
635 |
)
|
636 |
|
637 |
+
# 添加使用說明
|
638 |
+
gr.HTML("""
|
639 |
+
<div style='margin: 20px 0; padding: 15px; background: rgba(102, 126, 234, 0.1); border-radius: 10px;'>
|
640 |
+
<strong>📋 使用步驟:</strong>
|
641 |
+
<ol style='margin: 10px 0; padding-left: 20px;'>
|
642 |
+
<li>輸入您的 OpenAI API Key 並點擊「確認設定」</li>
|
643 |
+
<li>選擇 AI 模型並點擊「確認選擇」</li>
|
644 |
+
<li>前往「PDF 處理」頁面上傳文件開始使用!</li>
|
645 |
+
</ol>
|
646 |
+
</div>
|
647 |
+
""")
|
648 |
|
649 |
with gr.Tab("📄 PDF 處理", elem_classes="pdf-tab"):
|
650 |
gr.Markdown("### 📁 文件上傳與摘要生成")
|
|
|
661 |
summary_btn = gr.Button(
|
662 |
"🔄 開始生成摘要",
|
663 |
variant="primary",
|
664 |
+
elem_id="summary-button",
|
665 |
+
elem_classes="action-button primary-btn"
|
666 |
)
|
667 |
clear_btn = gr.Button(
|
668 |
"🗑️ 清除所有資料",
|
669 |
variant="secondary",
|
670 |
+
elem_id="clear-button",
|
671 |
+
elem_classes="action-button secondary-btn"
|
672 |
)
|
673 |
|
674 |
with gr.Column():
|
|
|
692 |
question_btn = gr.Button(
|
693 |
"📤 發送問題",
|
694 |
variant="primary",
|
695 |
+
elem_id="question-button",
|
696 |
+
elem_classes="action-button primary-btn"
|
697 |
)
|
698 |
|
699 |
gr.Markdown("""
|
|
|
713 |
)
|
714 |
|
715 |
# 事件處理器
|
716 |
+
# API Key 設定事件
|
717 |
+
api_key_btn.click(set_api_key, inputs=api_key_input, outputs=api_key_status)
|
718 |
api_key_input.submit(set_api_key, inputs=api_key_input, outputs=api_key_status)
|
719 |
+
|
720 |
+
# 模型選擇事件
|
721 |
+
model_btn.click(set_model, inputs=model_choice, outputs=model_status)
|
722 |
model_choice.change(set_model, inputs=model_choice, outputs=model_status)
|
723 |
+
|
724 |
+
# PDF 處理事件
|
725 |
summary_btn.click(generate_summary, inputs=pdf_upload, outputs=summary_output)
|
726 |
+
|
727 |
+
# 問答事件
|
728 |
question_btn.click(ask_question, inputs=question_input, outputs=answer_output)
|
729 |
question_input.submit(ask_question, inputs=question_input, outputs=answer_output)
|
730 |
+
|
731 |
+
# 清除功能
|
732 |
clear_btn.click(clear_all, outputs=[summary_output, question_input, answer_output])
|
733 |
|
734 |
if __name__ == "__main__":
|