3a05chatgpt commited on
Commit
c746818
·
verified ·
1 Parent(s): ea1016c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -8
app.py CHANGED
@@ -9,6 +9,7 @@ summary_text = ""
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):
@@ -17,7 +18,7 @@ def set_model(model_name):
17
  return f"✅ 模型已選:{model_name}"
18
 
19
  def extract_pdf_text(file_path):
20
- doc = fitz.open(file_path) # 直接用檔案路徑打開
21
  text = ""
22
  for page in doc:
23
  text += page.get_text()
@@ -25,30 +26,28 @@ def extract_pdf_text(file_path):
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 版)")
@@ -57,7 +56,7 @@ with gr.Blocks() as demo:
57
  api_key_status = gr.Textbox(label="狀態", interactive=False)
58
  api_key_input.submit(set_api_key, inputs=api_key_input, outputs=api_key_status)
59
 
60
- model_choice = gr.Radio(["gpt-4", "gpt-4.1-nano", "gpt-4o"], label="選擇模型")
61
  model_status = gr.Textbox(label="模型狀態", interactive=False)
62
  model_choice.change(set_model, inputs=model_choice, outputs=model_status)
63
 
 
9
  def set_api_key(user_api_key):
10
  global api_key
11
  api_key = user_api_key
12
+ openai.api_key = api_key # ✅ 重點修正:直接全域設置
13
  return "✅ API Key 已設定"
14
 
15
  def set_model(model_name):
 
18
  return f"✅ 模型已選:{model_name}"
19
 
20
  def extract_pdf_text(file_path):
21
+ doc = fitz.open(file_path)
22
  text = ""
23
  for page in doc:
24
  text += page.get_text()
 
26
 
27
  def generate_summary(pdf_file):
28
  global summary_text
 
29
  pdf_text = extract_pdf_text(pdf_file)
30
  if not pdf_text.strip():
31
  return "⚠️ 無法解析 PDF 文字,可能為純圖片 PDF。"
32
+ response = openai.ChatCompletion.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
+ response = openai.ChatCompletion.create(
 
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['choices'][0]['message']['content']
51
 
52
  with gr.Blocks() as demo:
53
  gr.Markdown("# 📄 PDF 摘要 & 問答助手 (Hugging Face 版)")
 
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-3.5-turbo", "gpt-4", "gpt-4o"], label="選擇模型")
60
  model_status = gr.Textbox(label="模型狀態", interactive=False)
61
  model_choice.change(set_model, inputs=model_choice, outputs=model_status)
62