Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,73 @@
|
|
1 |
-
import openai
|
2 |
-
import gradio as gr
|
3 |
-
import fitz # PyMuPDF
|
4 |
-
|
5 |
-
api_key = ""
|
6 |
-
selected_model = ""
|
7 |
-
summary_text = ""
|
8 |
-
|
9 |
-
def set_api_key(user_api_key):
|
10 |
-
global api_key
|
11 |
-
api_key = user_api_key
|
12 |
-
return "✅ API Key 已設定"
|
13 |
-
|
14 |
-
def set_model(model_name):
|
15 |
-
global selected_model
|
16 |
-
selected_model = model_name
|
17 |
-
return f"✅ 模型已選:{model_name}"
|
18 |
-
|
19 |
-
def extract_pdf_text(file_obj):
|
20 |
-
doc = fitz.open(stream=file_obj.read(), filetype="pdf")
|
21 |
-
text = ""
|
22 |
-
for page in doc:
|
23 |
-
text += page.get_text()
|
24 |
-
return text
|
25 |
-
|
26 |
-
def generate_summary(pdf_file):
|
27 |
-
global summary_text
|
28 |
-
client = openai.OpenAI(api_key=api_key)
|
29 |
-
pdf_text = extract_pdf_text(pdf_file)
|
30 |
-
if not pdf_text.strip():
|
31 |
-
return "⚠️ 無法解析 PDF 文字,可能為純圖片 PDF。"
|
32 |
-
response = client.chat.completions.create(
|
33 |
-
model=selected_model,
|
34 |
-
messages=[
|
35 |
-
{"role": "system", "content": "請將以下 PDF 內容整理為條列式摘要重點。"},
|
36 |
-
{"role": "user", "content": pdf_text[:4000]}
|
37 |
-
]
|
38 |
-
)
|
39 |
-
summary_text = response.choices[0].message.content
|
40 |
-
return summary_text
|
41 |
-
|
42 |
-
def ask_question(user_question):
|
43 |
-
client = openai.OpenAI(api_key=api_key)
|
44 |
-
response = client.chat.completions.create(
|
45 |
-
model=selected_model,
|
46 |
-
messages=[
|
47 |
-
{"role": "system", "content": f"根據以下 PDF 摘要內容回答問題:\n{summary_text}"},
|
48 |
-
{"role": "user", "content": user_question}
|
49 |
-
]
|
50 |
-
)
|
51 |
-
return response.choices[0].message.content
|
52 |
-
|
53 |
-
with gr.Blocks() as demo:
|
54 |
-
gr.Markdown("# 📄 PDF 摘要 & 問答助手 (Hugging Face 版)")
|
55 |
-
api_key_input = gr.Textbox(label="輸入 OpenAI API Key", type="password")
|
56 |
-
api_key_status = gr.Textbox(label="狀態", interactive=False)
|
57 |
-
api_key_input.submit(set_api_key, inputs=api_key_input, outputs=api_key_status)
|
58 |
-
|
59 |
-
model_choice = gr.Radio(["gpt-4", "gpt-4.1-nano", "gpt-4o"], label="選擇模型")
|
60 |
-
model_status = gr.Textbox(label="模型狀態", interactive=False)
|
61 |
-
model_choice.change(set_model, inputs=model_choice, outputs=model_status)
|
62 |
-
|
63 |
-
pdf_upload = gr.File(label="上傳 PDF")
|
64 |
-
summary_output = gr.Textbox(label="PDF 摘要", lines=10)
|
65 |
-
summary_btn = gr.Button("生成摘要")
|
66 |
-
summary_btn.click(generate_summary, inputs=pdf_upload, outputs=summary_output)
|
67 |
-
|
68 |
-
question_input = gr.Textbox(label="請輸入您的問題")
|
69 |
-
answer_output = gr.Textbox(label="AI 回答", lines=5)
|
70 |
-
question_btn = gr.Button("送出問題")
|
71 |
-
question_btn.click(ask_question, inputs=question_input, outputs=answer_output)
|
72 |
-
|
73 |
-
demo.launch()
|
|
|
1 |
+
import openai
|
2 |
+
import gradio as gr
|
3 |
+
import fitz # PyMuPDF
|
4 |
+
|
5 |
+
api_key = ""
|
6 |
+
selected_model = ""
|
7 |
+
summary_text = ""
|
8 |
+
|
9 |
+
def set_api_key(user_api_key):
|
10 |
+
global api_key
|
11 |
+
api_key = user_api_key
|
12 |
+
return "✅ API Key 已設定"
|
13 |
+
|
14 |
+
def set_model(model_name):
|
15 |
+
global selected_model
|
16 |
+
selected_model = model_name
|
17 |
+
return f"✅ 模型已選:{model_name}"
|
18 |
+
|
19 |
+
def extract_pdf_text(file_obj):
|
20 |
+
doc = fitz.open(stream=file_obj.read(), filetype="pdf")
|
21 |
+
text = ""
|
22 |
+
for page in doc:
|
23 |
+
text += page.get_text()
|
24 |
+
return text
|
25 |
+
|
26 |
+
def generate_summary(pdf_file):
|
27 |
+
global summary_text
|
28 |
+
client = openai.OpenAI(api_key=api_key)
|
29 |
+
pdf_text = extract_pdf_text(pdf_file)
|
30 |
+
if not pdf_text.strip():
|
31 |
+
return "⚠️ 無法解析 PDF 文字,可能為純圖片 PDF。"
|
32 |
+
response = client.chat.completions.create(
|
33 |
+
model=selected_model,
|
34 |
+
messages=[
|
35 |
+
{"role": "system", "content": "請將以下 PDF 內容整理為條列式摘要重點。"},
|
36 |
+
{"role": "user", "content": pdf_text[:4000]}
|
37 |
+
]
|
38 |
+
)
|
39 |
+
summary_text = response.choices[0].message.content
|
40 |
+
return summary_text
|
41 |
+
|
42 |
+
def ask_question(user_question):
|
43 |
+
client = openai.OpenAI(api_key=api_key)
|
44 |
+
response = client.chat.completions.create(
|
45 |
+
model=selected_model,
|
46 |
+
messages=[
|
47 |
+
{"role": "system", "content": f"根據以下 PDF 摘要內容回答問題:\n{summary_text}"},
|
48 |
+
{"role": "user", "content": user_question}
|
49 |
+
]
|
50 |
+
)
|
51 |
+
return response.choices[0].message.content
|
52 |
+
|
53 |
+
with gr.Blocks() as demo:
|
54 |
+
gr.Markdown("# 📄 PDF 摘要 & 問答助手 (Hugging Face 版)")
|
55 |
+
api_key_input = gr.Textbox(label="輸入 OpenAI API Key", type="password")
|
56 |
+
api_key_status = gr.Textbox(label="狀態", interactive=False)
|
57 |
+
api_key_input.submit(set_api_key, inputs=api_key_input, outputs=api_key_status)
|
58 |
+
|
59 |
+
model_choice = gr.Radio(["gpt-4", "gpt-4.1-nano", "gpt-4o"], label="選擇模型")
|
60 |
+
model_status = gr.Textbox(label="模型狀態", interactive=False)
|
61 |
+
model_choice.change(set_model, inputs=model_choice, outputs=model_status)
|
62 |
+
|
63 |
+
pdf_upload = gr.File(label="上傳 PDF")
|
64 |
+
summary_output = gr.Textbox(label="PDF 摘要", lines=10)
|
65 |
+
summary_btn = gr.Button("生成摘要")
|
66 |
+
summary_btn.click(generate_summary, inputs=pdf_upload, outputs=summary_output)
|
67 |
+
|
68 |
+
question_input = gr.Textbox(label="請輸入您的問題")
|
69 |
+
answer_output = gr.Textbox(label="AI 回答", lines=5)
|
70 |
+
question_btn = gr.Button("送出問題")
|
71 |
+
question_btn.click(ask_question, inputs=question_input, outputs=answer_output)
|
72 |
+
|
73 |
+
demo.launch(share=True)
|