Spaces:
Sleeping
Sleeping
File size: 5,373 Bytes
1687ca3 1f07c6f b11b466 1687ca3 b11b466 1687ca3 b11b466 40774bb 1687ca3 40774bb b11b466 1f07c6f c0edcf5 b11b466 c0edcf5 1687ca3 c0edcf5 1687ca3 2bf436a b11b466 1f07c6f b11b466 1687ca3 b11b466 1f07c6f b11b466 1f07c6f b11b466 1f07c6f b11b466 1f07c6f 1687ca3 b11b466 1f07c6f b11b466 1f07c6f b11b466 1f07c6f 1687ca3 b11b466 1687ca3 a3f064a c0edcf5 1f07c6f c0edcf5 1f07c6f c0edcf5 1f07c6f c0edcf5 1f07c6f c0edcf5 1f07c6f 0c8d1ca 1f07c6f 1687ca3 b11b466 1f07c6f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 148 149 |
import openai
import gradio as gr
import fitz
from openai import OpenAI
import traceback
api_key = ""
selected_model = "gpt-4"
summary_text = ""
client = None
pdf_text = ""
def set_api_key(user_api_key):
global api_key, client
try:
api_key = user_api_key.strip()
if not api_key:
return "❌ API Key 不能為空"
client = OpenAI(api_key=api_key)
client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "你好"}],
max_tokens=5
)
return "✅ API Key 已設定並驗證成功"
except Exception as e:
return f"❌ API Key 設定失敗: {str(e)}"
def set_model(model_name):
global selected_model
selected_model = model_name
return f"✅ 模型已選擇:{model_name}"
def extract_pdf_text(file_path):
try:
doc = fitz.open(file_path)
text = ""
for page_num, page in enumerate(doc):
page_text = page.get_text()
if page_text.strip():
text += f"\n--- 第 {page_num + 1} 頁 ---\n{page_text}"
doc.close()
return text
except Exception as e:
return f"❌ PDF 解析錯誤: {str(e)}"
def generate_summary(pdf_file):
global summary_text, pdf_text
if not client:
return "❌ 請先設定 API Key"
if not pdf_file:
return "❌ 請先上傳 PDF 文件"
try:
pdf_text = extract_pdf_text(pdf_file.name)
if not pdf_text.strip():
return "⚠️ 無法解析 PDF 文字"
pdf_text_truncated = pdf_text[:8000]
response = client.chat.completions.create(
model=selected_model,
messages=[
{"role": "system", "content": "請用繁體中文整理以下 PDF 內容摘要。"},
{"role": "user", "content": pdf_text_truncated}
],
temperature=0.3
)
summary_text = response.choices[0].message.content
return summary_text
except Exception as e:
print(traceback.format_exc())
return f"❌ 摘要生成失敗: {str(e)}"
def ask_question(user_question):
if not client:
return "❌ 請先設定 API Key"
if not summary_text and not pdf_text:
return "❌ 請先生成 PDF 摘要"
if not user_question.strip():
return "❌ 請輸入問題"
try:
context = f"PDF 摘要:\n{summary_text}\n\n原始內容(部分):\n{pdf_text[:2000]}"
response = client.chat.completions.create(
model=selected_model,
messages=[
{"role": "system", "content": f"根據以下 PDF 內容回答問題,請用繁體中文回答:\n{context}"},
{"role": "user", "content": user_question}
],
temperature=0.2
)
return response.choices[0].message.content
except Exception as e:
print(traceback.format_exc())
return f"❌ 問答生成失敗: {str(e)}"
def clear_all():
global summary_text, pdf_text
summary_text = ""
pdf_text = ""
return "", "", ""
with gr.Blocks(
title="PDF 摘要助手",
css="""
.gradio-container {
max-width: none !important;
width: 100% !important;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
min-height: 100vh;
}
.main-content {
max-width: 1600px !important;
margin: 20px auto !important;
padding: 30px !important;
background: rgba(255, 255, 255, 0.95) !important;
border-radius: 20px !important;
}
"""
) as demo:
with gr.Column():
gr.Markdown("## 📄 PDF 摘要 & 問答助手")
with gr.Tab("🔧 設定"):
api_key_input = gr.Textbox(label="🔑 輸入 OpenAI API Key", type="password")
api_key_status = gr.Textbox(label="API 狀態", interactive=False, value="等待設定 API Key...")
api_key_btn = gr.Button("確認 API Key")
api_key_btn.click(set_api_key, inputs=api_key_input, outputs=api_key_status)
model_choice = gr.Radio(["gpt-4", "gpt-4.1", "gpt-4.5"], label="選擇 AI 模型", value="gpt-4")
model_status = gr.Textbox(label="模型狀態", interactive=False, value="✅ 已選擇:gpt-4")
model_choice.change(set_model, inputs=model_choice, outputs=model_status)
with gr.Tab("📄 摘要"):
pdf_upload = gr.File(label="上傳 PDF", file_types=[".pdf"])
summary_btn = gr.Button("生成摘要")
summary_output = gr.Textbox(label="PDF 摘要", lines=12)
summary_btn.click(generate_summary, inputs=pdf_upload, outputs=summary_output)
with gr.Tab("❓ 問答"):
question_input = gr.Textbox(label="請輸入問題", lines=2)
question_btn = gr.Button("送出問題")
answer_output = gr.Textbox(label="AI 回答", lines=8)
question_btn.click(ask_question, inputs=question_input, outputs=answer_output)
question_input.submit(ask_question, inputs=question_input, outputs=answer_output)
clear_btn = gr.Button("🗑️ 清除所有資料")
clear_btn.click(clear_all, outputs=[summary_output, question_input, answer_output])
if __name__ == "__main__":
demo.launch(show_error=True)
|