alanchen1115 commited on
Commit
889ff07
·
verified ·
1 Parent(s): ef15af5

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -5
main.py CHANGED
@@ -5,6 +5,7 @@ from google import genai
5
  from linebot import LineBotApi, WebhookHandler
6
  from linebot.exceptions import InvalidSignatureError
7
  from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
 
8
 
9
  # 設定 Google AI API 金鑰
10
  client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
@@ -15,7 +16,13 @@ generation_config = genai.types.GenerateContentConfig(max_output_tokens=3000, te
15
 
16
  # Retrieve the PDF
17
  files = glob.glob('docs/*.pdf')
18
- files_content = [client.files.upload(file=filepath) for filepath in files]
 
 
 
 
 
 
19
 
20
  # 設定 Line Bot 的 API 金鑰和秘密金鑰
21
  line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
@@ -89,15 +96,14 @@ def handle_message(event):
89
  # 取得使用者輸入的文字
90
  prompt = event.message.text
91
  # Combine the PDF contents and the new prompt into a single list for the 'contents' argument
92
- combined_contents = files_content + [prompt]
93
  completion = client.models.generate_content(
94
  model="gemini-2.5-flash",
95
- contents=combined_contents,
96
- config=generation_config)
97
  # 檢查生成結果是否為空
98
  if (completion != None):
99
  # 取得生成結果
100
- out = completion.text
101
  else:
102
  # 回覆 "Gemini沒答案!請換個說法!"
103
  out = "Gemini沒答案!請換個說法!"
 
5
  from linebot import LineBotApi, WebhookHandler
6
  from linebot.exceptions import InvalidSignatureError
7
  from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage
8
+ from pypdf import PdfMerger
9
 
10
  # 設定 Google AI API 金鑰
11
  client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
 
16
 
17
  # Retrieve the PDF
18
  files = glob.glob('docs/*.pdf')
19
+ merger = PdfMerger()
20
+ for file in files:
21
+ merger.append(file)
22
+ merger.write("docs/combined.pdf")
23
+ merger.close()
24
+
25
+ pdf_content = client.files.upload(file="docs/combined.pdf")
26
 
27
  # 設定 Line Bot 的 API 金鑰和秘密金鑰
28
  line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
 
96
  # 取得使用者輸入的文字
97
  prompt = event.message.text
98
  # Combine the PDF contents and the new prompt into a single list for the 'contents' argument
 
99
  completion = client.models.generate_content(
100
  model="gemini-2.5-flash",
101
+ contents=[pdf_content, prompt],
102
+ config=generation_config).text
103
  # 檢查生成結果是否為空
104
  if (completion != None):
105
  # 取得生成結果
106
+ out = completion
107
  else:
108
  # 回覆 "Gemini沒答案!請換個說法!"
109
  out = "Gemini沒答案!請換個說法!"