alanchen1115 commited on
Commit
dbabdcf
·
verified ·
1 Parent(s): 4de4077

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +3 -4
main.py CHANGED
@@ -1,4 +1,4 @@
1
- import json, os, pathlib, glob
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
4
  from google import genai
@@ -15,7 +15,7 @@ generation_config = genai.types.GenerateContentConfig(max_output_tokens=3000, te
15
 
16
  # Retrieve the PDF
17
  files = glob.glob('docs/*.pdf')
18
- files_content = [genai.types.Part.from_bytes(data=(pathlib.Path(filepath)).read_bytes(), mime_type='application/pdf') for filepath in files]
19
 
20
  # 設定 Line Bot 的 API 金鑰和秘密金鑰
21
  line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
@@ -89,8 +89,7 @@ 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 = [prompt] + files_content
93
- # 使用 client.models.generate_content
94
  completion = client.models.generate_content(
95
  model="gemini-2.5-flash",
96
  contents=combined_contents,
 
1
+ import json, os, glob
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
4
  from google import genai
 
15
 
16
  # Retrieve the PDF
17
  files = glob.glob('docs/*.pdf')
18
+ files_content = [client.files.upload(filefilepath) for filepath in files]
19
 
20
  # 設定 Line Bot 的 API 金鑰和秘密金鑰
21
  line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
 
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,