Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,24 +21,38 @@ def set_api_key(user_api_key):
|
|
21 |
if not api_key:
|
22 |
return "❌ API Key 不能為空"
|
23 |
|
|
|
|
|
|
|
24 |
client = OpenAI(api_key=api_key)
|
25 |
|
26 |
# 測試 API Key 是否有效
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
except Exception as e:
|
35 |
-
return f"❌
|
36 |
|
37 |
def set_model(model_name):
|
38 |
"""設定選擇的模型"""
|
39 |
global selected_model
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
42 |
|
43 |
def extract_pdf_text(file_path):
|
44 |
"""從 PDF 文件中提取文字"""
|
@@ -615,9 +629,10 @@ with gr.Blocks(
|
|
615 |
|
616 |
with gr.Column():
|
617 |
model_choice = gr.Radio(
|
618 |
-
["gpt-4", "gpt-4.1", "gpt-4.5"],
|
619 |
label="🤖 選擇 AI 模型",
|
620 |
value="gpt-4",
|
|
|
621 |
elem_classes="model-selector"
|
622 |
)
|
623 |
# 添加模型確認按鈕
|
@@ -634,7 +649,7 @@ with gr.Blocks(
|
|
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>
|
@@ -643,8 +658,32 @@ with gr.Blocks(
|
|
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("### 📁 文件上傳與摘要生成")
|
@@ -712,14 +751,39 @@ with gr.Blocks(
|
|
712 |
elem_classes="answer-output"
|
713 |
)
|
714 |
|
715 |
-
# 事件處理器
|
716 |
-
# API Key
|
717 |
-
|
718 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
719 |
|
720 |
# 模型選擇事件
|
721 |
-
model_btn.click(
|
722 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
723 |
|
724 |
# PDF 處理事件
|
725 |
summary_btn.click(generate_summary, inputs=pdf_upload, outputs=summary_output)
|
|
|
21 |
if not api_key:
|
22 |
return "❌ API Key 不能為空"
|
23 |
|
24 |
+
if not api_key.startswith('sk-'):
|
25 |
+
return "❌ API Key 格式錯誤,應該以 'sk-' 開頭"
|
26 |
+
|
27 |
client = OpenAI(api_key=api_key)
|
28 |
|
29 |
# 測試 API Key 是否有效
|
30 |
+
try:
|
31 |
+
test_response = client.chat.completions.create(
|
32 |
+
model="gpt-3.5-turbo",
|
33 |
+
messages=[{"role": "user", "content": "Hello"}],
|
34 |
+
max_tokens=5
|
35 |
+
)
|
36 |
+
return "✅ API Key 已設定並驗證成功!可以開始使用了。"
|
37 |
+
except Exception as e:
|
38 |
+
if "incorrect_api_key" in str(e).lower():
|
39 |
+
return "❌ API Key 無效,請檢查是否正確"
|
40 |
+
elif "quota" in str(e).lower():
|
41 |
+
return "⚠️ API Key 有效,但配額不足"
|
42 |
+
else:
|
43 |
+
return f"⚠️ API Key 設定成功,但測試時出現問題:{str(e)[:100]}"
|
44 |
+
|
45 |
except Exception as e:
|
46 |
+
return f"❌ 設定失敗:{str(e)}"
|
47 |
|
48 |
def set_model(model_name):
|
49 |
"""設定選擇的模型"""
|
50 |
global selected_model
|
51 |
+
if model_name:
|
52 |
+
selected_model = model_name
|
53 |
+
return f"✅ 模型已更新為:{model_name}"
|
54 |
+
else:
|
55 |
+
return "❌ 請選擇一個模型"
|
56 |
|
57 |
def extract_pdf_text(file_path):
|
58 |
"""從 PDF 文件中提取文字"""
|
|
|
629 |
|
630 |
with gr.Column():
|
631 |
model_choice = gr.Radio(
|
632 |
+
choices=["gpt-4", "gpt-4.1", "gpt-4.5"],
|
633 |
label="🤖 選擇 AI 模型",
|
634 |
value="gpt-4",
|
635 |
+
interactive=True,
|
636 |
elem_classes="model-selector"
|
637 |
)
|
638 |
# 添加模型確認按鈕
|
|
|
649 |
elem_classes="status-display"
|
650 |
)
|
651 |
|
652 |
+
# 添加使用說明和測試區域
|
653 |
gr.HTML("""
|
654 |
<div style='margin: 20px 0; padding: 15px; background: rgba(102, 126, 234, 0.1); border-radius: 10px;'>
|
655 |
<strong>📋 使用步驟:</strong>
|
|
|
658 |
<li>選擇 AI 模型並點擊「確認選擇」</li>
|
659 |
<li>前往「PDF 處理」頁面上傳文件開始使用!</li>
|
660 |
</ol>
|
661 |
+
<div style='margin-top: 15px; padding: 10px; background: rgba(255, 193, 7, 0.1); border-radius: 8px;'>
|
662 |
+
<strong>🔧 調試信息:</strong>
|
663 |
+
<br>• 如果按鈕沒反應,請檢查瀏覽器控制台
|
664 |
+
<br>• API Key 應該以 'sk-' 開頭
|
665 |
+
<br>• 模型選擇為單選,一次只能選一個
|
666 |
+
</div>
|
667 |
</div>
|
668 |
""")
|
669 |
+
|
670 |
+
# 添加測試按鈕
|
671 |
+
with gr.Row():
|
672 |
+
test_btn = gr.Button(
|
673 |
+
"🧪 測試按鈕功能",
|
674 |
+
variant="secondary",
|
675 |
+
elem_id="test-button"
|
676 |
+
)
|
677 |
+
test_output = gr.Textbox(
|
678 |
+
label="測試結果",
|
679 |
+
interactive=False,
|
680 |
+
placeholder="點擊測試按鈕查看是否正常工作"
|
681 |
+
)
|
682 |
+
|
683 |
+
def test_function():
|
684 |
+
return "✅ 按鈕功能正常!如果您看到這個訊息,表示按鈕點擊事件正常工作。"
|
685 |
+
|
686 |
+
test_btn.click(test_function, outputs=test_output)
|
687 |
|
688 |
with gr.Tab("📄 PDF 處理", elem_classes="pdf-tab"):
|
689 |
gr.Markdown("### 📁 文件上傳與摘要生成")
|
|
|
751 |
elem_classes="answer-output"
|
752 |
)
|
753 |
|
754 |
+
# 事件處理器 - 確保按鈕點擊有反應
|
755 |
+
# API Key 設定事件(雙重綁定確保有效)
|
756 |
+
def handle_api_key_click(api_key_value):
|
757 |
+
print(f"處理 API Key: {api_key_value[:10]}..." if api_key_value else "空值")
|
758 |
+
return set_api_key(api_key_value)
|
759 |
+
|
760 |
+
def handle_model_click(model_value):
|
761 |
+
print(f"處理模型選擇: {model_value}")
|
762 |
+
return set_model(model_value)
|
763 |
+
|
764 |
+
# 綁定事件
|
765 |
+
api_key_btn.click(
|
766 |
+
fn=handle_api_key_click,
|
767 |
+
inputs=[api_key_input],
|
768 |
+
outputs=[api_key_status]
|
769 |
+
)
|
770 |
+
api_key_input.submit(
|
771 |
+
fn=handle_api_key_click,
|
772 |
+
inputs=[api_key_input],
|
773 |
+
outputs=[api_key_status]
|
774 |
+
)
|
775 |
|
776 |
# 模型選擇事件
|
777 |
+
model_btn.click(
|
778 |
+
fn=handle_model_click,
|
779 |
+
inputs=[model_choice],
|
780 |
+
outputs=[model_status]
|
781 |
+
)
|
782 |
+
model_choice.change(
|
783 |
+
fn=handle_model_click,
|
784 |
+
inputs=[model_choice],
|
785 |
+
outputs=[model_status]
|
786 |
+
)
|
787 |
|
788 |
# PDF 處理事件
|
789 |
summary_btn.click(generate_summary, inputs=pdf_upload, outputs=summary_output)
|