Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,18 @@
|
|
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 |
-
|
13 |
return "✅ API Key 已設定"
|
14 |
|
15 |
def set_model(model_name):
|
@@ -29,25 +32,25 @@ def generate_summary(pdf_file):
|
|
29 |
pdf_text = extract_pdf_text(pdf_file)
|
30 |
if not pdf_text.strip():
|
31 |
return "⚠️ 無法解析 PDF 文字,可能為純圖片 PDF。"
|
32 |
-
response =
|
33 |
model=selected_model,
|
34 |
messages=[
|
35 |
{"role": "system", "content": "請將以下 PDF 內容整理為條列式摘要重點。"},
|
36 |
{"role": "user", "content": pdf_text[:4000]}
|
37 |
]
|
38 |
)
|
39 |
-
summary_text = response
|
40 |
return summary_text
|
41 |
|
42 |
def ask_question(user_question):
|
43 |
-
response =
|
44 |
model=selected_model,
|
45 |
messages=[
|
46 |
{"role": "system", "content": f"根據以下 PDF 摘要內容回答問題:\n{summary_text}"},
|
47 |
{"role": "user", "content": user_question}
|
48 |
]
|
49 |
)
|
50 |
-
return response
|
51 |
|
52 |
with gr.Blocks() as demo:
|
53 |
gr.Markdown("# 📄 PDF 摘要 & 問答助手 (Hugging Face 版)")
|
|
|
1 |
import openai
|
2 |
import gradio as gr
|
3 |
import fitz # PyMuPDF
|
4 |
+
from openai import OpenAI # ✅ 新增正確 Client
|
5 |
|
6 |
api_key = ""
|
7 |
selected_model = ""
|
8 |
summary_text = ""
|
9 |
|
10 |
+
client = None # ✅ 全域 client 物件
|
11 |
+
|
12 |
def set_api_key(user_api_key):
|
13 |
+
global api_key, client
|
14 |
api_key = user_api_key
|
15 |
+
client = OpenAI(api_key=api_key) # ✅ 正確初始化新版 Client
|
16 |
return "✅ API Key 已設定"
|
17 |
|
18 |
def set_model(model_name):
|
|
|
32 |
pdf_text = extract_pdf_text(pdf_file)
|
33 |
if not pdf_text.strip():
|
34 |
return "⚠️ 無法解析 PDF 文字,可能為純圖片 PDF。"
|
35 |
+
response = client.chat.completions.create(
|
36 |
model=selected_model,
|
37 |
messages=[
|
38 |
{"role": "system", "content": "請將以下 PDF 內容整理為條列式摘要重點。"},
|
39 |
{"role": "user", "content": pdf_text[:4000]}
|
40 |
]
|
41 |
)
|
42 |
+
summary_text = response.choices[0].message.content
|
43 |
return summary_text
|
44 |
|
45 |
def ask_question(user_question):
|
46 |
+
response = client.chat.completions.create(
|
47 |
model=selected_model,
|
48 |
messages=[
|
49 |
{"role": "system", "content": f"根據以下 PDF 摘要內容回答問題:\n{summary_text}"},
|
50 |
{"role": "user", "content": user_question}
|
51 |
]
|
52 |
)
|
53 |
+
return response.choices[0].message.content
|
54 |
|
55 |
with gr.Blocks() as demo:
|
56 |
gr.Markdown("# 📄 PDF 摘要 & 問答助手 (Hugging Face 版)")
|