Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -160,17 +160,160 @@ def clear_all():
|
|
160 |
pdf_text = ""
|
161 |
return "", "", ""
|
162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
# 創建 Gradio 介面
|
164 |
with gr.Blocks(
|
165 |
title="PDF 摘要助手",
|
166 |
-
css=
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
.gr-footer { display: none !important; }
|
173 |
-
"""
|
174 |
) as demo:
|
175 |
|
176 |
gr.Markdown("""
|
@@ -197,41 +340,47 @@ with gr.Blocks(
|
|
197 |
api_key_input = gr.Textbox(
|
198 |
label="🔑 輸入 OpenAI API Key",
|
199 |
type="password",
|
200 |
-
placeholder="請輸入您的 OpenAI API Key (sk-...)"
|
|
|
201 |
)
|
202 |
-
api_key_btn = gr.Button("確認 API Key", variant="primary")
|
203 |
api_key_status = gr.Textbox(
|
204 |
label="📊 API 狀態",
|
205 |
interactive=False,
|
206 |
-
value="🔄 等待設定 API Key..."
|
|
|
207 |
)
|
208 |
|
209 |
gr.Markdown("### 模型選擇")
|
210 |
model_choice = gr.Radio(
|
211 |
-
["gpt-
|
212 |
label="🤖 選擇 AI 模型",
|
213 |
-
value="gpt-4"
|
|
|
214 |
)
|
215 |
model_status = gr.Textbox(
|
216 |
label="🎯 模型狀態",
|
217 |
interactive=False,
|
218 |
-
value="✅ 已選擇:gpt-4"
|
|
|
219 |
)
|
220 |
|
221 |
with gr.Tab("📄 PDF 摘要"):
|
222 |
gr.Markdown("### 文件上傳與摘要生成")
|
223 |
pdf_upload = gr.File(
|
224 |
label="📁 上傳 PDF 文件",
|
225 |
-
file_types=[".pdf"]
|
|
|
226 |
)
|
227 |
with gr.Row():
|
228 |
-
summary_btn = gr.Button("🔄 生成摘要", variant="primary")
|
229 |
-
clear_btn = gr.Button("🗑️ 清除資料", variant="secondary")
|
230 |
|
231 |
summary_output = gr.Textbox(
|
232 |
label="📋 PDF 摘要",
|
233 |
lines=15,
|
234 |
-
placeholder="上傳 PDF 文件並點擊「生成摘要」按鈕,AI 將為您分析文檔內容..."
|
|
|
235 |
)
|
236 |
|
237 |
with gr.Tab("❓ 智能問答"):
|
@@ -239,14 +388,16 @@ with gr.Blocks(
|
|
239 |
question_input = gr.Textbox(
|
240 |
label="💭 請輸入您的問題",
|
241 |
lines=3,
|
242 |
-
placeholder="例如:這份文件的主要結論是什麼?文中提到的關鍵數據有哪些?"
|
|
|
243 |
)
|
244 |
-
question_btn = gr.Button("📤 送出問題", variant="primary")
|
245 |
|
246 |
answer_output = gr.Textbox(
|
247 |
label="🤖 AI 回答",
|
248 |
lines=12,
|
249 |
-
placeholder="請先上傳並生成 PDF 摘要,然後輸入問題,AI 將基於文檔內容為您提供回答..."
|
|
|
250 |
)
|
251 |
|
252 |
gr.Markdown("""
|
@@ -257,7 +408,7 @@ with gr.Blocks(
|
|
257 |
- 文件中提到的建議有哪些?
|
258 |
""")
|
259 |
|
260 |
-
# 事件綁定
|
261 |
api_key_btn.click(set_api_key, inputs=api_key_input, outputs=api_key_status)
|
262 |
api_key_input.submit(set_api_key, inputs=api_key_input, outputs=api_key_status)
|
263 |
|
@@ -273,5 +424,7 @@ with gr.Blocks(
|
|
273 |
if __name__ == "__main__":
|
274 |
demo.launch(
|
275 |
show_error=True,
|
276 |
-
share=
|
277 |
-
|
|
|
|
|
|
160 |
pdf_text = ""
|
161 |
return "", "", ""
|
162 |
|
163 |
+
# 自定義 CSS 樣式 - 藍紫色主題
|
164 |
+
custom_css = """
|
165 |
+
/* 主要容器背景 */
|
166 |
+
.gradio-container {
|
167 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
168 |
+
min-height: 100vh;
|
169 |
+
}
|
170 |
+
|
171 |
+
/* 隱藏 Gradio footer 和 logo */
|
172 |
+
footer { display: none !important; }
|
173 |
+
.gradio-container footer { display: none !important; }
|
174 |
+
div[class*="footer"] { display: none !important; }
|
175 |
+
div[class*="Footer"] { display: none !important; }
|
176 |
+
.gr-footer { display: none !important; }
|
177 |
+
|
178 |
+
/* 標籤頁樣式 */
|
179 |
+
.tab-nav {
|
180 |
+
background: rgba(255, 255, 255, 0.1) !important;
|
181 |
+
border-radius: 15px !important;
|
182 |
+
backdrop-filter: blur(10px) !important;
|
183 |
+
margin-bottom: 20px !important;
|
184 |
+
}
|
185 |
+
|
186 |
+
.tab-nav button {
|
187 |
+
background: rgba(255, 255, 255, 0.1) !important;
|
188 |
+
color: white !important;
|
189 |
+
border: none !important;
|
190 |
+
border-radius: 10px !important;
|
191 |
+
margin: 5px !important;
|
192 |
+
font-weight: 600 !important;
|
193 |
+
transition: all 0.3s ease !important;
|
194 |
+
}
|
195 |
+
|
196 |
+
.tab-nav button:hover {
|
197 |
+
background: rgba(255, 255, 255, 0.2) !important;
|
198 |
+
transform: translateY(-2px) !important;
|
199 |
+
}
|
200 |
+
|
201 |
+
.tab-nav button.selected {
|
202 |
+
background: rgba(255, 255, 255, 0.3) !important;
|
203 |
+
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2) !important;
|
204 |
+
}
|
205 |
+
|
206 |
+
/* 卡片樣式 */
|
207 |
+
.block {
|
208 |
+
background: rgba(255, 255, 255, 0.95) !important;
|
209 |
+
border-radius: 20px !important;
|
210 |
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1) !important;
|
211 |
+
backdrop-filter: blur(10px) !important;
|
212 |
+
border: 1px solid rgba(255, 255, 255, 0.2) !important;
|
213 |
+
margin: 10px 0 !important;
|
214 |
+
padding: 20px !important;
|
215 |
+
}
|
216 |
+
|
217 |
+
/* 按鈕樣式 */
|
218 |
+
.btn {
|
219 |
+
background: linear-gradient(45deg, #667eea, #764ba2) !important;
|
220 |
+
color: white !important;
|
221 |
+
border: none !important;
|
222 |
+
border-radius: 15px !important;
|
223 |
+
padding: 12px 24px !important;
|
224 |
+
font-weight: 600 !important;
|
225 |
+
transition: all 0.3s ease !important;
|
226 |
+
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4) !important;
|
227 |
+
}
|
228 |
+
|
229 |
+
.btn:hover {
|
230 |
+
transform: translateY(-3px) !important;
|
231 |
+
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6) !important;
|
232 |
+
}
|
233 |
+
|
234 |
+
.btn-secondary {
|
235 |
+
background: linear-gradient(45deg, #a8a8a8, #6c757d) !important;
|
236 |
+
box-shadow: 0 4px 15px rgba(168, 168, 168, 0.4) !important;
|
237 |
+
}
|
238 |
+
|
239 |
+
.btn-secondary:hover {
|
240 |
+
box-shadow: 0 6px 20px rgba(168, 168, 168, 0.6) !important;
|
241 |
+
}
|
242 |
+
|
243 |
+
/* 輸入框樣式 */
|
244 |
+
.gr-textbox, .gr-file, .gr-radio {
|
245 |
+
border-radius: 15px !important;
|
246 |
+
border: 2px solid rgba(102, 126, 234, 0.3) !important;
|
247 |
+
background: rgba(255, 255, 255, 0.9) !important;
|
248 |
+
transition: all 0.3s ease !important;
|
249 |
+
}
|
250 |
+
|
251 |
+
.gr-textbox:focus, .gr-file:focus {
|
252 |
+
border-color: #667eea !important;
|
253 |
+
box-shadow: 0 0 20px rgba(102, 126, 234, 0.3) !important;
|
254 |
+
}
|
255 |
+
|
256 |
+
/* 標題樣式 */
|
257 |
+
h1, h2, h3 {
|
258 |
+
color: white !important;
|
259 |
+
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3) !important;
|
260 |
+
font-weight: 700 !important;
|
261 |
+
}
|
262 |
+
|
263 |
+
/* Markdown 內容樣式 */
|
264 |
+
.markdown {
|
265 |
+
background: rgba(255, 255, 255, 0.95) !important;
|
266 |
+
border-radius: 15px !important;
|
267 |
+
padding: 20px !important;
|
268 |
+
margin: 10px 0 !important;
|
269 |
+
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1) !important;
|
270 |
+
}
|
271 |
+
|
272 |
+
/* 進度條樣式 */
|
273 |
+
.progress {
|
274 |
+
background: linear-gradient(45deg, #667eea, #764ba2) !important;
|
275 |
+
border-radius: 10px !important;
|
276 |
+
}
|
277 |
+
|
278 |
+
/* 滾動條樣式 */
|
279 |
+
::-webkit-scrollbar {
|
280 |
+
width: 8px;
|
281 |
+
}
|
282 |
+
|
283 |
+
::-webkit-scrollbar-track {
|
284 |
+
background: rgba(255, 255, 255, 0.1);
|
285 |
+
border-radius: 10px;
|
286 |
+
}
|
287 |
+
|
288 |
+
::-webkit-scrollbar-thumb {
|
289 |
+
background: linear-gradient(45deg, #667eea, #764ba2);
|
290 |
+
border-radius: 10px;
|
291 |
+
}
|
292 |
+
|
293 |
+
::-webkit-scrollbar-thumb:hover {
|
294 |
+
background: linear-gradient(45deg, #5a6fd8, #6a4190);
|
295 |
+
}
|
296 |
+
|
297 |
+
/* 動畫效果 */
|
298 |
+
@keyframes fadeIn {
|
299 |
+
from { opacity: 0; transform: translateY(20px); }
|
300 |
+
to { opacity: 1; transform: translateY(0); }
|
301 |
+
}
|
302 |
+
|
303 |
+
.block {
|
304 |
+
animation: fadeIn 0.6s ease-out !important;
|
305 |
+
}
|
306 |
+
"""
|
307 |
+
|
308 |
# 創建 Gradio 介面
|
309 |
with gr.Blocks(
|
310 |
title="PDF 摘要助手",
|
311 |
+
css=custom_css,
|
312 |
+
theme=gr.themes.Soft(
|
313 |
+
primary_hue="blue",
|
314 |
+
secondary_hue="purple",
|
315 |
+
neutral_hue="slate",
|
316 |
+
)
|
|
|
|
|
317 |
) as demo:
|
318 |
|
319 |
gr.Markdown("""
|
|
|
340 |
api_key_input = gr.Textbox(
|
341 |
label="🔑 輸入 OpenAI API Key",
|
342 |
type="password",
|
343 |
+
placeholder="請輸入您的 OpenAI API Key (sk-...)",
|
344 |
+
elem_classes=["gr-textbox"]
|
345 |
)
|
346 |
+
api_key_btn = gr.Button("確認 API Key", variant="primary", elem_classes=["btn"])
|
347 |
api_key_status = gr.Textbox(
|
348 |
label="📊 API 狀態",
|
349 |
interactive=False,
|
350 |
+
value="🔄 等待設定 API Key...",
|
351 |
+
elem_classes=["gr-textbox"]
|
352 |
)
|
353 |
|
354 |
gr.Markdown("### 模型選擇")
|
355 |
model_choice = gr.Radio(
|
356 |
+
["gpt-3.5-turbo", "gpt-4", "gpt-4-turbo"],
|
357 |
label="🤖 選擇 AI 模型",
|
358 |
+
value="gpt-4",
|
359 |
+
elem_classes=["gr-radio"]
|
360 |
)
|
361 |
model_status = gr.Textbox(
|
362 |
label="🎯 模型狀態",
|
363 |
interactive=False,
|
364 |
+
value="✅ 已選擇:gpt-4",
|
365 |
+
elem_classes=["gr-textbox"]
|
366 |
)
|
367 |
|
368 |
with gr.Tab("📄 PDF 摘要"):
|
369 |
gr.Markdown("### 文件上傳與摘要生成")
|
370 |
pdf_upload = gr.File(
|
371 |
label="📁 上傳 PDF 文件",
|
372 |
+
file_types=[".pdf"],
|
373 |
+
elem_classes=["gr-file"]
|
374 |
)
|
375 |
with gr.Row():
|
376 |
+
summary_btn = gr.Button("🔄 生成摘要", variant="primary", elem_classes=["btn"])
|
377 |
+
clear_btn = gr.Button("🗑️ 清除資料", variant="secondary", elem_classes=["btn", "btn-secondary"])
|
378 |
|
379 |
summary_output = gr.Textbox(
|
380 |
label="📋 PDF 摘要",
|
381 |
lines=15,
|
382 |
+
placeholder="上傳 PDF 文件並點擊「生成摘要」按鈕,AI 將為您分析文檔內容...",
|
383 |
+
elem_classes=["gr-textbox"]
|
384 |
)
|
385 |
|
386 |
with gr.Tab("❓ 智能問答"):
|
|
|
388 |
question_input = gr.Textbox(
|
389 |
label="💭 請輸入您的問題",
|
390 |
lines=3,
|
391 |
+
placeholder="例如:這份文件的主要結論是什麼?文中提到的關鍵數據有哪些?",
|
392 |
+
elem_classes=["gr-textbox"]
|
393 |
)
|
394 |
+
question_btn = gr.Button("📤 送出問題", variant="primary", elem_classes=["btn"])
|
395 |
|
396 |
answer_output = gr.Textbox(
|
397 |
label="🤖 AI 回答",
|
398 |
lines=12,
|
399 |
+
placeholder="請先上傳並生成 PDF 摘要,然後輸入問題,AI 將基於文檔內容為您提供回答...",
|
400 |
+
elem_classes=["gr-textbox"]
|
401 |
)
|
402 |
|
403 |
gr.Markdown("""
|
|
|
408 |
- 文件中提到的建議有哪些?
|
409 |
""")
|
410 |
|
411 |
+
# 事件綁定
|
412 |
api_key_btn.click(set_api_key, inputs=api_key_input, outputs=api_key_status)
|
413 |
api_key_input.submit(set_api_key, inputs=api_key_input, outputs=api_key_status)
|
414 |
|
|
|
424 |
if __name__ == "__main__":
|
425 |
demo.launch(
|
426 |
show_error=True,
|
427 |
+
share=True,
|
428 |
+
server_name="0.0.0.0",
|
429 |
+
server_port=7860
|
430 |
+
)
|