Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -47,18 +47,18 @@ def extract_pdf_text(file_path):
|
|
47 |
def generate_summary(pdf_file):
|
48 |
global summary_text, pdf_text
|
49 |
if not client:
|
50 |
-
return "❌ 請先設定 API Key"
|
51 |
if not pdf_file:
|
52 |
return "❌ 請先上傳 PDF 文件"
|
53 |
try:
|
54 |
pdf_text = extract_pdf_text(pdf_file.name)
|
55 |
if not pdf_text.strip():
|
56 |
-
return "⚠️ 無法解析 PDF
|
57 |
pdf_text_truncated = pdf_text[:8000]
|
58 |
response = client.chat.completions.create(
|
59 |
model=selected_model,
|
60 |
messages=[
|
61 |
-
{"role": "system", "content": "
|
62 |
{"role": "user", "content": pdf_text_truncated}
|
63 |
],
|
64 |
temperature=0.3
|
@@ -71,7 +71,7 @@ def generate_summary(pdf_file):
|
|
71 |
|
72 |
def ask_question(user_question):
|
73 |
if not client:
|
74 |
-
return "❌ 請先設定 API Key"
|
75 |
if not summary_text and not pdf_text:
|
76 |
return "❌ 請先生成 PDF 摘要"
|
77 |
if not user_question.strip():
|
@@ -97,52 +97,34 @@ def clear_all():
|
|
97 |
pdf_text = ""
|
98 |
return "", "", ""
|
99 |
|
100 |
-
with gr.Blocks(
|
101 |
-
|
102 |
-
css="""
|
103 |
-
.gradio-container {
|
104 |
-
max-width: none !important;
|
105 |
-
width: 100% !important;
|
106 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
107 |
-
min-height: 100vh;
|
108 |
-
}
|
109 |
-
.main-content {
|
110 |
-
max-width: 1600px !important;
|
111 |
-
margin: 20px auto !important;
|
112 |
-
padding: 30px !important;
|
113 |
-
background: rgba(255, 255, 255, 0.95) !important;
|
114 |
-
border-radius: 20px !important;
|
115 |
-
}
|
116 |
-
"""
|
117 |
-
) as demo:
|
118 |
-
with gr.Column():
|
119 |
-
gr.Markdown("## 📄 PDF 摘要 & 問答助手")
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
|
144 |
-
|
145 |
-
|
146 |
|
147 |
if __name__ == "__main__":
|
148 |
demo.launch(show_error=True)
|
|
|
47 |
def generate_summary(pdf_file):
|
48 |
global summary_text, pdf_text
|
49 |
if not client:
|
50 |
+
return "❌ 請先設定 OpenAI API Key"
|
51 |
if not pdf_file:
|
52 |
return "❌ 請先上傳 PDF 文件"
|
53 |
try:
|
54 |
pdf_text = extract_pdf_text(pdf_file.name)
|
55 |
if not pdf_text.strip():
|
56 |
+
return "⚠️ 無法解析 PDF 文字,可能為純圖片 PDF 或空白文件。"
|
57 |
pdf_text_truncated = pdf_text[:8000]
|
58 |
response = client.chat.completions.create(
|
59 |
model=selected_model,
|
60 |
messages=[
|
61 |
+
{"role": "system", "content": "請將以下 PDF 內容整理為條列式摘要,用繁體中文回答:"},
|
62 |
{"role": "user", "content": pdf_text_truncated}
|
63 |
],
|
64 |
temperature=0.3
|
|
|
71 |
|
72 |
def ask_question(user_question):
|
73 |
if not client:
|
74 |
+
return "❌ 請先設定 OpenAI API Key"
|
75 |
if not summary_text and not pdf_text:
|
76 |
return "❌ 請先生成 PDF 摘要"
|
77 |
if not user_question.strip():
|
|
|
97 |
pdf_text = ""
|
98 |
return "", "", ""
|
99 |
|
100 |
+
with gr.Blocks(title="PDF 摘要助手") as demo:
|
101 |
+
gr.Markdown("## 📄 PDF 摘要 & 問答助手")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
+
with gr.Tab("🔧 設定"):
|
104 |
+
api_key_input = gr.Textbox(label="🔑 輸入 OpenAI API Key", type="password")
|
105 |
+
api_key_status = gr.Textbox(label="API 狀態", interactive=False, value="等待設定 API Key...")
|
106 |
+
api_key_btn = gr.Button("確認 API Key")
|
107 |
+
api_key_btn.click(set_api_key, inputs=api_key_input, outputs=api_key_status)
|
108 |
|
109 |
+
model_choice = gr.Radio(["gpt-4", "gpt-4.1", "gpt-4.5"], label="選擇 AI 模型", value="gpt-4")
|
110 |
+
model_status = gr.Textbox(label="模型狀態", interactive=False, value="✅ 已選擇:gpt-4")
|
111 |
+
model_choice.change(set_model, inputs=model_choice, outputs=model_status)
|
112 |
|
113 |
+
with gr.Tab("📄 摘要"):
|
114 |
+
pdf_upload = gr.File(label="上傳 PDF", file_types=[".pdf"])
|
115 |
+
summary_btn = gr.Button("生成摘要")
|
116 |
+
summary_output = gr.Textbox(label="PDF 摘要", lines=12)
|
117 |
+
summary_btn.click(generate_summary, inputs=pdf_upload, outputs=summary_output)
|
118 |
|
119 |
+
with gr.Tab("❓ 問答"):
|
120 |
+
question_input = gr.Textbox(label="請輸入問題", lines=2)
|
121 |
+
question_btn = gr.Button("送出問題")
|
122 |
+
answer_output = gr.Textbox(label="AI 回答", lines=8)
|
123 |
+
question_btn.click(ask_question, inputs=question_input, outputs=answer_output)
|
124 |
+
question_input.submit(ask_question, inputs=question_input, outputs=answer_output)
|
125 |
|
126 |
+
clear_btn = gr.Button("🗑️ 清除所有資料")
|
127 |
+
clear_btn.click(clear_all, outputs=[summary_output, question_input, answer_output])
|
128 |
|
129 |
if __name__ == "__main__":
|
130 |
demo.launch(show_error=True)
|